Skip to content

Commit cc08874

Browse files
committed
Move implementation to traced_execution_async
1 parent f940556 commit cc08874

File tree

2 files changed

+45
-47
lines changed
  • instrumentation
    • opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi
    • opentelemetry-instrumentation-psycopg/src/opentelemetry/instrumentation/psycopg

2 files changed

+45
-47
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import functools
4343
import logging
4444
import re
45-
from typing import Any, Callable, Generic, TypeVar
45+
from typing import Any, Awaitable, Callable, Generic, TypeVar
4646

4747
import wrapt
4848
from wrapt import wrap_function_wrapper
@@ -587,6 +587,44 @@ def traced_execution(
587587
self._populate_span(span, cursor, *args)
588588
return query_method(*args, **kwargs)
589589

590+
async def traced_execution_async(
591+
self,
592+
cursor: CursorT,
593+
query_method: Callable[..., Awaitable[Any]],
594+
*args: tuple[Any, ...],
595+
**kwargs: dict[Any, Any],
596+
):
597+
name = self.get_operation_name(cursor, args)
598+
if not name:
599+
name = (
600+
self._db_api_integration.database
601+
if self._db_api_integration.database
602+
else self._db_api_integration.name
603+
)
604+
605+
with self._db_api_integration._tracer.start_as_current_span(
606+
name, kind=SpanKind.CLIENT
607+
) as span:
608+
if span.is_recording():
609+
if args and self._commenter_enabled:
610+
if self._enable_attribute_commenter:
611+
# sqlcomment is added to executed query and db.statement span attribute
612+
args = self._update_args_with_added_sql_comment(
613+
args, cursor
614+
)
615+
self._populate_span(span, cursor, *args)
616+
else:
617+
# sqlcomment is only added to executed query
618+
# so db.statement is set before add_sql_comment
619+
self._populate_span(span, cursor, *args)
620+
args = self._update_args_with_added_sql_comment(
621+
args, cursor
622+
)
623+
else:
624+
# no sqlcomment anywhere
625+
self._populate_span(span, cursor, *args)
626+
return await query_method(*args, **kwargs)
627+
590628

591629
# pylint: disable=abstract-method
592630
class TracedCursorProxy(wrapt.ObjectProxy, Generic[CursorT]):

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

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
from __future__ import annotations
141141

142142
import logging
143-
from typing import Any, Awaitable, Callable, Collection, TypeVar
143+
from typing import Any, Callable, Collection, TypeVar
144144

145145
import psycopg # pylint: disable=import-self
146146
from psycopg.sql import Composed # pylint: disable=no-name-in-module
@@ -149,7 +149,7 @@
149149
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
150150
from opentelemetry.instrumentation.psycopg.package import _instruments
151151
from opentelemetry.instrumentation.psycopg.version import __version__
152-
from opentelemetry.trace import SpanKind, TracerProvider
152+
from opentelemetry.trace import TracerProvider
153153

154154
_logger = logging.getLogger(__name__)
155155
_OTEL_CURSOR_FACTORY_KEY = "_otel_orig_cursor_factory"
@@ -381,46 +381,6 @@ def callproc(self, *args: Any, **kwargs: Any):
381381
return TracedCursorFactory
382382

383383

384-
class AsyncCursorTracer(CursorTracer):
385-
async def traced_execution(
386-
self,
387-
cursor: dbapi.CursorT,
388-
query_method: Callable[..., Awaitable[Any]],
389-
*args: tuple[Any, ...],
390-
**kwargs: dict[Any, Any],
391-
):
392-
name = self.get_operation_name(cursor, args)
393-
if not name:
394-
name = (
395-
self._db_api_integration.database
396-
if self._db_api_integration.database
397-
else self._db_api_integration.name
398-
)
399-
400-
with self._db_api_integration._tracer.start_as_current_span(
401-
name, kind=SpanKind.CLIENT
402-
) as span:
403-
if span.is_recording():
404-
if args and self._commenter_enabled:
405-
if self._enable_attribute_commenter:
406-
# sqlcomment is added to executed query and db.statement span attribute
407-
args = self._update_args_with_added_sql_comment(
408-
args, cursor
409-
)
410-
self._populate_span(span, cursor, *args)
411-
else:
412-
# sqlcomment is only added to executed query
413-
# so db.statement is set before add_sql_comment
414-
self._populate_span(span, cursor, *args)
415-
args = self._update_args_with_added_sql_comment(
416-
args, cursor
417-
)
418-
else:
419-
# no sqlcomment anywhere
420-
self._populate_span(span, cursor, *args)
421-
return await query_method(*args, **kwargs)
422-
423-
424384
def _new_cursor_async_factory(
425385
db_api: DatabaseApiAsyncIntegration | None = None,
426386
base_factory: type[psycopg.AsyncCursor] | None = None,
@@ -435,21 +395,21 @@ def _new_cursor_async_factory(
435395
tracer_provider=tracer_provider,
436396
)
437397
base_factory = base_factory or psycopg.AsyncCursor
438-
_cursor_tracer = AsyncCursorTracer(db_api)
398+
_cursor_tracer = CursorTracer(db_api)
439399

440400
class TracedCursorAsyncFactory(base_factory):
441401
async def execute(self, *args: Any, **kwargs: Any):
442-
return await _cursor_tracer.traced_execution(
402+
return await _cursor_tracer.traced_execution_async(
443403
self, super().execute, *args, **kwargs
444404
)
445405

446406
async def executemany(self, *args: Any, **kwargs: Any):
447-
return await _cursor_tracer.traced_execution(
407+
return await _cursor_tracer.traced_execution_async(
448408
self, super().executemany, *args, **kwargs
449409
)
450410

451411
async def callproc(self, *args: Any, **kwargs: Any):
452-
return await _cursor_tracer.traced_execution(
412+
return await _cursor_tracer.traced_execution_async(
453413
self, super().callproc, *args, **kwargs
454414
)
455415

0 commit comments

Comments
 (0)