Skip to content

Fix psycopg2 (un)instrument_connection to use weakref, not mutate connection object#4257

Merged
xrmx merged 11 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:psycopg2-connections-weakref-not-mutate
Mar 3, 2026
Merged

Fix psycopg2 (un)instrument_connection to use weakref, not mutate connection object#4257
xrmx merged 11 commits intoopen-telemetry:mainfrom
tammy-baylis-swi:psycopg2-connections-weakref-not-mutate

Conversation

@tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Feb 24, 2026

Description

Changes psycopg2 instrumentation's instrument_connection and uninstrument_connection functions to use a WeakKeyDictionary store and retrieve original connection objects (respectively). Removes the attempts to set attributes on the connection objects themselves because psycopg2.extensions.connection does not allow setting of extra attributes and we get AttributeError.

This is an alternative/replacement for #3043

Fixes #2522 and #3138

Suggested in this PR's comment!

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-opentelemetry-instrumentation-psycopg2-0

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 marked this pull request as ready for review February 24, 2026 23:17
@tammy-baylis-swi tammy-baylis-swi requested a review from a team as a code owner February 24, 2026 23:17
Copy link

@JWinermaSplunk JWinermaSplunk left a comment

Choose a reason for hiding this comment

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

I am not aware of the previous PR, but this LGTM!

Copy link
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @tammy-baylis-swi 👍🏻

@tammy-baylis-swi tammy-baylis-swi moved this from Approved PRs to Ready for review in Python PR digest Feb 26, 2026
@tammy-baylis-swi tammy-baylis-swi moved this from Ready for review to Easy to review / merge / close in Python PR digest Feb 26, 2026
@xrmx xrmx moved this from Easy to review / merge / close to Approved PRs in Python PR digest Feb 27, 2026
@xrmx xrmx requested a review from herin049 March 2, 2026 11:01
@herin049
Copy link
Contributor

herin049 commented Mar 2, 2026

LGTM!

@xrmx xrmx enabled auto-merge (squash) March 3, 2026 08:32
@xrmx xrmx merged commit c907282 into open-telemetry:main Mar 3, 2026
771 checks passed
@github-project-automation github-project-automation bot moved this from Approved PRs to Done in Python PR digest Mar 3, 2026
@tammy-baylis-swi tammy-baylis-swi deleted the psycopg2-connections-weakref-not-mutate branch March 3, 2026 17:26
shuningc pushed a commit to shuningc/opentelemetry-python-contrib that referenced this pull request Mar 5, 2026
…nection object (open-telemetry#4257)

* Fix psycopg2 (un)instrument_connection to use weakref, not mutate object

* Changelog

* conditional import for docs types

* Lint

* SImplify test

* Simplify

* Fix docs

---------

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

5 participants