@@ -264,7 +264,7 @@ def response_hook(span: Span, status: str, response_headers: List):
264
264
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
265
265
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
266
266
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
267
- _ENVIRON_REQUEST_ID_KEY = "opentelemetry-flask.request_id_key "
267
+ _ENVIRON_REQCTX_ID_KEY = "opentelemetry-flask.reqctx_id_key "
268
268
_ENVIRON_TOKEN = "opentelemetry-flask.token"
269
269
270
270
_excluded_urls_from_env = get_excluded_urls ("FLASK" )
@@ -398,7 +398,7 @@ def _before_request():
398
398
activation = trace .use_span (span , end_on_exit = True )
399
399
activation .__enter__ () # pylint: disable=E1101
400
400
flask_request_environ [_ENVIRON_ACTIVATION_KEY ] = activation
401
- flask_request_environ [_ENVIRON_REQUEST_ID_KEY ] = id (flask .request )
401
+ flask_request_environ [_ENVIRON_REQCTX_ID_KEY ] = id (flask .request_ctx )
402
402
flask_request_environ [_ENVIRON_SPAN_KEY ] = span
403
403
flask_request_environ [_ENVIRON_TOKEN ] = token
404
404
@@ -438,13 +438,19 @@ def _teardown_request(exc):
438
438
return
439
439
440
440
activation = flask .request .environ .get (_ENVIRON_ACTIVATION_KEY )
441
- request_id = flask .request .environ .get (_ENVIRON_REQUEST_ID_KEY )
442
- if not activation or request_id != id (flask .request ):
441
+
442
+ original_reqctx_id = flask .request .environ .get (_ENVIRON_REQCTX_ID_KEY )
443
+ current_reqctx_id = id (flask .request_ctx )
444
+ if not activation or original_reqctx_id != current_reqctx_id :
443
445
# This request didn't start a span, maybe because it was created in
444
446
# a way that doesn't run `before_request`, like when it is created
445
447
# with `app.test_request_context`.
446
448
#
447
- # Similarly, check the thread_id against the current thread to ensure
449
+ # Similarly, check that the id(request) matches the current id(request)
450
+ # to ensure tear down only happens if they match. This situation can arise
451
+ # TODO
452
+ #
453
+ # Similarly, check the id(request) against the current thread to ensure
448
454
# tear down only happens on the original thread. This situation can
449
455
# arise if the original thread handling the request spawn children
450
456
# threads and then uses something like copy_current_request_context
0 commit comments