Skip to content

Commit 1c07a56

Browse files
authored
Flask: Add context injection safeties (#198)
1 parent 473477d commit 1c07a56

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

instana/instrumentation/flask/vanilla.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ def handle_user_exception_with_instana(wrapped, instance, argv, kwargs):
112112

113113
if hasattr(response, 'headers'):
114114
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
115-
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
115+
if hasattr(response.headers, 'add'):
116+
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
117+
elif type(response.headers) is dict or hasattr(response.headers, "__dict__"):
118+
response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id
116119

117120
scope.close()
118121
flask.g.scope = None

instana/instrumentation/flask/with_blinker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ def handle_user_exception_with_instana(wrapped, instance, argv, kwargs):
102102

103103
if hasattr(response, 'headers'):
104104
tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers)
105-
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
105+
if hasattr(response.headers, 'add'):
106+
response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id)
107+
elif type(response.headers) is dict or hasattr(response.headers, "__dict__"):
108+
response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id
106109

107110
scope.close()
108111
flask.g.scope = None
109-
return response
110112
except Exception as e:
111113
logger.debug("handle_user_exception_with_instana:", exc_info=True)
114+
finally:
115+
return response
112116

113117

114118
def teardown_request_with_instana(*argv, **kwargs):

0 commit comments

Comments
 (0)