From 82c8b6e4b19d1c59c34814dba3f572aba3d3b0a4 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 3 Dec 2024 13:10:57 +0100 Subject: [PATCH] . --- .../integrations/opentelemetry/sampler.py | 20 +++++++++++++++++-- .../opentelemetry/span_processor.py | 2 +- sentry_sdk/tracing_utils.py | 20 ------------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/sampler.py b/sentry_sdk/integrations/opentelemetry/sampler.py index 52d1e95d21..3487aa7e00 100644 --- a/sentry_sdk/integrations/opentelemetry/sampler.py +++ b/sentry_sdk/integrations/opentelemetry/sampler.py @@ -6,7 +6,7 @@ from opentelemetry.trace.span import TraceState import sentry_sdk -from sentry_sdk.tracing_utils import create_sampling_context, has_tracing_enabled +from sentry_sdk.tracing_utils import has_tracing_enabled from sentry_sdk.utils import is_valid_sample_rate, logger from sentry_sdk.integrations.opentelemetry.consts import ( TRACESTATE_SAMPLED_KEY, @@ -17,7 +17,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import Optional, Sequence, Union + from typing import Any, Optional, Sequence, Union from opentelemetry.context import Context from opentelemetry.trace import Link, SpanKind from opentelemetry.trace.span import SpanContext @@ -187,3 +187,19 @@ def should_sample( def get_description(self) -> str: return self.__class__.__name__ + + +def create_sampling_context(name, attributes, parent_context, trace_id): + # type: (str, Attributes, Context, str) -> dict[str, Any] + sampling_context = { + "transaction_context": { + "name": name, + "op": attributes.get(SentrySpanAttribute.OP), + "source": attributes.get(SentrySpanAttribute.SOURCE), + }, + "parent_sampled": get_parent_sampled(parent_context, trace_id), + } + + sampling_context.update(attributes) + + return sampling_context diff --git a/sentry_sdk/integrations/opentelemetry/span_processor.py b/sentry_sdk/integrations/opentelemetry/span_processor.py index 7d73007ad4..a20a5bf439 100644 --- a/sentry_sdk/integrations/opentelemetry/span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/span_processor.py @@ -14,13 +14,13 @@ import sentry_sdk from sentry_sdk.consts import SPANDATA from sentry_sdk.tracing import DEFAULT_SPAN_ORIGIN -from sentry_sdk.tracing_utils import create_sampling_context from sentry_sdk.utils import get_current_thread_meta from sentry_sdk.profiler.continuous_profiler import ( try_autostart_continuous_profiler, get_profiler_id, ) from sentry_sdk.profiler.transaction_profiler import Profile +from sentry_sdk.integrations.opentelemetry.sampler import create_sampling_context from sentry_sdk.integrations.opentelemetry.utils import ( is_sentry_span, convert_from_otel_timestamp, diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 4ee2b5334f..4e2f46c81a 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -11,8 +11,6 @@ import sentry_sdk from sentry_sdk.consts import OP, SPANDATA -from sentry_sdk.integrations.opentelemetry.consts import SentrySpanAttribute -from sentry_sdk.integrations.opentelemetry.sampler import get_parent_sampled from sentry_sdk.utils import ( capture_internal_exceptions, filename_for_module, @@ -36,8 +34,6 @@ from typing import Optional from typing import Union from types import FrameType - from opentelemetry.context import Context - from opentelemetry.util.types import Attributes SENTRY_TRACE_REGEX = re.compile( @@ -728,22 +724,6 @@ def get_current_span(scope=None): return current_span -def create_sampling_context(name, attributes, parent_context, trace_id): - # type: (str, Attributes, Context, str) -> dict[str, Any] - sampling_context = { - "transaction_context": { - "name": name, - "op": attributes.get(SentrySpanAttribute.OP), - "source": attributes.get(SentrySpanAttribute.SOURCE), - }, - "parent_sampled": get_parent_sampled(parent_context, trace_id), - } - - sampling_context.update(attributes) - - return sampling_context - - # Circular imports from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME,