From 73e62258ab71a1050d5da59b8f1aa7712052e06a Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Thu, 8 Oct 2020 15:25:20 -0400 Subject: [PATCH] Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212) --- .../instrumentation/asyncpg/__init__.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py index 189809809..6af816b39 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py @@ -112,9 +112,6 @@ class AsyncPGInstrumentor(BaseInstrumentor): unwrap(asyncpg.Connection, method) async def _do_execute(self, func, instance, args, kwargs): - span_attributes = _hydrate_span_from_args( - instance, args[0], args[1:] if self.capture_parameters else None, - ) tracer = getattr(asyncpg, _APPLIED) exception = None @@ -122,9 +119,14 @@ class AsyncPGInstrumentor(BaseInstrumentor): with tracer.start_as_current_span( "postgresql", kind=SpanKind.CLIENT ) as span: - - for attribute, value in span_attributes.items(): - span.set_attribute(attribute, value) + if span.is_recording(): + span_attributes = _hydrate_span_from_args( + instance, + args[0], + args[1:] if self.capture_parameters else None, + ) + for attribute, value in span_attributes.items(): + span.set_attribute(attribute, value) try: result = await func(*args, **kwargs) @@ -132,11 +134,12 @@ class AsyncPGInstrumentor(BaseInstrumentor): exception = exc raise finally: - if exception is not None: - span.set_status( - Status(_exception_to_canonical_code(exception)) - ) - else: - span.set_status(Status(StatusCanonicalCode.OK)) + if span.is_recording(): + if exception is not None: + span.set_status( + Status(_exception_to_canonical_code(exception)) + ) + else: + span.set_status(Status(StatusCanonicalCode.OK)) return result