- Notifications
You must be signed in to change notification settings - Fork 809
Fix psycopg2 instrument issue #2795
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
Open
qiuge615 wants to merge 11 commits into open-telemetry:main Choose a base branch from fidelity-contributions:fix-Psycopg2Instrumentor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits Select commit Hold shift + click to select a range
bc77e8f Fix psycopg2 intrusment issue
qiuge615 224e03a Update psycopg2 connection instrument logic
qiuge615 72ab2f3 Merge branch 'main' into fix-Psycopg2Instrumentor
qiuge615 092b8ba Addd changelog for psycopg2 instrument issue fix
qiuge615 6a8046a Merge branch 'main' into fix-Psycopg2Instrumentor
qiuge615 4614565 Update test case to reproduce psycopg2 connection instrument issue
qiuge615 dbd4483 Merge branch 'main' into fix-Psycopg2Instrumentor
qiuge615 4d4ca41 Merge branch 'main' into fix-Psycopg2Instrumentor
qiuge615 87d74c5 Add assert for _is_instrumented_by_opentelemetry in connection object
qiuge615 17f23e3 Merge branch 'main' into fix-Psycopg2Instrumentor
qiuge615 30cff63 Add test case for psycopg2 duplicated instrumentation.
qiuge615 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 |
|---|---|---|
| | @@ -110,6 +110,7 @@ | |
| cursor as pg_cursor, # pylint: disable=no-name-in-module | ||
| ) | ||
| from psycopg2.sql import Composed # pylint: disable=no-name-in-module | ||
| from wrapt import ObjectProxy | ||
| | ||
| from opentelemetry.instrumentation import dbapi | ||
| from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
| | @@ -158,24 +159,25 @@ 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=None): | ||
| if isinstance(connection, ObjectProxy): | ||
| Contributor 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 still not sure this solves the original issue (we still don't know WHY the error is being generated when we are trying to access | ||
| _logger.warning( | ||
| "Attempting to instrument Psycopg connection while already instrumented" | ||
| ) | ||
| return connection | ||
| return connection | ||
| | ||
| db_integration = DatabaseApiIntegration( | ||
| __name__, | ||
| self._DATABASE_SYSTEM, | ||
| connection_attributes=self._CONNECTION_ATTRIBUTES, | ||
| version=__version__, | ||
| tracer_provider=tracer_provider, | ||
| ) | ||
| db_integration.get_connection_attributes(connection) | ||
| db_integration.cursor_factory = _new_cursor_factory( | ||
| tracer_provider=tracer_provider | ||
| ) | ||
| return dbapi.get_traced_connection_proxy(connection, db_integration) | ||
| | ||
| # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql | ||
| @staticmethod | ||
| | @@ -184,7 +186,7 @@ def uninstrument_connection(connection): | |
| connection, _OTEL_CURSOR_FACTORY_KEY, None | ||
| ) | ||
| | ||
| return connection | ||
| return dbapi.uninstrument_connection(connection) | ||
| | ||
| | ||
| # TODO(owais): check if core dbapi can do this for all dbapi implementations e.g, pymysql and mysql | ||
| | ||
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
Oops, something went wrong.
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.