Skip to content

Commit de1d1b7

Browse files
committed
Fix issue with Flask instrumentation when a request spawn children threads and copie the request context
1 parent 7af87e1 commit de1d1b7

File tree

1 file changed

+25
-29
lines changed
  • instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask

1 file changed

+25
-29
lines changed

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

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,38 @@
3838
3939
FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={})
4040
41-
For example, FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor,
42-
invoking ``cursor.execute("select * from auth_users")`` will lead to sql query
43-
``select * from auth_users`` but when SQLCommenter is enabled the query will get appended with
44-
some configurable tags like:
4541
46-
.. code::
42+
For example,
43+
::
4744
48-
select * from auth_users /*metrics=value*/;"
45+
FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor, invoking cursor.execute("select * from auth_users")
46+
will lead to sql query "select * from auth_users" but when SQLCommenter is enabled
47+
the query will get appended with some configurable tags like "select * from auth_users /*metrics=value*/;"
4948
50-
Inorder for the commenter to append flask related tags to sql queries, the commenter needs
51-
to enabled on the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too.
49+
Inorder for the commenter to append flask related tags to sql queries, the commenter needs to enabled on
50+
the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too.
5251
5352
SQLCommenter Configurations
5453
***************************
55-
We can configure the tags to be appended to the sqlquery log by adding configuration
56-
inside ``commenter_options={}`` dict.
57-
58-
For example, enabling this flag will add flask and it's version which
59-
is ``/*flask%%3A2.9.3*/`` to the SQL query as a comment (default is True):
60-
61-
.. code:: python
62-
63-
framework = True
54+
We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword
6455
65-
For example, enabling this flag will add route uri ``/*route='/home'*/``
66-
to the SQL query as a comment (default is True):
56+
framework = True(Default) or False
6757
68-
.. code:: python
58+
For example,
59+
::
60+
Enabling this flag will add flask and it's version which is /*flask%%3A2.9.3*/
6961
70-
route = True
62+
route = True(Default) or False
7163
72-
For example, enabling this flag will add controller name ``/*controller='home_view'*/``
73-
to the SQL query as a comment (default is True):
64+
For example,
65+
::
66+
Enabling this flag will add route uri /*route='/home'*/
7467
75-
.. code:: python
68+
controller = True(Default) or False
7669
77-
controller = True
70+
For example,
71+
::
72+
Enabling this flag will add controller name /*controller='home_view'*/
7873
7974
Usage
8075
-----
@@ -238,10 +233,10 @@ def response_hook(span: Span, status: str, response_headers: List):
238233
API
239234
---
240235
"""
241-
242236
from logging import getLogger
243237
from time import time_ns
244238
from timeit import default_timer
239+
from threading import get_ident
245240
from typing import Collection
246241

247242
import flask
@@ -397,7 +392,7 @@ def _before_request():
397392

398393
activation = trace.use_span(span, end_on_exit=True)
399394
activation.__enter__() # pylint: disable=E1101
400-
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation
395+
flask_request_environ[_ENVIRON_ACTIVATION_KEY] = (get_ident(), activation)
401396
flask_request_environ[_ENVIRON_SPAN_KEY] = span
402397
flask_request_environ[_ENVIRON_TOKEN] = token
403398

@@ -436,8 +431,8 @@ def _teardown_request(exc):
436431
if excluded_urls and excluded_urls.url_disabled(flask.request.url):
437432
return
438433

439-
activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
440-
if not activation:
434+
thread_id, activation = flask.request.environ.get(_ENVIRON_ACTIVATION_KEY)
435+
if not activation or thread_id != get_ident():
441436
# This request didn't start a span, maybe because it was created in
442437
# a way that doesn't run `before_request`, like when it is created
443438
# with `app.test_request_context`.
@@ -456,6 +451,7 @@ def _teardown_request(exc):
456451

457452

458453
class _InstrumentedFlask(flask.Flask):
454+
459455
_excluded_urls = None
460456
_tracer_provider = None
461457
_request_hook = None

0 commit comments

Comments
 (0)