@@ -108,63 +108,66 @@ def trace_tween_factory(handler, registry):
108108 settings = registry .settings
109109 enabled = asbool (settings .get (SETTING_TRACE_ENABLED , True ))
110110
111- if enabled :
112- # make a request tracing function
113- def trace_tween (request ):
114- if disable_trace (request .url , _excluded_hosts , _excluded_paths ):
115- request .environ [_ENVIRON_ENABLED_KEY ] = False
116- # short-circuit when we don't want to trace anything
117- return handler (request )
118-
119- request .environ [_ENVIRON_ENABLED_KEY ] = True
120- request .environ [_ENVIRON_STARTTIME_KEY ] = time_ns ()
121-
122- response = None
123- try :
124- response = handler (request )
125- except HTTPException as exc :
126- # If the exception is a pyramid HTTPException,
127- # that's still valuable information that isn't necessarily
128- # a 500. For instance, HTTPFound is a 302.
129- # As described in docs, Pyramid exceptions are all valid
130- # response types
131- response = exc
132- raise
133- finally :
134- span = request .environ .get (_ENVIRON_SPAN_KEY )
135- enabled = request .environ .get (_ENVIRON_ENABLED_KEY )
136- if not span and enabled :
137- _logger .warning (
138- "Pyramid environ's OpenTelemetry span missing."
139- "If the OpenTelemetry tween was added manually, make sure"
140- "PyramidInstrumentor().instrument_config(config) is called"
111+ if not enabled :
112+ # If disabled, make a tween that signals to the
113+ # BeforeTraversal subscriber that tracing is disabled
114+ def disabled_tween (request ):
115+ request .environ [_ENVIRON_ENABLED_KEY ] = False
116+ return handler (request )
117+
118+ return disabled_tween
119+
120+ # make a request tracing function
121+ def trace_tween (request ):
122+ if disable_trace (request .url , _excluded_hosts , _excluded_paths ):
123+ request .environ [_ENVIRON_ENABLED_KEY ] = False
124+ # short-circuit when we don't want to trace anything
125+ return handler (request )
126+
127+ request .environ [_ENVIRON_ENABLED_KEY ] = True
128+ request .environ [_ENVIRON_STARTTIME_KEY ] = time_ns ()
129+
130+ try :
131+ response = handler (request )
132+ response_or_exception = response
133+ except HTTPException as exc :
134+ # If the exception is a pyramid HTTPException,
135+ # that's still valuable information that isn't necessarily
136+ # a 500. For instance, HTTPFound is a 302.
137+ # As described in docs, Pyramid exceptions are all valid
138+ # response types
139+ response_or_exception = exc
140+ raise
141+ finally :
142+ span = request .environ .get (_ENVIRON_SPAN_KEY )
143+ enabled = request .environ .get (_ENVIRON_ENABLED_KEY )
144+ if not span and enabled :
145+ _logger .warning (
146+ "Pyramid environ's OpenTelemetry span missing."
147+ "If the OpenTelemetry tween was added manually, make sure"
148+ "PyramidInstrumentor().instrument_config(config) is called"
149+ )
150+ elif enabled :
151+ if response_or_exception :
152+ otel_wsgi .add_response_attributes (
153+ span ,
154+ response_or_exception .status ,
155+ response_or_exception .headers ,
156+ )
157+
158+ activation = request .environ .get (_ENVIRON_ACTIVATION_KEY )
159+
160+ if isinstance (response_or_exception , HTTPException ):
161+ activation .__exit__ (
162+ type (response_or_exception ),
163+ response_or_exception ,
164+ getattr (response_or_exception , "__traceback__" , None ),
141165 )
142- elif enabled :
143- if response :
144- otel_wsgi .add_response_attributes (
145- span , response .status , response .headers
146- )
147-
148- activation = request .environ .get (_ENVIRON_ACTIVATION_KEY )
149-
150- if isinstance (response , HTTPException ):
151- activation .__exit__ (
152- type (response ),
153- response ,
154- getattr (response , "__traceback__" , None ),
155- )
156- else :
157- activation .__exit__ (None , None , None )
158-
159- context .detach (request .environ .get (_ENVIRON_TOKEN ))
160- return response
161-
162- return trace_tween
163-
164- # If not enabled, make a tween that signals to the
165- # BeforeTraversal subscriber that tracing is disabled
166- def disabled_tween (request ):
167- request .environ [_ENVIRON_ENABLED_KEY ] = False
168- return handler (request )
169-
170- return disabled_tween
166+ else :
167+ activation .__exit__ (None , None , None )
168+
169+ context .detach (request .environ .get (_ENVIRON_TOKEN ))
170+
171+ return response
172+
173+ return trace_tween
0 commit comments