Skip to content

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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

([#3380](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3381))
- `opentelemetry-instrumentation-redis` Add missing entry in doc string for `def _instrument`
([#3247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3247))
- `opentelemetry-instrumentation-botocore` sns-extension: Change destination name attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _intercept(self, request, metadata, client_info, invoker):
span.record_exception(exc)
raise exc
finally:
if not result:
if result is None:
span.end()
return self._trace_result(span, rpc_info, result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,47 @@ def test_error_stream_stream(self):
trace.StatusCode.ERROR,
)

def test_client_interceptor_falsy_response(
self,
): # pylint: disable=no-self-use
"""ensure that client interceptor closes the span only once even if the response is falsy."""

span_end_count = 0
tracer_provider, _exporter = self.create_tracer_provider()
tracer = tracer_provider.get_tracer(__name__)
original_start_span = tracer.start_span

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
Comment on lines +282 to +295
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)?

interceptor = OpenTelemetryClientInterceptor(tracer)

def invoker(_request, _metadata):
return {}

request = Request(client_id=1, request_data="data")
interceptor.intercept_unary(
request,
{},
_UnaryClientInfo(
full_method="/GRPCTestServer/SimpleMethod",
timeout=None,
),
invoker=invoker,
)
assert span_end_count == 1

def test_client_interceptor_trace_context_propagation(
self,
): # pylint: disable=no-self-use
Expand Down