Skip to content

Commit 4ad7592

Browse files
authored
pyramid: Fix which package is the correct caller in _traced_init. (#830)
1 parent 9d14265 commit 4ad7592

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.10.0-0.29b0...HEAD)
99

1010
### Fixed
11+
- `opentelemetry-instrumentation-pyramid` Fixed which package is the correct caller in _traced_init.
12+
([#830](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/830))
1113
- `opentelemetry-instrumentation-tornado` Fix Tornado errors mapping to 500
1214
([#1048])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1048)
1315
- `opentelemetry-instrumentation-urllib` make span attributes available to sampler

instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
API
145145
---
146146
"""
147-
147+
import platform
148148
from typing import Collection
149149

150150
from pyramid.config import Configurator
@@ -166,6 +166,11 @@
166166
# from importing an unused symbol.
167167
trace_tween_factory # pylint: disable=pointless-statement
168168

169+
if platform.python_implementation() == "PyPy":
170+
CALLER_LEVELS = 3
171+
else:
172+
CALLER_LEVELS = 2
173+
169174

170175
def _traced_init(wrapped, instance, args, kwargs):
171176
settings = kwargs.get("settings", {})
@@ -185,10 +190,12 @@ def _traced_init(wrapped, instance, args, kwargs):
185190
# to find the calling package. So if we let the original `__init__`
186191
# function call it, our wrapper will mess things up.
187192
if not kwargs.get("package", None):
188-
# Get the package for the third frame up from this one.
189-
# Default is `level=2` which will give us the package from `wrapt`
190-
# instead of the desired package (the caller)
191-
kwargs["package"] = caller_package(level=3)
193+
# Get the package for the 2nd frame up from this one.
194+
# Default is `level=2` one level down (in Configurator.__init__).
195+
# We want the 3rd level from _there_. Since we are already 1 level above,
196+
# we need the 2nd level up from here, which will give us the package from
197+
# `wrapt` instead of the desired package (the caller)
198+
kwargs["package"] = caller_package(level=CALLER_LEVELS)
192199

193200
wrapped(*args, **kwargs)
194201
instance.include("opentelemetry.instrumentation.pyramid.callbacks")

instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_tween_list(self):
8787
span_list = self.memory_exporter.get_finished_spans()
8888
self.assertEqual(len(span_list), 1)
8989

90+
def test_registry_name_is_this_module(self):
91+
config = Configurator()
92+
self.assertEqual(
93+
config.registry.__name__, __name__.rsplit(".", maxsplit=1)[0]
94+
)
95+
9096

9197
class TestWrappedWithOtherFramework(
9298
InstrumentationTest, TestBase, WsgiTestBase

0 commit comments

Comments
 (0)