Skip to content

Commit 881bc76

Browse files
committed
feature: hydrate AsyncPG span attributes at creation (#3643)
1 parent ba0644f commit 881bc76

File tree

1 file changed

+16
-19
lines changed
  • instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg

1 file changed

+16
-19
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,16 @@ async def _do_execute(self, func, instance, args, kwargs):
178178
except IndexError:
179179
name = ""
180180

181+
# Hydrate attributes before span creation to enable filtering
182+
span_attributes = _hydrate_span_from_args(
183+
instance,
184+
args[0],
185+
args[1:] if self.capture_parameters else None,
186+
)
187+
181188
with self._tracer.start_as_current_span(
182-
name, kind=SpanKind.CLIENT
189+
name, kind=SpanKind.CLIENT, attributes=span_attributes
183190
) as span:
184-
if span.is_recording():
185-
span_attributes = _hydrate_span_from_args(
186-
instance,
187-
args[0],
188-
args[1:] if self.capture_parameters else None,
189-
)
190-
for attribute, value in span_attributes.items():
191-
span.set_attribute(attribute, value)
192-
193191
try:
194192
result = await func(*args, **kwargs)
195193
except Exception as exc: # pylint: disable=W0703
@@ -217,20 +215,19 @@ async def _do_cursor_execute(self, func, instance, args, kwargs):
217215
except IndexError:
218216
name = ""
219217

218+
# Hydrate attributes before span creation to enable filtering
219+
span_attributes = _hydrate_span_from_args(
220+
instance._connection,
221+
instance._query,
222+
instance._args if self.capture_parameters else None,
223+
)
224+
220225
stop = False
221226
with self._tracer.start_as_current_span(
222227
f"CURSOR: {name}",
223228
kind=SpanKind.CLIENT,
229+
attributes=span_attributes,
224230
) as span:
225-
if span.is_recording():
226-
span_attributes = _hydrate_span_from_args(
227-
instance._connection,
228-
instance._query,
229-
instance._args if self.capture_parameters else None,
230-
)
231-
for attribute, value in span_attributes.items():
232-
span.set_attribute(attribute, value)
233-
234231
try:
235232
result = await func(*args, **kwargs)
236233
except StopAsyncIteration:

0 commit comments

Comments
 (0)