From ba190a875f88089cd5c2352fde7147b3babd43ca Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 14 Nov 2023 00:11:50 -0600 Subject: [PATCH] Clean up use of suppress_instrumentation in context and fix httpx bug --- .../pyproject.toml | 1 + .../instrumentation/aio_pika/utils.py | 9 --------- .../instrumentation/httpx/__init__.py | 6 +++--- .../instrumentation/pika/utils.py | 6 ++---- .../opentelemetry/instrumentation/utils.py | 20 +++++++++++++++++-- 5 files changed, 24 insertions(+), 18 deletions(-) delete mode 100644 instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/utils.py diff --git a/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml b/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml index ee87d4303b..b8e3691261 100644 --- a/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml +++ b/instrumentation/opentelemetry-instrumentation-aio-pika/pyproject.toml @@ -26,6 +26,7 @@ classifiers = [ ] dependencies = [ "opentelemetry-api ~= 1.5", + "opentelemetry-instrumentation == 0.42b0.dev", "wrapt >= 1.0.0, < 2.0.0", ] diff --git a/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/utils.py b/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/utils.py deleted file mode 100644 index fb94ddf468..0000000000 --- a/instrumentation/opentelemetry-instrumentation-aio-pika/src/opentelemetry/instrumentation/aio_pika/utils.py +++ /dev/null @@ -1,9 +0,0 @@ -from opentelemetry import context - - -def is_instrumentation_enabled() -> bool: - if context.get_value("suppress_instrumentation") or context.get_value( - context._SUPPRESS_INSTRUMENTATION_KEY - ): - return False - return True diff --git a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py index f5d34b3c40..55209bcd4f 100644 --- a/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py @@ -170,7 +170,7 @@ def response_hook(span, request, response): from opentelemetry.instrumentation.httpx.package import _instruments from opentelemetry.instrumentation.httpx.version import __version__ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor -from opentelemetry.instrumentation.utils import http_status_to_status_code +from opentelemetry.instrumentation.utils import http_status_to_status_code, is_http_instrumentation_enabled from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, TracerProvider, get_tracer @@ -316,7 +316,7 @@ def handle_request( httpx.Response, ]: """Add request info to span.""" - if context.get_value("suppress_instrumentation"): + if not is_http_instrumentation_enabled(): return self._transport.handle_request(*args, **kwargs) method, url, headers, stream, extensions = _extract_parameters( @@ -409,7 +409,7 @@ async def handle_async_request( httpx.Response, ]: """Add request info to span.""" - if context.get_value("suppress_instrumentation"): + if not is_http_instrumentation_enabled(): return await self._transport.handle_async_request(*args, **kwargs) method, url, headers, stream, extensions = _extract_parameters( diff --git a/instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py b/instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py index e9f819f2d6..866e618469 100644 --- a/instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py +++ b/instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py @@ -5,7 +5,7 @@ from pika.spec import Basic, BasicProperties from opentelemetry import context, propagate, trace -from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY +from opentelemetry.instrumentation.utils import is_instrumentation_enabled from opentelemetry.propagators.textmap import CarrierT, Getter from opentelemetry.semconv.trace import ( MessagingOperationValues, @@ -136,9 +136,7 @@ def _get_span( span_kind: SpanKind, operation: Optional[MessagingOperationValues] = None, ) -> Optional[Span]: - if context.get_value("suppress_instrumentation") or context.get_value( - _SUPPRESS_INSTRUMENTATION_KEY - ): + if not is_instrumentation_enabled(): return None task_name = properties.type if properties.type else task_name span = tracer.start_span( diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index e4f9b37c37..7baf124b68 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -23,9 +23,7 @@ from opentelemetry import context, trace -# pylint: disable=unused-import # pylint: disable=E0611 -from opentelemetry.context import _SUPPRESS_INSTRUMENTATION_KEY # noqa: F401 from opentelemetry.propagate import extract from opentelemetry.trace import StatusCode from opentelemetry.trace.propagation.tracecontext import ( @@ -212,3 +210,21 @@ def _get_opentelemetry_stability_opt_in( return _OpenTelemetrySemanticConventionStability._OTEL_SEMCONV_STABILITY_SIGNAL_MAPPING.get( type, _OpenTelemetryStabilityMode.DEFAULT ) + + +def is_instrumentation_enabled() -> bool: + if ( + context.get_value("suppress_instrumentation") + or + context.get_value(context._SUPPRESS_INSTRUMENTATION_KEY) # type: ignore + ): + return False + return True + + +def is_http_instrumentation_enabled() -> bool: + return ( + is_http_instrumentation_enabled() + and not + context.get_value(context._SUPPRESS_HTTP_INSTRUMENTATION_KEY) # type: ignore + )