Skip to content

Commit

Permalink
Fix issue with Flask instrumentation when a request spawn children th…
Browse files Browse the repository at this point in the history
…reads and copie the request context
  • Loading branch information
hangonlyra committed Feb 9, 2023
1 parent 7af87e1 commit cd89ca9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1618](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1618))

### Fixed

- Fix Flask instrumentation to only close the span if it was created by the same thread. `ValueError: generator already executing` ([#1654](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1654))
- Fix TortoiseORM instrumentation `AttributeError: type object 'Config' has no attribute 'title'`
([#1575](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1575))
- Fix SQLAlchemy uninstrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def response_hook(span: Span, status: str, response_headers: List):
API
---
"""

from logging import getLogger
from threading import get_ident
from time import time_ns
from timeit import default_timer
from typing import Collection
Expand All @@ -265,6 +265,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_THREAD_ID_KEY = "opentelemetry-flask.thread_id_key"
_ENVIRON_TOKEN = "opentelemetry-flask.token"

_excluded_urls_from_env = get_excluded_urls("FLASK")
Expand Down Expand Up @@ -398,6 +399,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_THREAD_ID_KEY] = get_ident()
flask_request_environ[_ENVIRON_SPAN_KEY] = span
flask_request_environ[_ENVIRON_TOKEN] = token

Expand Down Expand Up @@ -436,8 +438,9 @@ def _teardown_request(exc):
if excluded_urls and excluded_urls.url_disabled(flask.request.url):
return

activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
if not activation:
thread_id, activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
thread_id = flask.request.environ.get(_ENVIRON_THREAD_ID_KEY)
if not activation or thread_id != get_ident():
# 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`.
Expand Down

0 comments on commit cd89ca9

Please sign in to comment.