|
41 | 41 | import logging |
42 | 42 | import re |
43 | 43 | import typing |
| 44 | +from abc import ABC, abstractmethod |
44 | 45 |
|
45 | 46 | import wrapt |
46 | 47 |
|
@@ -390,35 +391,41 @@ def get_connection_attributes(self, connection): |
390 | 391 | self.span_attributes[SpanAttributes.NET_PEER_PORT] = port |
391 | 392 |
|
392 | 393 |
|
| 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 | + |
393 | 419 | def get_traced_connection_proxy( |
394 | 420 | connection, db_api_integration, *args, **kwargs |
395 | 421 | ): |
396 | 422 | # 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): |
410 | 424 | def cursor(self, *args, **kwargs): |
411 | 425 | return get_traced_cursor_proxy( |
412 | 426 | self.__wrapped__.cursor(*args, **kwargs), db_api_integration |
413 | 427 | ) |
414 | 428 |
|
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 | | - |
422 | 429 | return TracedConnectionProxy(connection, *args, **kwargs) |
423 | 430 |
|
424 | 431 |
|
@@ -543,35 +550,49 @@ def traced_execution( |
543 | 550 | return query_method(*args, **kwargs) |
544 | 551 |
|
545 | 552 |
|
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""" |
548 | 559 |
|
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) |
559 | 564 |
|
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 | + ) |
564 | 569 |
|
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 |
569 | 583 |
|
570 | | - def __enter__(self): |
571 | | - self.__wrapped__.__enter__() |
572 | | - return self |
| 584 | + def __exit__(self, *args, **kwargs): |
| 585 | + self.__wrapped__.__exit__(*args, **kwargs) |
573 | 586 |
|
574 | | - def __exit__(self, *args, **kwargs): |
575 | | - self.__wrapped__.__exit__(*args, **kwargs) |
576 | 587 |
|
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 |
0 commit comments