|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | """ |
16 | | -The integration with PostgreSQL supports the `Psycopg`_ library and is specified |
17 | | -to ``trace_integration`` using ``'PostgreSQL'``. |
| 16 | +The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by |
| 17 | +using ``Psycopg2Instrumentor``. |
18 | 18 |
|
19 | 19 | .. _Psycopg: http://initd.org/psycopg/ |
20 | 20 |
|
|
26 | 26 | import psycopg2 |
27 | 27 | from opentelemetry import trace |
28 | 28 | from opentelemetry.sdk.trace import TracerProvider |
29 | | - from opentelemetry.trace.ext.psycopg2 import trace_integration |
| 29 | + from opentelemetry.trace.ext.psycopg2 import Psycopg2Instrumentor |
30 | 30 |
|
31 | 31 | trace.set_tracer_provider(TracerProvider()) |
32 | 32 |
|
33 | | - trace_integration() |
| 33 | + Psycopg2Instrumentor().instrument() |
| 34 | +
|
34 | 35 | cnx = psycopg2.connect(database='Database') |
35 | 36 | cursor = cnx.cursor() |
36 | 37 | cursor.execute("INSERT INTO test (testField) VALUES (123)") |
|
41 | 42 | --- |
42 | 43 | """ |
43 | 44 |
|
44 | | -import logging |
45 | 45 | import typing |
46 | 46 |
|
47 | 47 | import psycopg2 |
48 | 48 | import wrapt |
49 | | -from psycopg2.sql import Composable |
50 | 49 |
|
51 | | -from opentelemetry.ext.dbapi import DatabaseApiIntegration, TracedCursor |
| 50 | +from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor |
| 51 | +from opentelemetry.ext import dbapi |
52 | 52 | from opentelemetry.ext.psycopg2.version import __version__ |
53 | | -from opentelemetry.trace import Tracer, get_tracer |
54 | | - |
55 | | -logger = logging.getLogger(__name__) |
56 | | - |
57 | | -DATABASE_COMPONENT = "postgresql" |
58 | | -DATABASE_TYPE = "sql" |
| 53 | +from opentelemetry.trace import TracerProvider, get_tracer |
59 | 54 |
|
60 | 55 |
|
61 | | -def trace_integration(tracer_provider=None): |
62 | | - """Integrate with PostgreSQL Psycopg library. |
63 | | - Psycopg: http://initd.org/psycopg/ |
64 | | - """ |
65 | | - |
66 | | - tracer = get_tracer(__name__, __version__, tracer_provider) |
67 | | - |
68 | | - connection_attributes = { |
| 56 | +class Psycopg2Instrumentor(BaseInstrumentor): |
| 57 | + _CONNECTION_ATTRIBUTES = { |
69 | 58 | "database": "info.dbname", |
70 | 59 | "port": "info.port", |
71 | 60 | "host": "info.host", |
72 | 61 | "user": "info.user", |
73 | 62 | } |
74 | | - db_integration = DatabaseApiIntegration( |
75 | | - tracer, |
76 | | - DATABASE_COMPONENT, |
77 | | - database_type=DATABASE_TYPE, |
78 | | - connection_attributes=connection_attributes, |
79 | | - ) |
80 | | - |
81 | | - # pylint: disable=unused-argument |
82 | | - def wrap_connect( |
83 | | - connect_func: typing.Callable[..., any], |
84 | | - instance: typing.Any, |
85 | | - args: typing.Tuple[any, any], |
86 | | - kwargs: typing.Dict[any, any], |
87 | | - ): |
88 | | - connection = connect_func(*args, **kwargs) |
89 | | - db_integration.get_connection_attributes(connection) |
90 | | - connection.cursor_factory = PsycopgTraceCursor |
91 | | - return connection |
92 | | - |
93 | | - try: |
94 | | - wrapt.wrap_function_wrapper(psycopg2, "connect", wrap_connect) |
95 | | - except Exception as ex: # pylint: disable=broad-except |
96 | | - logger.warning("Failed to integrate with pyscopg2. %s", str(ex)) |
97 | | - |
98 | | - class PsycopgTraceCursor(psycopg2.extensions.cursor): |
99 | | - def __init__(self, *args, **kwargs): |
100 | | - self._traced_cursor = TracedCursor(db_integration) |
101 | | - super(PsycopgTraceCursor, self).__init__(*args, **kwargs) |
102 | | - |
103 | | - # pylint: disable=redefined-builtin |
104 | | - def execute(self, query, vars=None): |
105 | | - if isinstance(query, Composable): |
106 | | - query = query.as_string(self) |
107 | | - return self._traced_cursor.traced_execution( |
108 | | - super(PsycopgTraceCursor, self).execute, query, vars |
109 | | - ) |
110 | | - |
111 | | - # pylint: disable=redefined-builtin |
112 | | - def executemany(self, query, vars): |
113 | | - if isinstance(query, Composable): |
114 | | - query = query.as_string(self) |
115 | | - return self._traced_cursor.traced_execution( |
116 | | - super(PsycopgTraceCursor, self).executemany, query, vars |
117 | | - ) |
118 | | - |
119 | | - # pylint: disable=redefined-builtin |
120 | | - def callproc(self, procname, vars=None): |
121 | | - return self._traced_cursor.traced_execution( |
122 | | - super(PsycopgTraceCursor, self).callproc, procname, vars |
123 | | - ) |
| 63 | + |
| 64 | + _DATABASE_COMPONENT = "postgresql" |
| 65 | + _DATABASE_TYPE = "sql" |
| 66 | + |
| 67 | + def _instrument(self, **kwargs): |
| 68 | + """Integrate with PostgreSQL Psycopg library. |
| 69 | + Psycopg: http://initd.org/psycopg/ |
| 70 | + """ |
| 71 | + |
| 72 | + tracer_provider = kwargs.get("tracer_provider") |
| 73 | + |
| 74 | + tracer = get_tracer(__name__, __version__, tracer_provider) |
| 75 | + |
| 76 | + dbapi.wrap_connect( |
| 77 | + tracer, |
| 78 | + psycopg2, |
| 79 | + "connect", |
| 80 | + self._DATABASE_COMPONENT, |
| 81 | + self._DATABASE_TYPE, |
| 82 | + self._CONNECTION_ATTRIBUTES, |
| 83 | + ) |
| 84 | + |
| 85 | + def _uninstrument(self, **kwargs): |
| 86 | + """"Disable Psycopg2 instrumentation""" |
| 87 | + dbapi.unwrap_connect(psycopg2, "connect") |
| 88 | + |
| 89 | + # pylint:disable=no-self-use |
| 90 | + def instrument_connection(self, connection): |
| 91 | + """Enable instrumentation in a Psycopg2 connection. |
| 92 | +
|
| 93 | + Args: |
| 94 | + connection: The connection to instrument. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + An instrumented connection. |
| 98 | + """ |
| 99 | + tracer = get_tracer(__name__, __version__) |
| 100 | + |
| 101 | + return dbapi.instrument_connection( |
| 102 | + tracer, |
| 103 | + connection, |
| 104 | + self._DATABASE_COMPONENT, |
| 105 | + self._DATABASE_TYPE, |
| 106 | + self._CONNECTION_ATTRIBUTES, |
| 107 | + ) |
| 108 | + |
| 109 | + def uninstrument_connection(self, connection): |
| 110 | + """Disable instrumentation in a Psycopg2 connection. |
| 111 | +
|
| 112 | + Args: |
| 113 | + connection: The connection to uninstrument. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + An uninstrumented connection. |
| 117 | + """ |
| 118 | + return dbapi.uninstrument_connection(connection) |
0 commit comments