Skip to content

Conversation

@ibash
Copy link
Contributor

@ibash ibash commented Mar 4, 2025

Description

This copies the traced_execution of AsyncCursorTracer except query_method is awaited within the span.

Fixes #2486

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?

I ran this locally by making the corresponding changes in my site-packages folder. Then I checked logfire (what we're using for opentelemetry) to confirm that sql queries now had non-zero timings.

Also ran the tox command to run tests.

Happy to build and test the built package if you tell me how to build!

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 4, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@ibash
Copy link
Contributor Author

ibash commented Mar 5, 2025

@federicobond would love a review when you get a chance!

Copy link

@emilingerslev emilingerslev left a comment

Choose a reason for hiding this comment

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

I've validated that this indeed solves the problem. Only concern here is minor; The override of traced_execution changes the signature from sync to async, effectively also changing the return type. Another option is to extend CursorTracer with a traced_execution_async method, keeping it all in one class, but supporting both.

But as I said this works ✅

This copies the traced_execution of AsyncCursorTracer except
query_method is awaited within the span.

Fixes open-telemetry#2486
@ibash ibash force-pushed the ibash/async_tracing branch from ecea87a to 71f50a7 Compare March 25, 2025 18:20
@xrmx
Copy link
Contributor

xrmx commented Mar 27, 2025

I've validated that this indeed solves the problem. Only concern here is minor; The override of traced_execution changes the signature from sync to async, effectively also changing the return type. Another option is to extend CursorTracer with a traced_execution_async method, keeping it all in one class, but supporting both.

Thanks for testing. Not sure I follow the return type concern, AFAIU the users of that _cursor_tracer are awaiting it so the fact it was not an async function was the issue?

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 think we are missing tests to avoid regressions in the future

@ibash
Copy link
Contributor Author

ibash commented Apr 7, 2025

@xrmx sorry for the delay here -- added a regression test. Please let me know if there's a better way to assert this behavior.

@ibash ibash requested a review from xrmx April 7, 2025 17:16
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Apr 9, 2025
@ibash ibash requested review from emilingerslev and xrmx April 16, 2025 22:21
@jwilm
Copy link

jwilm commented May 19, 2025

Thanks @ibash for this PR and @xrmx for your reviews. I'm eager to see this PR land and published; is there anything I can do to help?

@ibash
Copy link
Contributor Author

ibash commented May 19, 2025

@jwilm not ideal but I did publish the patched version to github, in our requirements.txt we use:

opentelemetry-instrumentation-fastapi==0.52b1
opentelemetry-instrumentation-psycopg @ https://github.com/ibash/opentelemetry-python-contrib/releases/download/v52b1/opentelemetry_instrumentation_psycopg-0.52b1-py3-none-any.whl

Adds a test to check that the async tracer is actually awaiting
cursor.execute.

On my machine, with the bug present, the duration is 14000ns. With the
bug patched the the duration is orders of magnitude larger.
@ibash ibash force-pushed the ibash/async_tracing branch from 2fcd49d to 0d8cd74 Compare June 2, 2025 18:03
@ibash
Copy link
Contributor Author

ibash commented Jun 2, 2025

@xrmx fixed the formatting issue -- any chance this can get reviewed / merged?

@Kludex
Copy link
Member

Kludex commented Jul 10, 2025

I've validated that this indeed solves the problem. Only concern here is minor; The override of traced_execution changes the signature from sync to async, effectively also changing the return type. Another option is to extend CursorTracer with a traced_execution_async method, keeping it all in one class, but supporting both.

Thanks for testing. Not sure I follow the return type concern, AFAIU the users of that _cursor_tracer are awaiting it so the fact it was not an async function was the issue?

When you subclass, you need to maintain the method's signature (Liskov Substitution Principle).

@Kludex
Copy link
Member

Kludex commented Jul 10, 2025

Thanks for using Logfire! ❤️

@ibash
Copy link
Contributor Author

ibash commented Jul 21, 2025

@Kludex @xrmx updated this to have traced_execution_async instead

@aabmass aabmass enabled auto-merge (squash) August 13, 2025 14:10
@aabmass aabmass merged commit 29dfd56 into open-telemetry:main Aug 13, 2025
624 of 625 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in @xrmx's Python PR digest Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-psycopg claims to work for async queries, but doesn't record time

8 participants