Skip to content

Commit 5e1e57a

Browse files
DB-API integration: abstract classes BaseTracedConnection|CursorProxy
1 parent 23f67eb commit 5e1e57a

File tree

1 file changed

+61
-45
lines changed
  • instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi

1 file changed

+61
-45
lines changed

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

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import logging
4242
import re
4343
import typing
44+
from abc import ABC, abstractmethod
4445

4546
import wrapt
4647

@@ -390,35 +391,41 @@ def get_connection_attributes(self, connection):
390391
self.span_attributes[SpanAttributes.NET_PEER_PORT] = port
391392

392393

394+
class BaseTracedConnectionProxy(ABC, wrapt.ObjectProxy):
395+
# pylint: disable=unused-argument
396+
def __init__(self, connection, *args, **kwargs):
397+
wrapt.ObjectProxy.__init__(self, connection)
398+
399+
def __getattribute__(self, name):
400+
if object.__getattribute__(self, name):
401+
return object.__getattribute__(self, name)
402+
403+
return object.__getattribute__(
404+
object.__getattribute__(self, "_connection"), name
405+
)
406+
407+
@abstractmethod
408+
def cursor(self, *args, **kwargs):
409+
"""Returns instrumented database query cursor"""
410+
411+
def __enter__(self):
412+
self.__wrapped__.__enter__()
413+
return self
414+
415+
def __exit__(self, *args, **kwargs):
416+
self.__wrapped__.__exit__(*args, **kwargs)
417+
418+
393419
def get_traced_connection_proxy(
394420
connection, db_api_integration, *args, **kwargs
395421
):
396422
# pylint: disable=abstract-method
397-
class TracedConnectionProxy(wrapt.ObjectProxy):
398-
# pylint: disable=unused-argument
399-
def __init__(self, connection, *args, **kwargs):
400-
wrapt.ObjectProxy.__init__(self, connection)
401-
402-
def __getattribute__(self, name):
403-
if object.__getattribute__(self, name):
404-
return object.__getattribute__(self, name)
405-
406-
return object.__getattribute__(
407-
object.__getattribute__(self, "_connection"), name
408-
)
409-
423+
class TracedConnectionProxy(BaseTracedConnectionProxy):
410424
def cursor(self, *args, **kwargs):
411425
return get_traced_cursor_proxy(
412426
self.__wrapped__.cursor(*args, **kwargs), db_api_integration
413427
)
414428

415-
def __enter__(self):
416-
self.__wrapped__.__enter__()
417-
return self
418-
419-
def __exit__(self, *args, **kwargs):
420-
self.__wrapped__.__exit__(*args, **kwargs)
421-
422429
return TracedConnectionProxy(connection, *args, **kwargs)
423430

424431

@@ -538,35 +545,44 @@ def traced_execution(
538545
return query_method(*args, **kwargs)
539546

540547

541-
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
542-
_cursor_tracer = CursorTracer(db_api_integration)
548+
# pylint: disable=abstract-method
549+
class BaseTracedCursorProxy(ABC, wrapt.ObjectProxy):
550+
# pylint: disable=unused-argument
551+
@abstractmethod
552+
def __init__(self, cursor, *args, **kwargs):
553+
"""Wrap db client cursor for tracing"""
554+
wrapt.ObjectProxy.__init__(self, cursor)
555+
self._cursor_tracer = None
556+
557+
def callproc(self, *args, **kwargs):
558+
return self._cursor_tracer.traced_execution(
559+
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
560+
)
543561

544-
# pylint: disable=abstract-method
545-
class TracedCursorProxy(wrapt.ObjectProxy):
546-
# pylint: disable=unused-argument
547-
def __init__(self, cursor, *args, **kwargs):
548-
wrapt.ObjectProxy.__init__(self, cursor)
549-
550-
def execute(self, *args, **kwargs):
551-
return _cursor_tracer.traced_execution(
552-
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
553-
)
562+
def execute(self, *args, **kwargs):
563+
return self._cursor_tracer.traced_execution(
564+
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
565+
)
554566

555-
def executemany(self, *args, **kwargs):
556-
return _cursor_tracer.traced_execution(
557-
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
558-
)
567+
def executemany(self, *args, **kwargs):
568+
return self._cursor_tracer.traced_execution(
569+
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
570+
)
559571

560-
def callproc(self, *args, **kwargs):
561-
return _cursor_tracer.traced_execution(
562-
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
563-
)
572+
def __enter__(self):
573+
self.__wrapped__.__enter__()
574+
return self
564575

565-
def __enter__(self):
566-
self.__wrapped__.__enter__()
567-
return self
576+
def __exit__(self, *args, **kwargs):
577+
self.__wrapped__.__exit__(*args, **kwargs)
568578

569-
def __exit__(self, *args, **kwargs):
570-
self.__wrapped__.__exit__(*args, **kwargs)
579+
580+
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
581+
class TracedCursorProxy(BaseTracedCursorProxy):
582+
def __init__(self, *args, **kwargs):
583+
super().__init__(*args, **kwargs)
584+
self._cursor_tracer = CursorTracer(
585+
db_api_integration,
586+
)
571587

572588
return TracedCursorProxy(cursor, *args, **kwargs)

0 commit comments

Comments
 (0)