diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f64627a70..7c73f3ca31 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ on: - 'release/*' pull_request: env: - CORE_REPO_SHA: 5de7ffd7cbb555fb04d0138361a188496557080d + CORE_REPO_SHA: ce6449accf315977dd1eab940f7f49b7d894618f jobs: build: 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 1f465ca57a..e4277323e7 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -28,7 +28,6 @@ ) from opentelemetry.propagators import extract from opentelemetry.trace import SpanKind, get_tracer -from opentelemetry.util import ExcludeList try: from django.core.urlresolvers import ( # pylint: disable=no-name-in-module @@ -62,18 +61,9 @@ class _DjangoMiddleware(MiddlewareMixin): _environ_span_key = "opentelemetry-instrumentor-django.span_key" _environ_exception_key = "opentelemetry-instrumentor-django.exception_key" - _excluded_urls = Configuration().DJANGO_EXCLUDED_URLS or [] - if _excluded_urls: - _excluded_urls = ExcludeList(str.split(_excluded_urls, ",")) - else: - _excluded_urls = ExcludeList(_excluded_urls) + _excluded_urls = Configuration()._excluded_urls("django") - _traced_request_attrs = [ - attr.strip() - for attr in (Configuration().DJANGO_TRACED_REQUEST_ATTRS or "").split( - "," - ) - ] + _traced_request_attrs = Configuration()._traced_request_attrs("django") @staticmethod def _get_span_name(request): diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 3f70f62bec..9bd23ee529 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -28,7 +28,6 @@ from opentelemetry.test.wsgitestutil import WsgiTestBase from opentelemetry.trace import SpanKind from opentelemetry.trace.status import StatusCode -from opentelemetry.util import ExcludeList # pylint: disable=import-error from .views import ( @@ -66,9 +65,30 @@ def setUp(self): setup_test_environment() _django_instrumentor.instrument() Configuration._reset() # pylint: disable=protected-access + self.env_patch = patch.dict( + "os.environ", + { + "OTEL_PYTHON_DJANGO_EXCLUDED_URLS": "http://testserver/excluded_arg/123,excluded_noarg", + "OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS": "path_info,content_type,non_existing_variable", + }, + ) + self.env_patch.start() + self.exclude_patch = patch( + "opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls", + Configuration()._excluded_urls("django"), + ) + self.traced_patch = patch( + "opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs", + Configuration()._traced_request_attrs("django"), + ) + self.exclude_patch.start() + self.traced_patch.start() def tearDown(self): super().tearDown() + self.env_patch.stop() + self.exclude_patch.stop() + self.traced_patch.stop() teardown_test_environment() _django_instrumentor.uninstrument() @@ -227,10 +247,6 @@ def test_error(self): self.assertEqual(view_data.labels, key) self.assertEqual(view_data.aggregator.current.count, 1) - @patch( - "opentelemetry.instrumentation.django.middleware._DjangoMiddleware._excluded_urls", - ExcludeList(["http://testserver/excluded_arg/123", "excluded_noarg"]), - ) def test_exclude_lists(self): client = Client() client.get("/excluded_arg/123") @@ -288,28 +304,11 @@ def test_span_name_404(self): self.assertEqual(span.name, "HTTP GET") def test_traced_request_attrs(self): - with patch( - "opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs", - [], - ): - Client().get("/span_name/1234/", CONTENT_TYPE="test/ct") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - - span = span_list[0] - self.assertNotIn("path_info", span.attributes) - self.assertNotIn("content_type", span.attributes) - self.memory_exporter.clear() - - with patch( - "opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs", - ["path_info", "content_type", "non_existing_variable"], - ): - Client().get("/span_name/1234/", CONTENT_TYPE="test/ct") - span_list = self.memory_exporter.get_finished_spans() - self.assertEqual(len(span_list), 1) - - span = span_list[0] - self.assertEqual(span.attributes["path_info"], "/span_name/1234/") - self.assertEqual(span.attributes["content_type"], "test/ct") - self.assertNotIn("non_existing_variable", span.attributes) + Client().get("/span_name/1234/", CONTENT_TYPE="test/ct") + span_list = self.memory_exporter.get_finished_spans() + self.assertEqual(len(span_list), 1) + + span = span_list[0] + self.assertEqual(span.attributes["path_info"], "/span_name/1234/") + self.assertEqual(span.attributes["content_type"], "test/ct") + self.assertNotIn("non_existing_variable", span.attributes) 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 55f8e98dcb..e9b1f16800 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py @@ -58,7 +58,7 @@ def on_get(self, req, resp): http_status_to_status_code, ) from opentelemetry.trace.status import Status -from opentelemetry.util import ExcludeList, time_ns +from opentelemetry.util import time_ns _logger = getLogger(__name__) @@ -68,15 +68,8 @@ def on_get(self, req, resp): _ENVIRON_TOKEN = "opentelemetry-falcon.token" _ENVIRON_EXC = "opentelemetry-falcon.exc" - -def get_excluded_urls(): - urls = configuration.Configuration().FALCON_EXCLUDED_URLS or "" - if urls: - urls = str.split(urls, ",") - return ExcludeList(urls) - - -_excluded_urls = get_excluded_urls() +cfg = configuration.Configuration() +_excluded_urls = cfg._excluded_urls("falcon") class FalconInstrumentor(BaseInstrumentor): @@ -156,12 +149,7 @@ class _TraceMiddleware: def __init__(self, tracer=None, traced_request_attrs=None): self.tracer = tracer - self._traced_request_attrs = traced_request_attrs or [ - attr.strip() - for attr in ( - Configuration().FALCON_TRACED_REQUEST_ATTRS or "" - ).split(",") - ] + self._traced_request_attrs = cfg._traced_request_attrs("falcon") def process_request(self, req, resp): span = req.env.get(_ENVIRON_SPAN_KEY) diff --git a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py index fe33a2f2dd..464f0d8f5c 100644 --- a/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py +++ b/instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py @@ -16,10 +16,10 @@ from falcon import testing +from opentelemetry.configuration import Configuration from opentelemetry.instrumentation.falcon import FalconInstrumentor from opentelemetry.test.test_base import TestBase from opentelemetry.trace.status import StatusCode -from opentelemetry.util import ExcludeList from .app import make_app @@ -29,6 +29,30 @@ def setUp(self): super().setUp() FalconInstrumentor().instrument() self.app = make_app() + # pylint: disable=protected-access + Configuration()._reset() + self.env_patch = patch.dict( + "os.environ", + { + "OTEL_PYTHON_FALCON_EXCLUDED_URLS": "ping", + "OTEL_PYTHON_FALCON_TRACED_REQUEST_ATTRS": "query_string", + }, + ) + self.env_patch.start() + self.exclude_patch = patch( + "opentelemetry.instrumentation.falcon._excluded_urls", + Configuration()._excluded_urls("falcon"), + ) + middleware = self.app._middleware[0][ # pylint:disable=W0212 + 0 + ].__self__ + self.traced_patch = patch.object( + middleware, + "_traced_request_attrs", + Configuration()._traced_request_attrs("falcon"), + ) + self.exclude_patch.start() + self.traced_patch.start() def client(self): return testing.TestClient(self.app) @@ -37,6 +61,9 @@ def tearDown(self): super().tearDown() with self.disable_logging(): FalconInstrumentor().uninstrument() + self.env_patch.stop() + self.exclude_patch.stop() + self.traced_patch.stop() def test_get(self): self._test_method("GET") @@ -155,10 +182,6 @@ def test_uninstrument(self): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) - @patch( - "opentelemetry.instrumentation.falcon._excluded_urls", - ExcludeList(["ping"]), - ) def test_exclude_lists(self): self.client().simulate_get(path="/ping") span_list = self.memory_exporter.get_finished_spans() @@ -171,19 +194,9 @@ def test_exclude_lists(self): def test_traced_request_attributes(self): self.client().simulate_get(path="/hello?q=abc") span = self.memory_exporter.get_finished_spans()[0] - self.assertNotIn("query_string", span.attributes) - self.memory_exporter.clear() - - middleware = self.app._middleware[0][ # pylint:disable=W0212 - 0 - ].__self__ - with patch.object( - middleware, "_traced_request_attrs", ["query_string"] - ): - self.client().simulate_get(path="/hello?q=abc") - span = self.memory_exporter.get_finished_spans()[0] - self.assertIn("query_string", span.attributes) - self.assertEqual(span.attributes["query_string"], "q=abc") + self.assertIn("query_string", span.attributes) + self.assertEqual(span.attributes["query_string"], "q=abc") + self.assertNotIn("not_available_attr", span.attributes) def test_traced_not_recording(self): mock_tracer = Mock() 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 bfc1b3d798..d4758fed58 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -55,7 +55,7 @@ def hello(): from opentelemetry import configuration, context, propagators, trace from opentelemetry.instrumentation.flask.version import __version__ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor -from opentelemetry.util import ExcludeList, time_ns +from opentelemetry.util import time_ns _logger = getLogger(__name__) @@ -65,14 +65,7 @@ def hello(): _ENVIRON_TOKEN = "opentelemetry-flask.token" -def get_excluded_urls(): - urls = configuration.Configuration().FLASK_EXCLUDED_URLS or [] - if urls: - urls = str.split(urls, ",") - return ExcludeList(urls) - - -_excluded_urls = get_excluded_urls() +_excluded_urls = configuration.Configuration()._excluded_urls("flask") def get_default_span_name(): diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py index 0bed5d20d8..57aab89058 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py @@ -17,10 +17,10 @@ from flask import Flask, request from opentelemetry import trace +from opentelemetry.configuration import Configuration from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.test.test_base import TestBase from opentelemetry.test.wsgitestutil import WsgiTestBase -from opentelemetry.util import ExcludeList # pylint: disable=import-error from .base_test import InstrumentationTest @@ -54,8 +54,23 @@ def setUp(self): self._common_initialization() + self.env_patch = patch.dict( + "os.environ", + { + "OTEL_PYTHON_FLASK_EXCLUDED_URLS": "http://localhost/excluded_arg/123,excluded_noarg" + }, + ) + self.env_patch.start() + self.exclude_patch = patch( + "opentelemetry.instrumentation.flask._excluded_urls", + Configuration()._excluded_urls("flask"), + ) + self.exclude_patch.start() + def tearDown(self): super().tearDown() + self.env_patch.stop() + self.exclude_patch.stop() with self.disable_logging(): FlaskInstrumentor().uninstrument_app(self.app) @@ -158,10 +173,6 @@ def test_internal_error(self): self.assertEqual(span_list[0].kind, trace.SpanKind.SERVER) self.assertEqual(span_list[0].attributes, expected_attrs) - @patch( - "opentelemetry.instrumentation.flask._excluded_urls", - ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]), - ) def test_exclude_lists(self): self.client.get("/excluded_arg/123") span_list = self.memory_exporter.get_finished_spans() 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 e7110bd2b5..40894e2c62 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py @@ -8,7 +8,7 @@ import opentelemetry.instrumentation.wsgi as otel_wsgi from opentelemetry import configuration, context, propagators, trace from opentelemetry.instrumentation.pyramid.version import __version__ -from opentelemetry.util import ExcludeList, time_ns +from opentelemetry.util import time_ns TWEEN_NAME = "opentelemetry.instrumentation.pyramid.trace_tween_factory" SETTING_TRACE_ENABLED = "opentelemetry-pyramid.trace_enabled" @@ -22,14 +22,7 @@ _logger = getLogger(__name__) -def get_excluded_urls(): - urls = configuration.Configuration().PYRAMID_EXCLUDED_URLS or [] - if urls: - urls = str.split(urls, ",") - return ExcludeList(urls) - - -_excluded_urls = get_excluded_urls() +_excluded_urls = configuration.Configuration()._excluded_urls("pyramid") def includeme(config): diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py index 77427b0db7..f5c8b3ba02 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_programmatic.py @@ -17,10 +17,10 @@ from pyramid.config import Configurator from opentelemetry import trace +from opentelemetry.configuration import Configuration from opentelemetry.instrumentation.pyramid import PyramidInstrumentor from opentelemetry.test.test_base import TestBase from opentelemetry.test.wsgitestutil import WsgiTestBase -from opentelemetry.util import ExcludeList # pylint: disable=import-error from .pyramid_base_test import InstrumentationTest @@ -54,6 +54,19 @@ def setUp(self): self._common_initialization(self.config) + self.env_patch = patch.dict( + "os.environ", + { + "OTEL_PYTHON_PYRAMID_EXCLUDED_URLS": "http://localhost/excluded_arg/123,excluded_noarg" + }, + ) + self.env_patch.start() + self.exclude_patch = patch( + "opentelemetry.instrumentation.pyramid.callbacks._excluded_urls", + Configuration()._excluded_urls("pyramid"), + ) + self.exclude_patch.start() + def tearDown(self): super().tearDown() with self.disable_logging(): @@ -187,10 +200,6 @@ def test_warnings(self, mock_logger): self.assertEqual(len(span_list), 0) self.assertEqual(mock_logger.warning.called, True) - @patch( - "opentelemetry.instrumentation.pyramid.callbacks._excluded_urls", - ExcludeList(["http://localhost/excluded_arg/123", "excluded_noarg"]), - ) def test_exclude_lists(self): self.client.get("/excluded_arg/123") span_list = self.memory_exporter.get_finished_spans() 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 6bb956ecb5..d825fbb71b 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py @@ -56,7 +56,7 @@ def get(self): ) from opentelemetry.trace.propagation.textmap import DictGetter from opentelemetry.trace.status import Status -from opentelemetry.util import ExcludeList, time_ns +from opentelemetry.util import time_ns from .client import fetch_async # pylint: disable=E0401 @@ -65,25 +65,9 @@ def get(self): _HANDLER_CONTEXT_KEY = "_otel_trace_context_key" _OTEL_PATCHED_KEY = "_otel_patched_key" - -def get_excluded_urls(): - urls = configuration.Configuration().TORNADO_EXCLUDED_URLS or "" - if urls: - urls = str.split(urls, ",") - return ExcludeList(urls) - - -def get_traced_request_attrs(): - attrs = configuration.Configuration().TORNADO_TRACED_REQUEST_ATTRS or "" - if attrs: - attrs = [attr.strip() for attr in attrs.split(",")] - else: - attrs = [] - return attrs - - -_excluded_urls = get_excluded_urls() -_traced_attrs = get_traced_request_attrs() +cfg = configuration.Configuration() +_excluded_urls = cfg._excluded_urls("tornado") +_traced_attrs = cfg._traced_request_attrs("tornado") carrier_getter = DictGetter() diff --git a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py index 5b429766ec..21002c83cd 100644 --- a/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py @@ -18,6 +18,7 @@ from tornado.testing import AsyncHTTPTestCase from opentelemetry import trace +from opentelemetry.configuration import Configuration from opentelemetry.instrumentation.tornado import ( TornadoInstrumentor, patch_handler_class, @@ -25,7 +26,6 @@ ) from opentelemetry.test.test_base import TestBase from opentelemetry.trace import SpanKind -from opentelemetry.util import ExcludeList from .tornado_test_app import ( AsyncHandler, @@ -44,9 +44,32 @@ def get_app(self): def setUp(self): TornadoInstrumentor().instrument() super().setUp() + # pylint: disable=protected-access + Configuration()._reset() + self.env_patch = patch.dict( + "os.environ", + { + "OTEL_PYTHON_TORNADO_EXCLUDED_URLS": "healthz,ping", + "OTEL_PYTHON_TORNADO_TRACED_REQUEST_ATTRS": "uri,full_url,query", + }, + ) + self.env_patch.start() + self.exclude_patch = patch( + "opentelemetry.instrumentation.tornado._excluded_urls", + Configuration()._excluded_urls("tornado"), + ) + self.traced_patch = patch( + "opentelemetry.instrumentation.tornado._traced_attrs", + Configuration()._traced_request_attrs("tornado"), + ) + self.exclude_patch.start() + self.traced_patch.start() def tearDown(self): TornadoInstrumentor().uninstrument() + self.env_patch.stop() + self.exclude_patch.stop() + self.traced_patch.stop() super().tearDown() @@ -326,10 +349,6 @@ def test_dynamic_handler(self): }, ) - @patch( - "opentelemetry.instrumentation.tornado._excluded_urls", - ExcludeList(["healthz", "ping"]), - ) def test_exclude_lists(self): def test_excluded(path): self.fetch(path) @@ -354,18 +373,14 @@ def test_excluded(path): test_excluded("/healthz") test_excluded("/ping") - @patch( - "opentelemetry.instrumentation.tornado._traced_attrs", - ["uri", "full_url", "query"], - ) def test_traced_attrs(self): - self.fetch("/ping?q=abc&b=123") + self.fetch("/pong?q=abc&b=123") spans = self.sorted_spans(self.memory_exporter.get_finished_spans()) self.assertEqual(len(spans), 2) server_span = spans[0] self.assertEqual(server_span.kind, SpanKind.SERVER) self.assert_span_has_attributes( - server_span, {"uri": "/ping?q=abc&b=123", "query": "q=abc&b=123"} + server_span, {"uri": "/pong?q=abc&b=123", "query": "q=abc&b=123"} ) self.memory_exporter.clear()