Skip to content

Commit 95588c1

Browse files
Refactor abstract classes BaseTracedConnection|CursorProxy
1 parent 9846f75 commit 95588c1

File tree

2 files changed

+78
-97
lines changed
  • instrumentation
    • opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi
    • opentelemetry-instrumentation-mysql/src/opentelemetry/instrumentation/mysql

2 files changed

+78
-97
lines changed

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

Lines changed: 67 additions & 46 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

@@ -543,35 +550,49 @@ def traced_execution(
543550
return query_method(*args, **kwargs)
544551

545552

546-
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
547-
_cursor_tracer = CursorTracer(db_api_integration)
553+
# pylint: disable=abstract-method
554+
class BaseTracedCursorProxy(ABC, wrapt.ObjectProxy):
555+
@abstractmethod
556+
# def _set_cursor_tracer(self, db_api_integration, **kwargs):
557+
def _set_cursor_tracer(self, **kwargs):
558+
"""Set self._cursor_tracer with db_api_integration kwarg"""
548559

549-
# pylint: disable=abstract-method
550-
class TracedCursorProxy(wrapt.ObjectProxy):
551-
# pylint: disable=unused-argument
552-
def __init__(self, cursor, *args, **kwargs):
553-
wrapt.ObjectProxy.__init__(self, cursor)
554-
555-
def execute(self, *args, **kwargs):
556-
return _cursor_tracer.traced_execution(
557-
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
558-
)
560+
# pylint: disable=unused-argument
561+
def __init__(self, cursor, *args, **kwargs):
562+
wrapt.ObjectProxy.__init__(self, cursor)
563+
self._cursor_tracer = self._set_cursor_tracer(**kwargs)
559564

560-
def executemany(self, *args, **kwargs):
561-
return _cursor_tracer.traced_execution(
562-
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
563-
)
565+
def callproc(self, *args, **kwargs):
566+
return self._cursor_tracer.traced_execution(
567+
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
568+
)
564569

565-
def callproc(self, *args, **kwargs):
566-
return _cursor_tracer.traced_execution(
567-
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
568-
)
570+
def execute(self, *args, **kwargs):
571+
return self._cursor_tracer.traced_execution(
572+
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
573+
)
574+
575+
def executemany(self, *args, **kwargs):
576+
return self._cursor_tracer.traced_execution(
577+
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
578+
)
579+
580+
def __enter__(self):
581+
self.__wrapped__.__enter__()
582+
return self
569583

570-
def __enter__(self):
571-
self.__wrapped__.__enter__()
572-
return self
584+
def __exit__(self, *args, **kwargs):
585+
self.__wrapped__.__exit__(*args, **kwargs)
573586

574-
def __exit__(self, *args, **kwargs):
575-
self.__wrapped__.__exit__(*args, **kwargs)
576587

577-
return TracedCursorProxy(cursor, *args, **kwargs)
588+
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
589+
class TracedCursorProxy(BaseTracedCursorProxy):
590+
def _set_cursor_tracer(self, **kwargs):
591+
self._cursor_tracer = CursorTracer(
592+
kwargs.get("db_api_integration"),
593+
)
594+
595+
kwargs["db_api_integration"] = db_api_integration
596+
cursor_proxy = TracedCursorProxy(cursor, *args, **kwargs)
597+
cursor_proxy._set_cursor_tracer(**kwargs)
598+
return cursor_proxy

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

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,7 @@ def get_traced_connection_proxy(
231231
connection, db_api_integration, *args, **kwargs
232232
):
233233
# pylint: disable=abstract-method
234-
class TracedConnectionProxy(wrapt.ObjectProxy):
235-
# pylint: disable=unused-argument
236-
def __init__(self, connection, *args, **kwargs):
237-
wrapt.ObjectProxy.__init__(self, connection)
238-
239-
def __getattribute__(self, name):
240-
if object.__getattribute__(self, name):
241-
return object.__getattribute__(self, name)
242-
243-
return object.__getattribute__(
244-
object.__getattribute__(self, "_connection"), name
245-
)
246-
234+
class TracedConnectionProxy(dbapi.BaseTracedConnectionProxy):
247235
def cursor(self, *args, **kwargs):
248236
wrapped_cursor = self.__wrapped__.cursor(*args, **kwargs)
249237

@@ -306,13 +294,6 @@ def is_mysql_connector_cursor_prepared(self, cursor): # pylint: disable=no-self
306294

307295
return False
308296

309-
def __enter__(self):
310-
self.__wrapped__.__enter__()
311-
return self
312-
313-
def __exit__(self, *args, **kwargs):
314-
self.__wrapped__.__exit__(*args, **kwargs)
315-
316297
return TracedConnectionProxy(connection, *args, **kwargs)
317298

318299

@@ -325,40 +306,19 @@ def __init__(
325306
super().__init__(db_api_integration)
326307
# It's common to have multiple db client cursors per app,
327308
# so enable_commenter is set at the cursor level and used
328-
# during traced query execution for mysql-connector
309+
# during CursorTracer.traced_execution for mysql-connector
329310
self._commenter_enabled = enable_commenter
330311

331312

332313
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
333-
enable_commenter = kwargs.get("enable_commenter", False)
334-
_cursor_tracer = CursorTracer(db_api_integration, enable_commenter)
335-
336-
# pylint: disable=abstract-method
337-
class TracedCursorProxy(wrapt.ObjectProxy):
338-
# pylint: disable=unused-argument
339-
def __init__(self, cursor, *args, **kwargs):
340-
wrapt.ObjectProxy.__init__(self, cursor)
341-
342-
def execute(self, *args, **kwargs):
343-
return _cursor_tracer.traced_execution(
344-
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
345-
)
346-
347-
def executemany(self, *args, **kwargs):
348-
return _cursor_tracer.traced_execution(
349-
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
314+
class TracedCursorProxy(dbapi.BaseTracedCursorProxy):
315+
def _set_cursor_tracer(self, **kwargs):
316+
self._cursor_tracer = CursorTracer(
317+
kwargs.get("db_api_integration"),
318+
kwargs.get("enable_commenter", False),
350319
)
351320

352-
def callproc(self, *args, **kwargs):
353-
return _cursor_tracer.traced_execution(
354-
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
355-
)
356-
357-
def __enter__(self):
358-
self.__wrapped__.__enter__()
359-
return self
360-
361-
def __exit__(self, *args, **kwargs):
362-
self.__wrapped__.__exit__(*args, **kwargs)
363-
364-
return TracedCursorProxy(cursor, *args, **kwargs)
321+
kwargs["db_api_integration"] = db_api_integration
322+
cursor_proxy = TracedCursorProxy(cursor, *args, **kwargs)
323+
cursor_proxy._set_cursor_tracer(**kwargs)
324+
return cursor_proxy

0 commit comments

Comments
 (0)