Skip to content

Commit 953a113

Browse files
committed
* refactor on package name psycopg insteead of psycopg3
1 parent 057504a commit 953a113

File tree

16 files changed

+303
-1161
lines changed

16 files changed

+303
-1161
lines changed

.github/workflows/instrumentations_1.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ jobs:
3131
- "richconsole"
3232
- "psycopg"
3333
- "prometheus-remote-write"
34-
- "psycopg3"
3534
- "sdkextension-aws"
3635
- "propagator-aws-xray"
3736
- "propagator-ot-trace"

docs-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ kafka-python>=2.0,<3.0
3636
mysql-connector-python~=8.0
3737
mysqlclient~=2.1.1
3838
psutil>=5
39-
psycopg>=3.1.17
39+
psycopg~=3.1.17
4040
pika>=0.12.0
4141
pymongo~=3.1
4242
PyMySQL~=0.9.3

docs/instrumentation/psycopg3/psycopg3.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

instrumentation/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
| [opentelemetry-instrumentation-pika](./opentelemetry-instrumentation-pika) | pika >= 0.12.0 | No
3232
| [opentelemetry-instrumentation-psycopg](./opentelemetry-instrumentation-psycopg) | psycopg >= 3.1.0 | No
3333
| [opentelemetry-instrumentation-psycopg2](./opentelemetry-instrumentation-psycopg2) | psycopg2 >= 2.7.3.1 | No
34-
| [opentelemetry-instrumentation-psycopg3](./opentelemetry-instrumentation-psycopg3) | psycopg >= 3.1.12 | No
3534
| [opentelemetry-instrumentation-pymemcache](./opentelemetry-instrumentation-pymemcache) | pymemcache >= 1.3.5, < 5 | No
3635
| [opentelemetry-instrumentation-pymongo](./opentelemetry-instrumentation-pymongo) | pymongo >= 3.1, < 5.0 | No
3736
| [opentelemetry-instrumentation-pymysql](./opentelemetry-instrumentation-pymysql) | PyMySQL < 2 | No

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
from typing import Collection
107107

108108
import psycopg
109+
from psycopg import AsyncCursor as pg_async_cursor
109110
from psycopg import Cursor as pg_cursor # pylint: disable=no-name-in-module
110111
from psycopg.sql import Composed # pylint: disable=no-name-in-module
111112

@@ -151,9 +152,36 @@ def _instrument(self, **kwargs):
151152
commenter_options=commenter_options,
152153
)
153154

155+
dbapi.wrap_connect(
156+
__name__,
157+
psycopg.Connection,
158+
"connect",
159+
self._DATABASE_SYSTEM,
160+
self._CONNECTION_ATTRIBUTES,
161+
version=__version__,
162+
tracer_provider=tracer_provider,
163+
db_api_integration_factory=DatabaseApiIntegration,
164+
enable_commenter=enable_sqlcommenter,
165+
commenter_options=commenter_options,
166+
)
167+
dbapi.wrap_connect(
168+
__name__,
169+
psycopg.AsyncConnection,
170+
"connect",
171+
self._DATABASE_SYSTEM,
172+
self._CONNECTION_ATTRIBUTES,
173+
version=__version__,
174+
tracer_provider=tracer_provider,
175+
db_api_integration_factory=DatabaseApiAsyncIntegration,
176+
enable_commenter=enable_sqlcommenter,
177+
commenter_options=commenter_options,
178+
)
179+
154180
def _uninstrument(self, **kwargs):
155181
""" "Disable Psycopg instrumentation"""
156182
dbapi.unwrap_connect(psycopg, "connect")
183+
dbapi.unwrap_connect(psycopg.Connection, "connect")
184+
dbapi.unwrap_connect(psycopg.AsyncConnection, "connect")
157185

158186
# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
159187
@staticmethod
@@ -204,6 +232,26 @@ def wrapped_connection(
204232
return connection
205233

206234

235+
class DatabaseApiAsyncIntegration(dbapi.DatabaseApiIntegration):
236+
async def wrapped_connection(
237+
self,
238+
connect_method: typing.Callable[..., typing.Any],
239+
args: typing.Tuple[typing.Any, typing.Any],
240+
kwargs: typing.Dict[typing.Any, typing.Any],
241+
):
242+
"""Add object proxy to connection object."""
243+
base_cursor_factory = kwargs.pop("cursor_factory", None)
244+
new_factory_kwargs = {"db_api": self}
245+
if base_cursor_factory:
246+
new_factory_kwargs["base_factory"] = base_cursor_factory
247+
kwargs["cursor_factory"] = _new_cursor_async_factory(
248+
**new_factory_kwargs
249+
)
250+
connection = await connect_method(*args, **kwargs)
251+
self.get_connection_attributes(connection)
252+
return connection
253+
254+
207255
class CursorTracer(dbapi.CursorTracer):
208256
def get_operation_name(self, cursor, args):
209257
if not args:
@@ -259,3 +307,36 @@ def callproc(self, *args, **kwargs):
259307
)
260308

261309
return TracedCursorFactory
310+
311+
312+
def _new_cursor_async_factory(
313+
db_api=None, base_factory=None, tracer_provider=None
314+
):
315+
if not db_api:
316+
db_api = DatabaseApiAsyncIntegration(
317+
__name__,
318+
Psycopg3Instrumentor._DATABASE_SYSTEM,
319+
connection_attributes=Psycopg3Instrumentor._CONNECTION_ATTRIBUTES,
320+
version=__version__,
321+
tracer_provider=tracer_provider,
322+
)
323+
base_factory = base_factory or pg_async_cursor
324+
_cursor_tracer = CursorTracer(db_api)
325+
326+
class TracedCursorAsyncFactory(base_factory):
327+
async def execute(self, *args, **kwargs):
328+
return await _cursor_tracer.traced_execution(
329+
self, super().execute, *args, **kwargs
330+
)
331+
332+
async def executemany(self, *args, **kwargs):
333+
return await _cursor_tracer.traced_execution(
334+
self, super().executemany, *args, **kwargs
335+
)
336+
337+
async def callproc(self, *args, **kwargs):
338+
return await _cursor_tracer.traced_execution(
339+
self, super().callproc, *args, **kwargs
340+
)
341+
342+
return TracedCursorAsyncFactory

0 commit comments

Comments
 (0)