Skip to content

Commit 0170bd4

Browse files
authored
Merge pull request #491 from instana/flask-header-capture
Flask: capture responseHeadersOnEntrySpans
2 parents c6d2c91 + b22ac1d commit 0170bd4

File tree

5 files changed

+178
-88
lines changed

5 files changed

+178
-88
lines changed

instana/instrumentation/flask/common.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import opentracing.ext.tags as ext
1010

1111
from ...log import logger
12-
from ...singletons import tracer
12+
from ...singletons import tracer, agent
1313

1414

1515
@wrapt.patch_function_wrapper('flask', 'templating._render')
@@ -77,3 +77,17 @@ def handle_user_exception_with_instana(wrapped, instance, argv, kwargs):
7777
logger.debug("handle_user_exception_with_instana:", exc_info=True)
7878

7979
return response
80+
81+
82+
def extract_custom_headers(span, headers, format):
83+
if agent.options.extra_http_headers is None:
84+
return
85+
try:
86+
for custom_header in agent.options.extra_http_headers:
87+
# Headers are available in this format: HTTP_X_CAPTURE_THIS
88+
flask_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header
89+
if flask_header in headers:
90+
span.set_tag("http.header.%s" % custom_header, headers[flask_header])
91+
92+
except Exception:
93+
logger.debug("extract_custom_headers: ", exc_info=True)

instana/instrumentation/flask/vanilla.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ...log import logger
1414
from ...singletons import agent, tracer
1515
from ...util.secrets import strip_secrets_from_query
16+
from .common import extract_custom_headers
1617

1718
path_tpl_re = re.compile('<.*>')
1819

@@ -25,12 +26,7 @@ def before_request_with_instana(*argv, **kwargs):
2526
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
2627
span = flask.g.scope.span
2728

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])
29+
extract_custom_headers(span, env, format=True)
3430

3531
span.set_tag(ext.HTTP_METHOD, flask.request.method)
3632
if 'PATH_INFO' in env:
@@ -68,6 +64,8 @@ def after_request_with_instana(response):
6864
span.mark_as_errored()
6965

7066
span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
67+
extract_custom_headers(span, response.headers, format=False)
68+
7169
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
7270
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
7371
except:

instana/instrumentation/flask/with_blinker.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ...log import logger
1212
from ...util.secrets import strip_secrets_from_query
1313
from ...singletons import agent, tracer
14+
from .common import extract_custom_headers
1415

1516
import flask
1617
from flask import request_started, request_finished, got_request_exception
@@ -28,12 +29,7 @@ def request_started_with_instana(sender, **extra):
2829
flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx)
2930
span = flask.g.scope.span
3031

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])
32+
extract_custom_headers(span, env, format=True)
3733

3834
span.set_tag(ext.HTTP_METHOD, flask.request.method)
3935
if 'PATH_INFO' in env:
@@ -68,6 +64,8 @@ def request_finished_with_instana(sender, response, **extra):
6864
span.mark_as_errored()
6965

7066
span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code))
67+
extract_custom_headers(span, response.headers, format=False)
68+
7169
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
7270
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
7371
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)