Skip to content

Commit bbf8160

Browse files
committed
flask: capture responseHeadersOnEntrySpans
Signed-off-by: Varsha GS <[email protected]>
1 parent 884edc3 commit bbf8160

File tree

4 files changed

+191
-87
lines changed

4 files changed

+191
-87
lines changed

instana/instrumentation/flask/vanilla.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717
path_tpl_re = re.compile('<.*>')
1818

1919

20+
def extract_custom_headers(span, headers, format):
21+
if agent.options.extra_http_headers is None:
22+
return
23+
try:
24+
for custom_header in agent.options.extra_http_headers:
25+
# Headers are available in this format: HTTP_X_CAPTURE_THIS
26+
flask_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
27+
28+
if flask_header in headers:
29+
span.set_tag("http.header.%s" % custom_header, headers[flask_header])
30+
31+
except Exception:
32+
logger.debug("extract_custom_headers: ", exc_info=True)
33+
34+
2035
def before_request_with_instana(*argv, **kwargs):
2136
try:
2237
env = flask.request.environ
@@ -25,12 +40,7 @@ def before_request_with_instana(*argv, **kwargs):
2540
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
2641
span = flask.g.scope.span
2742

28-
if agent.options.extra_http_headers is not None:
29-
for custom_header in agent.options.extra_http_headers:
30-
# Headers are available in this format: HTTP_X_CAPTURE_THIS
31-
header = ('HTTP_' + custom_header.upper()).replace('-', '_')
32-
if header in env:
33-
span.set_tag("http.header.%s" % custom_header, env[header])
43+
extract_custom_headers(span, env, format=True)
3444

3545
span.set_tag(ext.HTTP_METHOD, flask.request.method)
3646
if 'PATH_INFO' in env:
@@ -68,6 +78,8 @@ def after_request_with_instana(response):
6878
span.mark_as_errored()
6979

7080
span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
81+
extract_custom_headers(span, response.headers, format=False)
82+
7183
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
7284
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
7385
except:

instana/instrumentation/flask/with_blinker.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
path_tpl_re = re.compile('<.*>')
1919

2020

21+
def extract_custom_headers(span, headers, format):
22+
if agent.options.extra_http_headers is None:
23+
return
24+
try:
25+
for custom_header in agent.options.extra_http_headers:
26+
# Headers are available in this format: HTTP_X_CAPTURE_THIS
27+
flask_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
28+
29+
if flask_header in headers:
30+
span.set_tag("http.header.%s" % custom_header, headers[flask_header])
31+
32+
except Exception:
33+
logger.debug("extract_custom_headers: ", exc_info=True)
34+
35+
2136
def request_started_with_instana(sender, **extra):
2237
try:
2338
env = flask.request.environ
@@ -28,12 +43,7 @@ def request_started_with_instana(sender, **extra):
2843
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
2944
span = flask.g.scope.span
3045

31-
if agent.options.extra_http_headers is not None:
32-
for custom_header in agent.options.extra_http_headers:
33-
# Headers are available in this format: HTTP_X_CAPTURE_THIS
34-
header = ('HTTP_' + custom_header.upper()).replace('-', '_')
35-
if header in env:
36-
span.set_tag("http.header.%s" % custom_header, env[header])
46+
extract_custom_headers(span, env, format=True)
3747

3848
span.set_tag(ext.HTTP_METHOD, flask.request.method)
3949
if 'PATH_INFO' in env:
@@ -68,6 +78,8 @@ def request_finished_with_instana(sender, response, **extra):
6878
span.mark_as_errored()
6979

7080
span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
81+
extract_custom_headers(span, response.headers, format=False)
82+
7183
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
7284
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
7385
except:

tests/apps/flask_app/app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ def render_error():
161161

162162
@app.route("/response_headers")
163163
def response_headers():
164-
resp = Response("Foo bar baz")
165-
resp.headers['X-Capture-This'] = 'Ok'
166-
return resp
167-
164+
headers = {
165+
'X-Capture-This': 'Ok',
166+
'X-Capture-That': 'Ok too'
167+
}
168+
return Response("Stan wuz here with headers!", headers=headers)
168169

169170
@app.route("/boto3/sqs")
170171
def boto3_sqs():

0 commit comments

Comments
 (0)