Skip to content

fixed: asyncpg connection params are a namedtuple #3253

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

Merged
merged 3 commits into from
Feb 14, 2025

Conversation

privatwolke
Copy link
Contributor

@privatwolke privatwolke commented Feb 10, 2025

Follow-up on the apparently abbandonned #2114.
Closes #2114

Description

The asyncpg instrumentation attempts to fall back on using the database name as the span name in case the first argument to the instrumented method is falsey.

Any attempt to do so results in errors simliar to this (truncated traceback):

Traceback (most recent call last):
  File "/opt/venv/lib/python3.12/site-packages/asyncpg/pool.py", line 597, in fetch
    return await con.fetch(
           ^^^^^^^^^^^^^^^^
  File "/opt/venv/lib/python3.12/site-packages/opentelemetry/instrumentation/asyncpg/__init__.py", line 154, in _do_execute
    name = args[0] if args[0] else params.get("database", "postgresql")
                                   ^^^^^^^^^^
AttributeError: 'ConnectionParameters' object has no attribute 'get'

This has probably never worked since asyncpg defines the _params attribute as an instance of ConnectionParams
(https://github.com/MagicStack/asyncpg/blob/master/asyncpg/connection.py#L62) which is a NamedTuple instance and thus don't define get. The proper way of safely accessing properties on a NamedTuple is using getattr.

The only case that I've actually found which triggers this branch is if the supplied query is an empty string. This is something that causes an AttributeError for Connection.execute but is fine for fetch(), fetchval(), fetchrow() and executemany().

The tests have been expanded to check these cases. Also, more status code validation has been added where it was missing.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • docker-tests for asyncpg instrumentation extended and passed

Does This PR Require a Core Repo Change?

  • 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

@privatwolke privatwolke requested a review from a team as a code owner February 10, 2025 14:00
Copy link

linux-foundation-easycla bot commented Feb 10, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: privatwolke / name: Stephan Klein (2cb3fcf)
  • ✅ login: xrmx / name: Riccardo Magliocchetti (5d33f72, 5e8e58a)

@tammy-baylis-swi
Copy link
Contributor

Thanks @privatwolke for revisiting this especially with the added test coverage.

To get this check to pass, please could you run tox -e ruff on your local then commit the changes.

@privatwolke
Copy link
Contributor Author

@tammy-baylis-swi - thanks for the quick review! I've updated the PR with the suggested changes and also added additional tests for the Cursor instrumentation. Code formatting should now pass as well ✨

@privatwolke privatwolke force-pushed the patch-1 branch 2 times, most recently from d9c87a1 to 0059e86 Compare February 11, 2025 11:40
@tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi - thanks for the quick review! I've updated the PR with the suggested changes and also added additional tests for the Cursor instrumentation. Code formatting should now pass as well ✨

Thanks for those! I've added more suggestions and just triggered another ci/cd run. Please have another look.

@privatwolke privatwolke force-pushed the patch-1 branch 3 times, most recently from 2aeb4ad to 8038c76 Compare February 11, 2025 22:34
Follow-up on the apparently abbandonned open-telemetry#2114.

The asyncpg instrumentation attempts to fall back on using the database
name as the span name in case the first argument to the instrumented
method is falsey.

This has probably never worked since asyncpg defines the `_params`
attribute as an instance of `ConnectionParams`
(https://github.com/MagicStack/asyncpg/blob/master/asyncpg/connection.py#L62)
which is a NamedTuple instance and thus don't define `get`. The proper
way of safely accessing properties on a NamedTuple is using `getattr`.

The only case that I've actually found which triggers this branch is if
the supplied query is an empty string. This is something that causes an
`AttributeError` for `Connection.execute` but is fine for `fetch()`,
`fetchval()`, `fetchrow()` and `executemany()`.

The tests have been expanded to check these cases. Also, more status
code validation has been added where it was missing.
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi left a comment

Choose a reason for hiding this comment

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

Thank you @privatwolke ! Lgtm. We'll need more reviews from a repo Maintainer; feel free to bring this PR up in Slack and/or the Python SIG meeting (every Thurs 9am PT)

@xrmx xrmx enabled auto-merge (squash) February 13, 2025 09:21
@privatwolke
Copy link
Contributor Author

privatwolke commented Feb 14, 2025

@xrmx Thanks for approving this, but I think something went wrong with the automerge. Should I rebase my branch on the base branch?

@xrmx xrmx merged commit 1623dc0 into open-telemetry:main Feb 14, 2025
694 checks passed
@privatwolke privatwolke deleted the patch-1 branch February 14, 2025 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants