diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b45f3cfe5..4c60e3a3a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py index a2603bb6be..d3ad28521a 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py @@ -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 @@ -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: @@ -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 diff --git a/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py b/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py index 0075d3873c..0f30464744 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py @@ -18,7 +18,7 @@ ) 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, @@ -26,6 +26,7 @@ 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 @@ -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)