diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 922c5e0b41..7106185ad7 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -38,43 +38,38 @@ FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={}) -For example, FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor, -invoking ``cursor.execute("select * from auth_users")`` will lead to sql query -``select * from auth_users`` but when SQLCommenter is enabled the query will get appended with -some configurable tags like: -.. code:: +For example, +:: - select * from auth_users /*metrics=value*/;" + FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor, invoking cursor.execute("select * from auth_users") + will lead to sql query "select * from auth_users" but when SQLCommenter is enabled + the query will get appended with some configurable tags like "select * from auth_users /*metrics=value*/;" -Inorder for the commenter to append flask related tags to sql queries, the commenter needs -to enabled on the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too. + Inorder for the commenter to append flask related tags to sql queries, the commenter needs to enabled on + the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too. SQLCommenter Configurations *************************** -We can configure the tags to be appended to the sqlquery log by adding configuration -inside ``commenter_options={}`` dict. - -For example, enabling this flag will add flask and it's version which -is ``/*flask%%3A2.9.3*/`` to the SQL query as a comment (default is True): - -.. code:: python - - framework = True +We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword -For example, enabling this flag will add route uri ``/*route='/home'*/`` -to the SQL query as a comment (default is True): +framework = True(Default) or False -.. code:: python +For example, +:: +Enabling this flag will add flask and it's version which is /*flask%%3A2.9.3*/ - route = True +route = True(Default) or False -For example, enabling this flag will add controller name ``/*controller='home_view'*/`` -to the SQL query as a comment (default is True): +For example, +:: +Enabling this flag will add route uri /*route='/home'*/ -.. code:: python +controller = True(Default) or False - controller = True +For example, +:: +Enabling this flag will add controller name /*controller='home_view'*/ Usage ----- @@ -238,10 +233,10 @@ def response_hook(span: Span, status: str, response_headers: List): API --- """ - from logging import getLogger from time import time_ns from timeit import default_timer +from threading import get_ident from typing import Collection import flask @@ -397,7 +392,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_ACTIVATION_KEY] = (get_ident(), activation) flask_request_environ[_ENVIRON_SPAN_KEY] = span flask_request_environ[_ENVIRON_TOKEN] = token @@ -436,8 +431,8 @@ 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) + 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`. @@ -456,6 +451,7 @@ def _teardown_request(exc): class _InstrumentedFlask(flask.Flask): + _excluded_urls = None _tracer_provider = None _request_hook = None