38
38
39
39
FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={})
40
40
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:
45
41
46
- .. code::
42
+ For example,
43
+ ::
47
44
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*/;"
49
48
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.
52
51
53
52
SQLCommenter Configurations
54
53
***************************
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
64
55
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
67
57
68
- .. code:: python
58
+ For example,
59
+ ::
60
+ Enabling this flag will add flask and it's version which is /*flask%%3A2.9.3*/
69
61
70
- route = True
62
+ route = True(Default) or False
71
63
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'*/
74
67
75
- .. code:: python
68
+ controller = True(Default) or False
76
69
77
- controller = True
70
+ For example,
71
+ ::
72
+ Enabling this flag will add controller name /*controller='home_view'*/
78
73
79
74
Usage
80
75
-----
@@ -238,10 +233,10 @@ def response_hook(span: Span, status: str, response_headers: List):
238
233
API
239
234
---
240
235
"""
241
-
242
236
from logging import getLogger
243
237
from time import time_ns
244
238
from timeit import default_timer
239
+ from threading import get_ident
245
240
from typing import Collection
246
241
247
242
import flask
@@ -397,7 +392,7 @@ def _before_request():
397
392
398
393
activation = trace .use_span (span , end_on_exit = True )
399
394
activation .__enter__ () # pylint: disable=E1101
400
- flask_request_environ [_ENVIRON_ACTIVATION_KEY ] = activation
395
+ flask_request_environ [_ENVIRON_ACTIVATION_KEY ] = ( get_ident (), activation )
401
396
flask_request_environ [_ENVIRON_SPAN_KEY ] = span
402
397
flask_request_environ [_ENVIRON_TOKEN ] = token
403
398
@@ -436,8 +431,8 @@ def _teardown_request(exc):
436
431
if excluded_urls and excluded_urls .url_disabled (flask .request .url ):
437
432
return
438
433
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 () :
441
436
# This request didn't start a span, maybe because it was created in
442
437
# a way that doesn't run `before_request`, like when it is created
443
438
# with `app.test_request_context`.
@@ -456,6 +451,7 @@ def _teardown_request(exc):
456
451
457
452
458
453
class _InstrumentedFlask (flask .Flask ):
454
+
459
455
_excluded_urls = None
460
456
_tracer_provider = None
461
457
_request_hook = None
0 commit comments