Skip to content

Commit

Permalink
Clean up use of suppress_instrumentation in context and fix httpx bug
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Nov 14, 2023
1 parent b6d77f1 commit ba190a8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ classifiers = [
]
dependencies = [
"opentelemetry-api ~= 1.5",
"opentelemetry-instrumentation == 0.42b0.dev",
"wrapt >= 1.0.0, < 2.0.0",
]

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
)

0 comments on commit ba190a8

Please sign in to comment.