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

Check for None result in gRPC (#3380) #3381

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kevmodo
Copy link

@kevmodo kevmodo commented Mar 21, 2025

Description

This is a fix for #3380 that checks explicitly for a None result rather than evaluating truthiness. PubSub results with zero messages evaluate to False, causing span.end() to be called twice.

Fixes #3380

Type of change

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

How Has This Been Tested?

  • Ran Tox
  • Ran locally against an empty PubSub queue and no longer received warning messages, but spans correctly started and ended.
  • Ran against queue with messages and behavior was as expected (spans correctly started and ended, as before)

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

Copy link

linux-foundation-easycla bot commented Mar 21, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@kevmodo kevmodo requested a review from a team as a code owner March 21, 2025 07:39
@xrmx
Copy link
Contributor

xrmx commented Mar 21, 2025

Thanks for the PR, could you please write a test for this to avoid regressions in the future?

@kevmodo
Copy link
Author

kevmodo commented Mar 25, 2025

Thanks for the PR, could you please write a test for this to avoid regressions in the future?

That was an adventure! Added a test that counts span.end() invocations.

@@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-grpc` Check for None result in gRPC
Copy link
Contributor

Choose a reason for hiding this comment

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

This need to be moved to the unreleased version

Comment on lines +282 to +295
def counting_span_end(original_end):
def wrapper(*args, **kwargs):
nonlocal span_end_count
span_end_count += 1
return original_end(*args, **kwargs)

return wrapper

def patched_start_span(*args, **kwargs):
span = original_start_span(*args, **kwargs)
span.end = counting_span_end(span.end)
return span

tracer.start_span = patched_start_span
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried patching the end method of Span instead? something like with mock.patch.object(Span, "end") as span_end_mock: and then self.assertEqual(span_end_mock.call_count, 1)?

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.

GRPC traces called twice when result evaluates to false
2 participants