Skip to content

Commit

Permalink
use reqctx instead?
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewgrossman committed Feb 24, 2023
1 parent d11ff88 commit fb9161d
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def response_hook(span: Span, status: str, response_headers: List):
_ENVIRON_STARTTIME_KEY = "opentelemetry-flask.starttime_key"
_ENVIRON_SPAN_KEY = "opentelemetry-flask.span_key"
_ENVIRON_ACTIVATION_KEY = "opentelemetry-flask.activation_key"
_ENVIRON_REQUEST_ID_KEY = "opentelemetry-flask.request_id_key"
_ENVIRON_REQCTX_ID_KEY = "opentelemetry-flask.reqctx_id_key"
_ENVIRON_TOKEN = "opentelemetry-flask.token"

_excluded_urls_from_env = get_excluded_urls("FLASK")
Expand Down Expand Up @@ -398,7 +398,7 @@ def _before_request():
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__() # pylint: disable=E1101
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
flask_request_environ[_ENVIRON_REQUEST_ID_KEY] = id(flask.request)
flask_request_environ[_ENVIRON_REQCTX_ID_KEY] = id(flask.request_ctx)
flask_request_environ[_ENVIRON_SPAN_KEY] = span
flask_request_environ[_ENVIRON_TOKEN] = token

Expand Down Expand Up @@ -438,13 +438,19 @@ def _teardown_request(exc):
return

activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
request_id = flask.request.environ.get(_ENVIRON_REQUEST_ID_KEY)
if not activation or request_id != id(flask.request):

original_reqctx_id = flask.request.environ.get(_ENVIRON_REQCTX_ID_KEY)
current_reqctx_id = id(flask.request_ctx)
if not activation or original_reqctx_id != current_reqctx_id:
# This request didn't start a span, maybe because it was created in
# a way that doesn't run `before_request`, like when it is created
# with `app.test_request_context`.
#
# Similarly, check the thread_id against the current thread to ensure
# Similarly, check that the id(request) matches the current id(request)
# to ensure tear down only happens if they match. This situation can arise
# TODO
#
# Similarly, check the id(request) against the current thread to ensure
# tear down only happens on the original thread. This situation can
# arise if the original thread handling the request spawn children
# threads and then uses something like copy_current_request_context
Expand Down

0 comments on commit fb9161d

Please sign in to comment.