From a00d2949ad38bb74bc35f0fe2e151980e7824e05 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Tue, 5 Nov 2019 10:17:55 +0100 Subject: [PATCH] Flask: Add context injection safeties --- instana/instrumentation/flask/vanilla.py | 5 ++++- instana/instrumentation/flask/with_blinker.py | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/instana/instrumentation/flask/vanilla.py b/instana/instrumentation/flask/vanilla.py index 30c887aa..6a2dee5c 100644 --- a/instana/instrumentation/flask/vanilla.py +++ b/instana/instrumentation/flask/vanilla.py @@ -112,7 +112,10 @@ def handle_user_exception_with_instana(wrapped, instance, argv, kwargs): if hasattr(response, 'headers'): tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers) - response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id) + if hasattr(response.headers, 'add'): + response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id) + elif type(response.headers) is dict or hasattr(response.headers, "__dict__"): + response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id scope.close() flask.g.scope = None diff --git a/instana/instrumentation/flask/with_blinker.py b/instana/instrumentation/flask/with_blinker.py index 1a4879f2..5d3e68a3 100644 --- a/instana/instrumentation/flask/with_blinker.py +++ b/instana/instrumentation/flask/with_blinker.py @@ -102,13 +102,17 @@ def handle_user_exception_with_instana(wrapped, instance, argv, kwargs): if hasattr(response, 'headers'): tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers) - response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id) + if hasattr(response.headers, 'add'): + response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id) + elif type(response.headers) is dict or hasattr(response.headers, "__dict__"): + response.headers['Server-Timing'] = "intid;desc=%s" % scope.span.context.trace_id scope.close() flask.g.scope = None - return response except Exception as e: logger.debug("handle_user_exception_with_instana:", exc_info=True) + finally: + return response def teardown_request_with_instana(*argv, **kwargs):