From afe84d2a0cde2007c7e303ff281e47ef16e62833 Mon Sep 17 00:00:00 2001 From: Ryan SIU Date: Thu, 1 Jul 2021 15:04:18 +0800 Subject: [PATCH 1/3] Respect suppress instrumentation key in gRPC client --- .../instrumentation/grpc/_client.py | 9 +++- .../tests/test_client_interceptor.py | 48 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) 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 7d3d22e714..3fc1fe7aa3 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 @@ -122,6 +123,9 @@ def _start_guarded_span(self, *args, **kwargs): return _GuardedSpan(self._start_span(*args, **kwargs)) def intercept_unary(self, request, metadata, client_info, invoker): + if context.get_value( _SUPPRESS_INSTRUMENTATION_KEY): + return invoker(request, metadata) + if not metadata: mutable_metadata = OrderedDict() else: @@ -189,6 +193,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 109a0d8563..993d3f5971 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 @@ -178,6 +179,7 @@ def test_stream_unary(self): }, ) + def test_stream_stream(self): bidirectional_streaming_method(self._stub) spans = self.memory_exporter.get_finished_spans() @@ -286,3 +288,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) From b0e315f6b5511b178d5150e2621d4aaa3846a24b Mon Sep 17 00:00:00 2001 From: Ryan SIU Date: Thu, 1 Jul 2021 15:21:12 +0800 Subject: [PATCH 2/3] Lint the changed files --- .../src/opentelemetry/instrumentation/grpc/_client.py | 4 ++-- .../tests/test_client_interceptor.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) 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 3fc1fe7aa3..735d276097 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py @@ -123,7 +123,7 @@ def _start_guarded_span(self, *args, **kwargs): return _GuardedSpan(self._start_span(*args, **kwargs)) def intercept_unary(self, request, metadata, client_info, invoker): - if context.get_value( _SUPPRESS_INSTRUMENTATION_KEY): + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): return invoker(request, metadata) if not metadata: @@ -193,7 +193,7 @@ def _intercept_server_stream( def intercept_stream( self, request_or_iterator, metadata, client_info, invoker ): - if context.get_value( _SUPPRESS_INSTRUMENTATION_KEY): + if context.get_value(_SUPPRESS_INSTRUMENTATION_KEY): return invoker(request_or_iterator, metadata) if client_info.is_server_stream: diff --git a/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py b/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py index 993d3f5971..a2a61f453f 100644 --- a/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py +++ b/instrumentation/opentelemetry-instrumentation-grpc/tests/test_client_interceptor.py @@ -179,7 +179,6 @@ def test_stream_unary(self): }, ) - def test_stream_stream(self): bidirectional_streaming_method(self._stub) spans = self.memory_exporter.get_finished_spans() From 11b5b227d8fcfd611a2f0965f81f4bcfd4a92e54 Mon Sep 17 00:00:00 2001 From: Ryan SIU Date: Sat, 10 Jul 2021 13:26:42 +0800 Subject: [PATCH 3/3] Add the CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afa238d74e..dd6f496faa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,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)) ### Added - `opentelemetry-instrumentation-httpx` Add `httpx` instrumentation