-
Notifications
You must be signed in to change notification settings - Fork 931
Fix psycopg2 instrument_connection AttributeError #3043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
tammy-baylis-swi
wants to merge
31
commits into
open-telemetry:main
from
tammy-baylis-swi:fix-psycopg2-instrument-connection
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
69d1ea3
Fix psycopg2 instrument_connection
tammy-baylis-swi c6181ef
Changelog
tammy-baylis-swi 72d41e5
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi ea8d73d
Fix unit test
tammy-baylis-swi a0f74ca
Rm psycopg2 wrapt dep
tammy-baylis-swi a0c9940
Fix docs and comments
tammy-baylis-swi b591b0a
Add and update tests
tammy-baylis-swi 59fa855
Rm enable_commenter fixes for a different pr
tammy-baylis-swi dd7e3ea
Revert doc reqs
tammy-baylis-swi 0b3f273
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 40f077b
Update readme
tammy-baylis-swi 5a099d5
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi a7f969d
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 0f7baec
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi a2fa981
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi e8d47bf
Rm instrument_connection is-instrumented check; use dict to store cnx
tammy-baylis-swi 8a316a4
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi b3b3888
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 238746e
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 6f67b33
Rm cursor_factory restore at uninstrument_connection
tammy-baylis-swi b2edf01
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 86dd604
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 9ef7b50
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 7f3632a
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 38b9d09
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 680b0f5
Changelog
tammy-baylis-swi 6eff789
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi 0102cee
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi b6a3af2
Changelog
tammy-baylis-swi b065a38
Fix docstring
tammy-baylis-swi 8abf974
Merge branch 'main' into fix-psycopg2-instrument-connection
tammy-baylis-swi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,6 +151,7 @@ | |
| ) | ||
| 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 ( | ||
|
|
@@ -161,7 +162,6 @@ | |
| from opentelemetry.instrumentation.psycopg2.version import __version__ | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
| _OTEL_CURSOR_FACTORY_KEY = "_otel_orig_cursor_factory" | ||
|
|
||
|
|
||
| class Psycopg2Instrumentor(BaseInstrumentor): | ||
|
|
@@ -193,8 +193,8 @@ def instrumentation_dependencies(self) -> Collection[str]: | |
| return _instruments_any | ||
|
|
||
| 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) | ||
|
|
@@ -222,7 +222,10 @@ def _uninstrument(self, **kwargs): | |
|
|
||
| # 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): | ||
| def instrument_connection( | ||
| connection, | ||
| tracer_provider: typing.Optional[trace_api.TracerProvider] = None, | ||
| ): | ||
| """Enable instrumentation in a psycopg2 connection. | ||
|
|
||
| Args: | ||
|
|
@@ -235,31 +238,19 @@ def instrument_connection(connection, tracer_provider=None): | |
| Returns: | ||
| An instrumented psycopg2 connection object. | ||
| """ | ||
|
|
||
| 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: | ||
| _logger.warning( | ||
| "Attempting to instrument Psycopg connection while already instrumented" | ||
| ) | ||
| # TODO Add check for attempt to instrument a connection when already instrumented | ||
| # https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138 | ||
| connection.cursor_factory = _new_cursor_factory( | ||
| base_factory=connection.cursor_factory, | ||
| tracer_provider=tracer_provider, | ||
| ) | ||
| 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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should leave this as a no-op. If we used a class-level store (eg What do you think @tammy-baylis-swi? |
||
| connection.cursor_factory = getattr( | ||
| connection, _OTEL_CURSOR_FACTORY_KEY, None | ||
| ) | ||
|
|
||
| # TODO Add check for attempt to instrument a connection when already instrumented | ||
| # https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138 | ||
| return connection | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.