diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 6f6b16fe29..0f0eed11a4 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from time import time from logging import getLogger from os import environ -from re import compile as re_compile, search +from re import compile as re_compile +from re import search +from time import time from django.conf import settings @@ -63,6 +64,7 @@ def __init__(self, excluded_urls): def url_disabled(self, url: str) -> bool: return bool(self._excluded_urls and search(self._regex, url)) + _root = r"OTEL_PYTHON_{}" @@ -85,8 +87,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) @@ -153,21 +154,21 @@ def process_request(self, request): # pylint:disable=W0212 request._otel_start_time = time() - environ = request.META + request_meta = request.META - token = attach(extract(carrier_getter, environ)) + token = attach(extract(carrier_getter, request_meta)) tracer = get_tracer(__name__, __version__) span = tracer.start_span( self._get_span_name(request), kind=SpanKind.SERVER, - start_time=environ.get( + start_time=request_meta.get( "opentelemetry-instrumentor-django.starttime_key" ), ) - attributes = collect_request_attributes(environ) + attributes = collect_request_attributes(request_meta) # pylint:disable=W0212 request._otel_labels = self._get_metric_labels_from_attributes( attributes diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 3e634620f3..fe3f25255c 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -23,7 +23,8 @@ from opentelemetry.instrumentation.django import DjangoInstrumentor from opentelemetry.instrumentation.django.middleware import ( - _get_excluded_urls, _get_traced_request_attrs + _get_excluded_urls, + _get_traced_request_attrs, ) from opentelemetry.sdk.util import get_dict_as_key from opentelemetry.test.test_base import TestBase diff --git a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py index 9d88b9f1e8..a3ffb0d5fa 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py @@ -43,10 +43,11 @@ def on_get(self, req, resp): --- """ -from sys import exc_info from logging import getLogger -from re import compile as re_compile, search from os import environ +from re import compile as re_compile +from re import search +from sys import exc_info import falcon @@ -81,6 +82,7 @@ def __init__(self, excluded_urls): def url_disabled(self, url: str) -> bool: return bool(self._excluded_urls and search(self._regex, url)) + _root = r"OTEL_PYTHON_{}" @@ -103,8 +105,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) diff --git a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py index 73baf83415..0b4f52fda2 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py @@ -17,7 +17,9 @@ from falcon import testing from opentelemetry.instrumentation.falcon import ( - FalconInstrumentor, _get_excluded_urls, _get_traced_request_attrs + FalconInstrumentor, + _get_excluded_urls, + _get_traced_request_attrs, ) from opentelemetry.test.test_base import TestBase from opentelemetry.trace.status import StatusCode @@ -47,9 +49,7 @@ def setUp(self): 0 ].__self__ self.traced_patch = patch.object( - middleware, - "_traced_request_attrs", - _get_traced_request_attrs(), + middleware, "_traced_request_attrs", _get_traced_request_attrs(), ) self.exclude_patch.start() self.traced_patch.start() diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py index 02e9fd46d4..ad3e3eae6c 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py @@ -12,11 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -from re import compile as re_compile, search from os import environ +from re import compile as re_compile +from re import search -from starlette.routing import Match import fastapi +from starlette.routing import Match from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware from opentelemetry.instrumentation.fastapi.version import __version__ # noqa @@ -40,8 +41,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) 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 2d883e46d8..e0ced69599 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -48,8 +48,9 @@ def hello(): """ from logging import getLogger -from re import compile as re_compile, search from os import environ +from re import compile as re_compile +from re import search import flask @@ -84,8 +85,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) @@ -104,12 +104,12 @@ def get_default_span_name(): def _rewrapped_app(wsgi_app): - def _wrapped_app(environ, start_response): + def _wrapped_app(wrapped_app_environ, start_response): # We want to measure the time for route matching, etc. # In theory, we could start the span here and use # update_name later but that API is "highly discouraged" so # we better avoid it. - environ[_ENVIRON_STARTTIME_KEY] = time_ns() + wrapped_app_environ[_ENVIRON_STARTTIME_KEY] = time_ns() def _start_response(status, response_headers, *args, **kwargs): if not _excluded_urls.url_disabled(flask.request.url): @@ -128,7 +128,7 @@ def _start_response(status, response_headers, *args, **kwargs): return start_response(status, response_headers, *args, **kwargs) - return wsgi_app(environ, _start_response) + return wsgi_app(wrapped_app_environ, _start_response) return _wrapped_app @@ -138,10 +138,12 @@ def _before_request(): if _excluded_urls.url_disabled(flask.request.url): return - environ = flask.request.environ + flask_request_environ = flask.request.environ span_name = name_callback() token = context.attach( - propagators.extract(otel_wsgi.carrier_getter, environ) + propagators.extract( + otel_wsgi.carrier_getter, flask_request_environ + ) ) tracer = trace.get_tracer(__name__, __version__) @@ -149,10 +151,12 @@ def _before_request(): span = tracer.start_span( span_name, kind=trace.SpanKind.SERVER, - start_time=environ.get(_ENVIRON_STARTTIME_KEY), + start_time=flask_request_environ.get(_ENVIRON_STARTTIME_KEY), ) if span.is_recording(): - attributes = otel_wsgi.collect_request_attributes(environ) + attributes = otel_wsgi.collect_request_attributes( + flask_request_environ + ) if flask.request.url_rule: # For 404 that result from no route found, etc, we # don't have a url_rule. @@ -162,9 +166,9 @@ def _before_request(): activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() - environ[_ENVIRON_ACTIVATION_KEY] = activation - environ[_ENVIRON_SPAN_KEY] = span - environ[_ENVIRON_TOKEN] = token + flask_request_environ[_ENVIRON_ACTIVATION_KEY] = activation + flask_request_environ[_ENVIRON_SPAN_KEY] = span + flask_request_environ[_ENVIRON_TOKEN] = token return _before_request diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index c0e1e339cb..d989c66474 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -17,9 +17,6 @@ class InstrumentationTest: - def setUp(self): # pylint: disable=invalid-name - super().setUp() # pylint: disable=no-member - @staticmethod def _hello_endpoint(helloid): if helloid == 500: diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index 92e95105a3..9ea43b9997 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -18,7 +18,8 @@ from opentelemetry import trace from opentelemetry.instrumentation.flask import ( - FlaskInstrumentor, _get_excluded_urls + FlaskInstrumentor, + _get_excluded_urls, ) from opentelemetry.test.test_base import TestBase from opentelemetry.test.wsgitestutil import WsgiTestBase diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py index 495c238943..dd6a26755b 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py @@ -1,6 +1,7 @@ from logging import getLogger -from re import compile as re_compile, search from os import environ +from re import compile as re_compile +from re import search from pyramid.events import BeforeTraversal from pyramid.httpexceptions import HTTPException @@ -41,12 +42,12 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) + _excluded_urls = _get_excluded_urls() @@ -69,10 +70,10 @@ def _insert_tween(config): def _before_traversal(event): request = event.request - environ = request.environ - span_name = otel_wsgi.get_default_span_name(environ) + request_environ = request.environ + span_name = otel_wsgi.get_default_span_name(request_environ) - enabled = environ.get(_ENVIRON_ENABLED_KEY) + enabled = request_environ.get(_ENVIRON_ENABLED_KEY) if enabled is None: _logger.warning( "Opentelemetry pyramid tween 'opentelemetry.instrumentation.pyramid.trace_tween_factory'" @@ -85,24 +86,24 @@ def _before_traversal(event): # Tracing not enabled, return return - start_time = environ.get(_ENVIRON_STARTTIME_KEY) + start_time = request_environ.get(_ENVIRON_STARTTIME_KEY) token = context.attach( - propagators.extract(otel_wsgi.carrier_getter, environ) + propagators.extract(otel_wsgi.carrier_getter, request_environ) ) tracer = trace.get_tracer(__name__, __version__) if request.matched_route: span_name = request.matched_route.pattern else: - span_name = otel_wsgi.get_default_span_name(environ) + span_name = otel_wsgi.get_default_span_name(request_environ) span = tracer.start_span( span_name, kind=trace.SpanKind.SERVER, start_time=start_time, ) if span.is_recording(): - attributes = otel_wsgi.collect_request_attributes(environ) + attributes = otel_wsgi.collect_request_attributes(request_environ) if request.matched_route: attributes["http.route"] = request.matched_route.pattern for key, value in attributes.items(): @@ -110,9 +111,9 @@ def _before_traversal(event): activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() - environ[_ENVIRON_ACTIVATION_KEY] = activation - environ[_ENVIRON_SPAN_KEY] = span - environ[_ENVIRON_TOKEN] = token + request_environ[_ENVIRON_ACTIVATION_KEY] = activation + request_environ[_ENVIRON_SPAN_KEY] = span + request_environ[_ENVIRON_TOKEN] = token def trace_tween_factory(handler, registry): diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py index 71269ad5be..70ab268c23 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/pyramid_base_test.py @@ -19,9 +19,6 @@ class InstrumentationTest: - def setUp(self): # pylint: disable=invalid-name - super().setUp() # pylint: disable=no-member - @staticmethod def _hello_endpoint(request): helloid = int(request.matchdict["helloid"]) diff --git a/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py b/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py index 2782c1bad4..cff9b2d5cc 100644 --- a/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py @@ -12,8 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from re import compile as re_compile, search from os import environ +from re import compile as re_compile +from re import search from starlette import applications from starlette.routing import Match @@ -40,8 +41,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) diff --git a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py index 8525000c1b..fc8269b755 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py @@ -39,7 +39,8 @@ def get(self): from functools import partial from logging import getLogger from os import environ -from re import compile as re_compile, search +from re import compile as re_compile +from re import search import tornado.web import wrapt @@ -99,8 +100,7 @@ def _get_excluded_urls(): if excluded_urls: excluded_urls = [ - excluded_url.strip() - for excluded_url in excluded_urls.split(",") + excluded_url.strip() for excluded_url in excluded_urls.split(",") ] return _ExcludeList(excluded_urls) diff --git a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py index 5159f8ab35..11eef390ee 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py @@ -20,10 +20,10 @@ from opentelemetry import trace from opentelemetry.instrumentation.tornado import ( TornadoInstrumentor, + _get_excluded_urls, + _get_traced_request_attrs, patch_handler_class, unpatch_handler_class, - _get_traced_request_attrs, - _get_excluded_urls, ) from opentelemetry.test.test_base import TestBase from opentelemetry.trace import SpanKind