1010from opentelemetry .semconv .trace import SpanAttributes
1111
1212from instana .log import logger
13- from instana .singletons import tracer
13+ from instana .singletons import get_tracer
1414from instana .propagators .format import Format
1515
1616
1919 from flask .typing import ResponseReturnValue
2020 from jinja2 .environment import Template
2121
22- @wrapt .patch_function_wrapper ('flask' , 'templating._render' )
22+
23+ @wrapt .patch_function_wrapper ("flask" , "templating._render" )
2324def render_with_instana (
2425 wrapped : Callable [..., str ],
2526 instance : object ,
@@ -32,6 +33,7 @@ def render_with_instana(
3233
3334 parent_span = flask .g .span
3435 parent_context = parent_span .get_span_context ()
36+ tracer = get_tracer ()
3537
3638 with tracer .start_as_current_span ("render" , span_context = parent_context ) as span :
3739 try :
@@ -50,7 +52,7 @@ def render_with_instana(
5052 raise
5153
5254
53- @wrapt .patch_function_wrapper (' flask' , ' Flask.handle_user_exception' )
55+ @wrapt .patch_function_wrapper (" flask" , " Flask.handle_user_exception" )
5456def handle_user_exception_with_instana (
5557 wrapped : Callable [..., Union ["HTTPException" , "ResponseReturnValue" ]],
5658 instance : flask .app .Flask ,
@@ -70,7 +72,7 @@ def handle_user_exception_with_instana(
7072 if isinstance (response , tuple ):
7173 status_code = response [1 ]
7274 else :
73- if hasattr (response , ' code' ):
75+ if hasattr (response , " code" ):
7476 status_code = response .code
7577 else :
7678 status_code = response .status_code
@@ -80,12 +82,13 @@ def handle_user_exception_with_instana(
8082
8183 span .set_attribute (SpanAttributes .HTTP_STATUS_CODE , int (status_code ))
8284
83- if hasattr (response , 'headers' ):
85+ if hasattr (response , "headers" ):
86+ tracer = get_tracer ()
8487 tracer .inject (span .context , Format .HTTP_HEADERS , response .headers )
8588 if span and span .is_recording ():
8689 span .end ()
8790 flask .g .span = None
88- except :
91+ except Exception :
8992 logger .debug ("handle_user_exception_with_instana:" , exc_info = True )
9093
9194 return response
0 commit comments