From 80d8cdadad7c1c1daeb8c475000499dbf89a4e8f Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Thu, 9 Dec 2021 01:30:36 +0000 Subject: [PATCH 1/7] 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 From 512d314cf78adb6395f8ffada03a0a2ac7d7726f Mon Sep 17 00:00:00 2001 From: Greg Buehler Date: Thu, 17 Mar 2022 10:32:07 -0700 Subject: [PATCH 2/7] Update test_automatic.py --- .../tests/test_automatic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py index 046a41c5bc..3e20b98e2b 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py @@ -81,7 +81,7 @@ def test_tween_list(self): def test_registry_name_is_this_module(self): config = Configurator() - self.assertEqual(config.registry.__name__, __name__.rsplit('.')[0]) + self.assertEqual(config.registry.__name__, __name__.rsplit(".")[0]) class TestWrappedWithOtherFramework( From 70a375ffcd3b201a7665a1a3a5943bdd2d9e1ea1 Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Fri, 25 Mar 2022 18:22:51 +0000 Subject: [PATCH 3/7] fix lint --- .../tests/test_automatic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py index 3e20b98e2b..b71e2f4152 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py @@ -81,7 +81,7 @@ def test_tween_list(self): def test_registry_name_is_this_module(self): config = Configurator() - self.assertEqual(config.registry.__name__, __name__.rsplit(".")[0]) + self.assertEqual(config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0]) class TestWrappedWithOtherFramework( From 464009b9f168dab5fd8f67cb73185083507ca58d Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Tue, 29 Mar 2022 19:19:17 +0000 Subject: [PATCH 4/7] fix lint again --- .../tests/test_automatic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py index b71e2f4152..06db6874a8 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py @@ -81,7 +81,9 @@ def test_tween_list(self): def test_registry_name_is_this_module(self): config = Configurator() - self.assertEqual(config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0]) + self.assertEqual( + config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0] + ) class TestWrappedWithOtherFramework( From c1dbf5970fd8a2081b7e522b69c1ea44e7da45f8 Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Thu, 14 Apr 2022 18:21:37 +0000 Subject: [PATCH 5/7] Fix for PyPy --- .../src/opentelemetry/instrumentation/pyramid/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 58e8049f23..5cd0a3a305 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py @@ -93,6 +93,7 @@ API --- """ +import platform from typing import Collection @@ -115,6 +116,11 @@ # from importing an unused symbol. trace_tween_factory # pylint: disable=pointless-statement +if platform.python_implementation() == 'PyPy': + CALLER_LEVELS = 3 +else: + CALLER_LEVELS = 2 + def _traced_init(wrapped, instance, args, kwargs): settings = kwargs.get("settings", {}) @@ -139,7 +145,7 @@ def _traced_init(wrapped, instance, args, kwargs): # 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) + kwargs["package"] = caller_package(level=CALLER_LEVELS) wrapped(*args, **kwargs) instance.include("opentelemetry.instrumentation.pyramid.callbacks") From c004178ece057f54016ac8a548ae15e9a92147fb Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Thu, 14 Apr 2022 18:52:22 +0000 Subject: [PATCH 6/7] lint --- .../src/opentelemetry/instrumentation/pyramid/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 51ab890c74..1124603991 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py @@ -167,7 +167,7 @@ # from importing an unused symbol. trace_tween_factory # pylint: disable=pointless-statement -if platform.python_implementation() == 'PyPy': +if platform.python_implementation() == "PyPy": CALLER_LEVELS = 3 else: CALLER_LEVELS = 2 From 69d4d476ab147d68b6147067b6e96c219364a61c Mon Sep 17 00:00:00 2001 From: Ken Robbins Date: Thu, 14 Apr 2022 20:55:15 +0000 Subject: [PATCH 7/7] lint --- .../src/opentelemetry/instrumentation/pyramid/__init__.py | 1 - 1 file changed, 1 deletion(-) 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 1124603991..0c7a6bdfdd 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py @@ -145,7 +145,6 @@ --- """ import platform - from typing import Collection from pyramid.config import Configurator