-
Notifications
You must be signed in to change notification settings - Fork 215
fix: Check span recording state before ending process span #1513
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
base: main
Are you sure you want to change the base?
fix: Check span recording state before ending process span #1513
Conversation
Adds a check to verify the subscribe span is still recording before attempting to end the process span in the __exit__ method. This prevents errors when the span has already been terminated or is no longer active. This fix ensures proper cleanup of OpenTelemetry spans during subscriber callback execution and prevents potential errors in the context manager exit flow.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Thanks for putting this together! |
Thanks for the review, I'll be away next week, but I'll try to fix the flaky test the week after that 👍 |
Summary
This PR adds a check to verify that the subscribe span is still recording before attempting to end the process span in the
__exit__method of theSubscribeOpenTelemetrycontext manager.Problem
When the OpenTelemetry subscriber context manager exits, it may attempt to end the process span even when the parent subscribe span has already been terminated or is no longer in a recording state. This leads to log errors like:
/.venv/lib/python3.12/site-packages/opentelemetry/sdk/trace/init.py#L943
This happens because using
pubsubinstrumentation theend()method may be called twice:On
ackpython-pubsub/google/cloud/pubsub_v1/subscriber/message.py
Line 276 in e6294a1
Then on
__exit__python-pubsub/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py
Lines 174 to 175 in e6294a1
Solution
Added a condition
self._subscribe_span.is_recording()to the existing check before callingself.end_process_span():Testing
Impact
This is a defensive code change that prevents edge case errors in span management. It should not affect normal operation but provides better handling when spans are terminated in non-standard flows.