Skip to content

Commit fb9161d

Browse files
use reqctx instead?
1 parent d11ff88 commit fb9161d

File tree

1 file changed

+11
-5
lines changed
  • instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask

1 file changed

+11
-5
lines changed

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def response_hook(span: Span, status: str, response_headers: List):
264264
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
265265
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
266266
_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"
268268
_ENVIRON_TOKEN = "opentelemetry-flask.token"
269269

270270
_excluded_urls_from_env = get_excluded_urls("FLASK")
@@ -398,7 +398,7 @@ def _before_request():
398398
activation = trace.use_span(span, end_on_exit=True)
399399
activation.__enter__() # pylint: disable=E1101
400400
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)
402402
flask_request_environ[_ENVIRON_SPAN_KEY] = span
403403
flask_request_environ[_ENVIRON_TOKEN] = token
404404

@@ -438,13 +438,19 @@ def _teardown_request(exc):
438438
return
439439

440440
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:
443445
# This request didn't start a span, maybe because it was created in
444446
# a way that doesn't run `before_request`, like when it is created
445447
# with `app.test_request_context`.
446448
#
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
448454
# tear down only happens on the original thread. This situation can
449455
# arise if the original thread handling the request spawn children
450456
# threads and then uses something like copy_current_request_context

0 commit comments

Comments
 (0)