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 17 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

"Breaking bugfix": this includes removal of the @staticmethod decorators from this instrumentor's instrument_/uninstrument_connection methods because the following approach sets/gets instrumentor instance-level attributes. The methods were made static in #549. Not sure if it helps my argument but I'll note that the instrument_connection methods for the instrumentors of aiopg, DB-API integration, mysql-connector, and the util-http are not static (anymore?) either. Also, the existing unit tests call it as Psycopg2Instrumentor().instrument_connection(cnx) anyway and the documentation does not provide any examples.

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. To preserve this behavior, replaces with a set to instrumentor instance-level _is_instrumented_by_opentelemetry as defined by inherited BaseInstrumentor (introduced in this PR). We only need to set for instrument_connection and not _instrument because the latter's caller, BaseInstrumentor.instrument, does it.

Similar AttributeError also happened with setattr for connection._otel_orig_cursor_factory so instead we store the value at instrumentor attribute _otel_cursor_factory. Since the call to instrument_connection is done at the connection level I think it's ok to store at this level instead of one more granular like the custom connection proxy class.

Also fixes the documentation a bit.

Fixes #2522

Alternative fix

An alternative fix that could keep these methods static is in this other PR: #2795. However, that approach removes connection.cursor_factory parts that might be needed for the fix in #246 to support JSON and JSONB types of DB columns. It also stops using _is_instrumented_by_opentelemetry. See also PR comment.

Please let me know what you think! I'm fine with whichever approach.

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 <riccardo.magliocchetti@gmail.com>
@tammy-baylis-swi tammy-baylis-swi requested a review from xrmx December 7, 2024 01:57
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