Skip to content
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

Fix psycopg2 instrument_connection AttributeError #3043

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Nov 23, 2024

Description

Removes the attempt to set attribute connection._is_instrumented_by_opentelemetry because psycopg2.extensions.connection does not allow setting of extra attributes and we get AttributeError. Similar AttributeError also happened with setattr for connection._otel_orig_cursor_factory so also removed.

Adds todos to re-add checks for connection-already-being-instrumented later, as discussed:

# TODO Add check for attempt to instrument a connection when already instrumented
#      https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3138

Fixes #2522

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • tox -e py312-test-instrumentation-psycopg2
  • Instrumented db client app that calls Psycopg2Instrumentor.instrument_connection
  • Instrumented db client app that calls Psycopg2Instrumentor.instrument

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@tammy-baylis-swi tammy-baylis-swi changed the title WIP/PoC Fix psycopg2 instrument_connection Fix psycopg2 instrument_connection Dec 5, 2024
@tammy-baylis-swi tammy-baylis-swi force-pushed the fix-psycopg2-instrument-connection branch from 354d085 to 7871734 Compare December 5, 2024 00:22
@tammy-baylis-swi tammy-baylis-swi changed the title Fix psycopg2 instrument_connection Fix psycopg2 instrument_connection AttributeError Dec 5, 2024
@tammy-baylis-swi tammy-baylis-swi force-pushed the fix-psycopg2-instrument-connection branch from 7871734 to fa46143 Compare December 5, 2024 00:30
@tammy-baylis-swi tammy-baylis-swi force-pushed the fix-psycopg2-instrument-connection branch from fa46143 to a0c9940 Compare December 5, 2024 00:37
@tammy-baylis-swi tammy-baylis-swi force-pushed the fix-psycopg2-instrument-connection branch from 5bd1e94 to b591b0a Compare December 5, 2024 21:18
Co-authored-by: Riccardo Magliocchetti <[email protected]>
@tammy-baylis-swi tammy-baylis-swi requested a review from xrmx December 7, 2024 01:57

# Save cursor_factory in instrumentor map because
# psycopg2 connection type does not allow arbitrary attrs
self._otel_cursor_factories[connection] = connection.cursor_factory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the set of cursor factories for anything? Don't we need to check if the factory is in the set already or is that part of the TODO? I'd rather have the whole duplicate functionality there or you can leave it for a different PR but remove the set for now since it doesn't seem to be doing anything.

@tammy-baylis-swi tammy-baylis-swi force-pushed the fix-psycopg2-instrument-connection branch from 0a1360d to 680b0f5 Compare February 27, 2025 17:38
Copy link
Contributor

@xrmx xrmx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the name of the tests very confusing but yeah, better double spans than a crash I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants