Skip to content

Commit 47caeab

Browse files
marcuslimdwsrikanthccvshalevr
authored
Stop multiple calls to AsyncPGInstrumentor.__init__ from clobbering instance attributes (#1791)
* Stop multiple calls to AsyncPGInstrumentor.__init__ from clobbering instance tracer attribute * Remove class-level initialisation of _tracer * Fix regex * Add to changelog * Fix lint errors * Update CHANGELOG.md Co-authored-by: Shalev Roda <65566801+shalevr@users.noreply.github.com> * Set tracer in class definition to avoid lint error --------- Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com> Co-authored-by: Shalev Roda <65566801+shalevr@users.noreply.github.com>
1 parent 13ce910 commit 47caeab

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
([#2151](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2151))
1212
- `opentelemetry-resource-detector-azure` Added 10s timeout to VM Resource Detector
1313
([#2119](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2119))
14+
- `opentelemetry-instrumentation-asyncpg` Allow AsyncPGInstrumentor to be instantiated multiple times
15+
([#1791](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1791))
1416
- `opentelemetry-instrumentation-confluent-kafka` Add support for higher versions until 2.3.0 of confluent_kafka
1517
([#2132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2132))
1618
- `opentelemetry-resource-detector-azure` Changed timeout to 4 seconds due to [timeout bug](https://github.com/open-telemetry/opentelemetry-python/issues/3644)

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ def _hydrate_span_from_args(connection, query, parameters) -> dict:
9696

9797

9898
class AsyncPGInstrumentor(BaseInstrumentor):
99+
100+
_leading_comment_remover = re.compile(r"^/\*.*?\*/")
101+
_tracer = None
102+
99103
def __init__(self, capture_parameters=False):
100104
super().__init__()
101105
self.capture_parameters = capture_parameters
102-
self._tracer = None
103-
self._leading_comment_remover = re.compile(r"^/\*.*?\*/")
104106

105107
def instrumentation_dependencies(self) -> Collection[str]:
106108
return _instruments

instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class TestAsyncPGInstrumentation(TestBase):
8-
def test_duplicated_instrumentation(self):
8+
def test_duplicated_instrumentation_can_be_uninstrumented(self):
99
AsyncPGInstrumentor().instrument()
1010
AsyncPGInstrumentor().instrument()
1111
AsyncPGInstrumentor().instrument()
@@ -16,6 +16,14 @@ def test_duplicated_instrumentation(self):
1616
hasattr(method, "_opentelemetry_ext_asyncpg_applied")
1717
)
1818

19+
def test_duplicated_instrumentation_works(self):
20+
first = AsyncPGInstrumentor()
21+
first.instrument()
22+
second = AsyncPGInstrumentor()
23+
second.instrument()
24+
self.assertIsNotNone(first._tracer)
25+
self.assertIsNotNone(second._tracer)
26+
1927
def test_duplicated_uninstrumentation(self):
2028
AsyncPGInstrumentor().instrument()
2129
AsyncPGInstrumentor().uninstrument()

0 commit comments

Comments
 (0)