Skip to content

Commit

Permalink
Respect suppress instrumentation key in gRPC client (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanSiu1995 authored Jul 14, 2021
1 parent df0ca3b commit 3a1746a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updating dependency for opentelemetry api/sdk packages to support major version instead
of pinning to specific versions.
([#567](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/567))
- `opentelemetry-instrumentation-grpc` Respect the suppress instrumentation in gRPC client instrumentor
([#559](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/559))
- `opentelemetry-instrumentation-grpc` Fixed asynchonous unary call traces
([#536](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/536))
- `opentelemetry-sdk-extension-aws` Update AWS entry points to match spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

import grpc

from opentelemetry import trace
from opentelemetry import context, trace
from opentelemetry.instrumentation.grpc import grpcext
from opentelemetry.instrumentation.grpc._utilities import RpcInfo
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import inject
from opentelemetry.propagators.textmap import Setter
from opentelemetry.semconv.trace import SpanAttributes
Expand Down Expand Up @@ -101,6 +102,9 @@ def _trace_result(self, span, rpc_info, result):
return result

def _intercept(self, request, metadata, client_info, invoker):
if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
return invoker(request, metadata)

if not metadata:
mutable_metadata = OrderedDict()
else:
Expand Down Expand Up @@ -184,6 +188,9 @@ def _intercept_server_stream(
def intercept_stream(
self, request_or_iterator, metadata, client_info, invoker
):
if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY):
return invoker(request_or_iterator, metadata)

if client_info.is_server_stream:
return self._intercept_server_stream(
request_or_iterator, metadata, client_info, invoker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
)

import opentelemetry.instrumentation.grpc
from opentelemetry import trace
from opentelemetry import context, trace
from opentelemetry.instrumentation.grpc import GrpcInstrumentorClient
from opentelemetry.instrumentation.grpc._client import (
OpenTelemetryClientInterceptor,
)
from opentelemetry.instrumentation.grpc.grpcext._interceptor import (
_UnaryClientInfo,
)
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.test.mock_textmap import MockTextMapPropagator
Expand Down Expand Up @@ -301,3 +302,47 @@ def invoker(request, metadata):

finally:
set_global_textmap(previous_propagator)

def test_unary_unary_with_suppress_key(self):
token = context.attach(
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
simple_method(self._stub)
spans = self.memory_exporter.get_finished_spans()
finally:
context.detach(token)
self.assertEqual(len(spans), 0)

def test_unary_stream_with_suppress_key(self):
token = context.attach(
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
server_streaming_method(self._stub)
spans = self.memory_exporter.get_finished_spans()
finally:
context.detach(token)
self.assertEqual(len(spans), 0)

def test_stream_unary_with_suppress_key(self):
token = context.attach(
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
client_streaming_method(self._stub)
spans = self.memory_exporter.get_finished_spans()
finally:
context.detach(token)
self.assertEqual(len(spans), 0)

def test_stream_stream_with_suppress_key(self):
token = context.attach(
context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)
)
try:
bidirectional_streaming_method(self._stub)
spans = self.memory_exporter.get_finished_spans()
finally:
context.detach(token)
self.assertEqual(len(spans), 0)

0 comments on commit 3a1746a

Please sign in to comment.