From 80d8cdadad7c1c1daeb8c475000499dbf89a4e8f Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Thu, 9 Dec 2021 01:30:36 +0000 Subject: [PATCH] pyramid: Fix which package is the correct caller in _traced_init. --- CHANGELOG.md | 2 ++ .../opentelemetry/instrumentation/pyramid/__init__.py | 10 ++++++---- .../tests/test_automatic.py | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8967010a7..6258f7dc65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-instrumentation-wsgi` Capture custom request/response headers in span attributes ([#925])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/925) +- `opentelemetry-instrumentation-pyramid` Fixed which package is the correct caller in _traced_init. + ([#830](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/830)) ### Added diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py index bcde7eda74..58e8049f23 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py @@ -134,10 +134,12 @@ def _traced_init(wrapped, instance, args, kwargs): # to find the calling package. So if we let the original `__init__` # function call it, our wrapper will mess things up. if not kwargs.get("package", None): - # Get the package for the third frame up from this one. - # Default is `level=2` which will give us the package from `wrapt` - # instead of the desired package (the caller) - kwargs["package"] = caller_package(level=3) + # Get the package for the 2nd frame up from this one. + # Default is `level=2` one level down (in Configurator.__init__). + # We want the 3rd level from _there_. Since we are already 1 level above, + # we need the 2nd level up from here, which will give us the package from + # `wrapt` instead of the desired package (the caller) + kwargs["package"] = caller_package(level=2) wrapped(*args, **kwargs) instance.include("opentelemetry.instrumentation.pyramid.callbacks") diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py index 37b2be4c76..046a41c5bc 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py @@ -79,6 +79,10 @@ def test_tween_list(self): span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) + def test_registry_name_is_this_module(self): + config = Configurator() + self.assertEqual(config.registry.__name__, __name__.rsplit('.')[0]) + class TestWrappedWithOtherFramework( InstrumentationTest, TestBase, WsgiTestBase