Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69d1ea3
Fix psycopg2 instrument_connection
tammy-baylis-swi Nov 23, 2024
c6181ef
Changelog
tammy-baylis-swi Nov 23, 2024
72d41e5
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 4, 2024
ea8d73d
Fix unit test
tammy-baylis-swi Dec 5, 2024
a0f74ca
Rm psycopg2 wrapt dep
tammy-baylis-swi Dec 5, 2024
a0c9940
Fix docs and comments
tammy-baylis-swi Dec 5, 2024
b591b0a
Add and update tests
tammy-baylis-swi Dec 5, 2024
59fa855
Rm enable_commenter fixes for a different pr
tammy-baylis-swi Dec 5, 2024
dd7e3ea
Revert doc reqs
tammy-baylis-swi Dec 5, 2024
0b3f273
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 5, 2024
40f077b
Update readme
tammy-baylis-swi Dec 6, 2024
5a099d5
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 9, 2024
a7f969d
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 11, 2024
0f7baec
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 19, 2024
a2fa981
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Dec 20, 2024
e8d47bf
Rm instrument_connection is-instrumented check; use dict to store cnx
tammy-baylis-swi Dec 21, 2024
8a316a4
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 2, 2025
b3b3888
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 7, 2025
238746e
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 8, 2025
6f67b33
Rm cursor_factory restore at uninstrument_connection
tammy-baylis-swi Jan 10, 2025
b2edf01
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 10, 2025
86dd604
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 13, 2025
9ef7b50
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 14, 2025
7f3632a
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Jan 15, 2025
38b9d09
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi Feb 27, 2025
680b0f5
Changelog
tammy-baylis-swi Feb 27, 2025
6eff789
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi May 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2816](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2816))
- `opentelemetry-instrumentation-sqlalchemy`: Fix a remaining memory leak in EngineTracer
([#3053](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3053))
- `opentelemetry-instrumentation-psycopg2`: fix AttributeError at `instrument_connection`
([#3043](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3043))

### Breaking changes

Expand Down
1 change: 1 addition & 0 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mysqlclient~=2.1.1
openai >= 1.26.0
psutil>=5
psycopg~=3.1.17
psycopg2~=2.9.9
pika>=0.12.0
pymongo~=4.6.3
PyMySQL~=1.1.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OpenTelemetry Psycopg Instrumentation
OpenTelemetry Psycopg2 Instrumentation
=====================================

|pypi|
Expand All @@ -16,6 +16,6 @@ Installation

References
----------
* `OpenTelemetry Psycopg Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/psycopg2/psycopg2.html>`_
* `OpenTelemetry Psycopg2 Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/psycopg2/psycopg2.html>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
* `OpenTelemetry Python Examples <https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples>`_
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.

"""
The integration with PostgreSQL supports the `Psycopg`_ library, it can be enabled by
The integration with PostgreSQL supports the `Psycopg2`_ library, it can be enabled by
using ``Psycopg2Instrumentor``.

.. _Psycopg: http://initd.org/psycopg/
.. _Psycopg2: https://www.psycopg.org/docs/

SQLCOMMENTER
*****************************************
Expand Down Expand Up @@ -111,13 +111,13 @@
)
from psycopg2.sql import Composed # pylint: disable=no-name-in-module

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation import dbapi
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.psycopg2.package import _instruments
from opentelemetry.instrumentation.psycopg2.version import __version__

_logger = logging.getLogger(__name__)
_OTEL_CURSOR_FACTORY_KEY = "_otel_orig_cursor_factory"


class Psycopg2Instrumentor(BaseInstrumentor):
Expand All @@ -130,12 +130,14 @@ class Psycopg2Instrumentor(BaseInstrumentor):

_DATABASE_SYSTEM = "postgresql"

_otel_cursor_factory = None

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
"""Integrate with PostgreSQL Psycopg library.
Psycopg: http://initd.org/psycopg/
"""Integrate with PostgreSQL Psycopg2 library.
Psycopg2: https://www.psycopg.org/docs/
"""
tracer_provider = kwargs.get("tracer_provider")
enable_sqlcommenter = kwargs.get("enable_commenter", False)
Expand All @@ -158,32 +160,39 @@ def _uninstrument(self, **kwargs):
dbapi.unwrap_connect(psycopg2, "connect")

# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
@staticmethod
def instrument_connection(connection, tracer_provider=None):
if not hasattr(connection, "_is_instrumented_by_opentelemetry"):
connection._is_instrumented_by_opentelemetry = False

if not connection._is_instrumented_by_opentelemetry:
setattr(
connection, _OTEL_CURSOR_FACTORY_KEY, connection.cursor_factory
)
connection.cursor_factory = _new_cursor_factory(
tracer_provider=tracer_provider
)
connection._is_instrumented_by_opentelemetry = True
else:
def instrument_connection(
self,
connection,
tracer_provider: typing.Optional[trace_api.TracerProvider] = None,
enable_commenter: bool = False,
commenter_options: dict = None,
):
if self._is_instrumented_by_opentelemetry:
# _instrument (via BaseInstrumentor) or instrument_connection (this)
# was already called
_logger.warning(
"Attempting to instrument Psycopg connection while already instrumented"
"Attempting to instrument Psycopg2 connection while already instrumented"
)
self._is_instrumented_by_opentelemetry = True
return connection

# Save cursor_factory at instrumentor level because
# psycopg2 connection type does not allow arbitrary attrs
self._otel_cursor_factory = connection.cursor_factory
connection.cursor_factory = _new_cursor_factory(
base_factory=connection.cursor_factory,
tracer_provider=tracer_provider,
enable_commenter=enable_commenter,
commenter_options=commenter_options,
)
self._is_instrumented_by_opentelemetry = True

return connection

# TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql
@staticmethod
def uninstrument_connection(connection):
connection.cursor_factory = getattr(
connection, _OTEL_CURSOR_FACTORY_KEY, None
)

def uninstrument_connection(self, connection):
self._is_instrumented_by_opentelemetry = False
connection.cursor_factory = self._otel_cursor_factory
return connection


Expand Down Expand Up @@ -231,14 +240,23 @@ def get_statement(self, cursor, args):
return statement


def _new_cursor_factory(db_api=None, base_factory=None, tracer_provider=None):
def _new_cursor_factory(
db_api: dbapi.DatabaseApiIntegration = None,
base_factory: pg_cursor = None,
tracer_provider: typing.Optional[trace_api.TracerProvider] = None,
enable_commenter: bool = False,
commenter_options: dict = None,
):
if not db_api:
db_api = DatabaseApiIntegration(
__name__,
Psycopg2Instrumentor._DATABASE_SYSTEM,
connection_attributes=Psycopg2Instrumentor._CONNECTION_ATTRIBUTES,
version=__version__,
tracer_provider=tracer_provider,
enable_commenter=enable_commenter,
commenter_options=commenter_options,
connect_module=psycopg2,
)

base_factory = base_factory or pg_cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def test_instrument_connection_with_instrument(self):
self.assertEqual(len(spans_list), 0)

Psycopg2Instrumentor().instrument()
cnx = psycopg2.connect(database="test")
cnx = Psycopg2Instrumentor().instrument_connection(cnx)
cursor = cnx.cursor()
cursor.execute(query)
Expand Down
Loading