diff --git a/sentry_sdk/_span_batcher.py b/sentry_sdk/_span_batcher.py new file mode 100644 index 0000000000..eb749fdfa0 --- /dev/null +++ b/sentry_sdk/_span_batcher.py @@ -0,0 +1,135 @@ +import threading +from collections import defaultdict +from datetime import datetime, timezone +from typing import TYPE_CHECKING + +from sentry_sdk._batcher import Batcher +from sentry_sdk.consts import SPANSTATUS +from sentry_sdk.envelope import Envelope, Item, PayloadRef +from sentry_sdk.utils import format_timestamp, serialize_attribute, safe_repr + +if TYPE_CHECKING: + from typing import Any, Callable, Optional + from sentry_sdk.traces import SpanStatus, StreamedSpan + from sentry_sdk._types import SerializedAttributeValue + + +class SpanBatcher(Batcher["StreamedSpan"]): + # TODO[span-first]: size-based flushes + # TODO[span-first]: adjust flush/drop defaults + MAX_BEFORE_FLUSH = 1000 + MAX_BEFORE_DROP = 5000 + FLUSH_WAIT_TIME = 5.0 + + TYPE = "span" + CONTENT_TYPE = "application/vnd.sentry.items.span.v2+json" + + def __init__( + self, + capture_func: "Callable[[Envelope], None]", + record_lost_func: "Callable[..., None]", + ) -> None: + # Spans from different traces cannot be emitted in the same envelope + # since the envelope contains a shared trace header. That's why we bucket + # by trace_id, so that we can then send the buckets each in its own + # envelope. + # trace_id -> span buffer + self._span_buffer: dict[str, list["StreamedSpan"]] = defaultdict(list) + self._capture_func = capture_func + self._record_lost_func = record_lost_func + self._running = True + self._lock = threading.Lock() + + self._flush_event: "threading.Event" = threading.Event() + + self._flusher: "Optional[threading.Thread]" = None + self._flusher_pid: "Optional[int]" = None + + def get_size(self) -> int: + # caller is responsible for locking before checking this + return sum(len(buffer) for buffer in self._span_buffer.values()) + + def add(self, span: "StreamedSpan") -> None: + if not self._ensure_thread() or self._flusher is None: + return None + + with self._lock: + size = self.get_size() + if size >= self.MAX_BEFORE_DROP: + self._record_lost_func( + reason="queue_overflow", + data_category="span", + quantity=1, + ) + return None + + self._span_buffer[span.trace_id].append(span) + if size + 1 >= self.MAX_BEFORE_FLUSH: + self._flush_event.set() + + @staticmethod + def _to_transport_format(item: "StreamedSpan") -> "Any": + res: "dict[str, Any]" = { + "trace_id": item.trace_id, + "span_id": item.span_id, + "name": item.get_name(), + "status": item.status.value, + "is_segment": item.is_segment(), + "start_timestamp": item.start_timestamp.timestamp(), # TODO[span-first] + } + + if item.timestamp: + # this is here to make mypy happy + res["end_timestamp"] = item.timestamp.timestamp() + + if item.parent_span_id: + res["parent_span_id"] = item.parent_span_id + + if item.attributes: + res["attributes"] = { + k: serialize_attribute(v) for (k, v) in item.attributes.items() + } + + return res + + def _flush(self) -> None: + with self._lock: + if len(self._span_buffer) == 0: + return None + + envelopes = [] + for trace_id, spans in self._span_buffer.items(): + if spans: + dsc = spans[0].dynamic_sampling_context() + + envelope = Envelope( + headers={ + "sent_at": format_timestamp(datetime.now(timezone.utc)), + "trace": dsc, + } + ) + + envelope.add_item( + Item( + type="span", + content_type="application/vnd.sentry.items.span.v2+json", + headers={ + "item_count": len(spans), + }, + payload=PayloadRef( + json={ + "items": [ + self._to_transport_format(span) + for span in spans + ] + } + ), + ) + ) + + envelopes.append(envelope) + + self._span_buffer.clear() + + for envelope in envelopes: + self._capture_func(envelope) diff --git a/sentry_sdk/ai/monitoring.py b/sentry_sdk/ai/monitoring.py index 581e967bd4..6498a25c2f 100644 --- a/sentry_sdk/ai/monitoring.py +++ b/sentry_sdk/ai/monitoring.py @@ -6,6 +6,7 @@ import sentry_sdk.utils from sentry_sdk import start_span from sentry_sdk.tracing import Span +from sentry_sdk.traces import StreamedSpan from sentry_sdk.utils import ContextVar, reraise, capture_internal_exceptions from typing import TYPE_CHECKING @@ -97,7 +98,7 @@ async def async_wrapped(*args: "Any", **kwargs: "Any") -> "Any": def record_token_usage( - span: "Span", + span: "Union[Span, StreamedSpan]", input_tokens: "Optional[int]" = None, input_tokens_cached: "Optional[int]" = None, input_tokens_cache_write: "Optional[int]" = None, @@ -106,30 +107,35 @@ def record_token_usage( total_tokens: "Optional[int]" = None, ) -> None: # TODO: move pipeline name elsewhere + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + ai_pipeline_name = get_ai_pipeline_name() if ai_pipeline_name: - span.set_data(SPANDATA.GEN_AI_PIPELINE_NAME, ai_pipeline_name) + set_on_span(SPANDATA.GEN_AI_PIPELINE_NAME, ai_pipeline_name) if input_tokens is not None: - span.set_data(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) + set_on_span(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, input_tokens) if input_tokens_cached is not None: - span.set_data( + set_on_span( SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHED, input_tokens_cached, ) if input_tokens_cache_write is not None: - span.set_data( + set_on_span( SPANDATA.GEN_AI_USAGE_INPUT_TOKENS_CACHE_WRITE, input_tokens_cache_write, ) if output_tokens is not None: - span.set_data(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) + set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens) if output_tokens_reasoning is not None: - span.set_data( + set_on_span( SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, output_tokens_reasoning, ) @@ -138,4 +144,4 @@ def record_token_usage( total_tokens = input_tokens + output_tokens if total_tokens is not None: - span.set_data(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) + set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens) diff --git a/sentry_sdk/ai/utils.py b/sentry_sdk/ai/utils.py index a4ebe96d99..65b8589507 100644 --- a/sentry_sdk/ai/utils.py +++ b/sentry_sdk/ai/utils.py @@ -8,12 +8,14 @@ from sentry_sdk._types import BLOB_DATA_SUBSTITUTE if TYPE_CHECKING: - from typing import Any, Callable, Dict, List, Optional, Tuple + from typing import Any, Callable, Dict, List, Optional, Tuple, Union from sentry_sdk.tracing import Span import sentry_sdk from sentry_sdk.utils import logger +from sentry_sdk.traces import StreamedSpan +from sentry_sdk.tracing_utils import has_span_streaming_enabled MAX_GEN_AI_MESSAGE_BYTES = 20_000 # 20KB # Maximum characters when only a single message is left after bytes truncation @@ -489,13 +491,19 @@ def _normalize_data(data: "Any", unpack: bool = True) -> "Any": def set_data_normalized( - span: "Span", key: str, value: "Any", unpack: bool = True + span: "Union[Span, StreamedSpan]", key: str, value: "Any", unpack: bool = True ) -> None: normalized = _normalize_data(value, unpack=unpack) + + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + if isinstance(normalized, (int, float, bool, str)): - span.set_data(key, normalized) + set_on_span(key, normalized) else: - span.set_data(key, json.dumps(normalized)) + set_on_span(key, json.dumps(normalized)) def normalize_message_role(role: str) -> str: @@ -525,7 +533,14 @@ def normalize_message_roles(messages: "list[dict[str, Any]]") -> "list[dict[str, def get_start_span_function() -> "Callable[..., Any]": + client = sentry_sdk.get_client() + current_span = sentry_sdk.get_current_span() + if isinstance(current_span, StreamedSpan) or has_span_streaming_enabled( + client.options + ): + return sentry_sdk.traces.start_span + transaction_exists = ( current_span is not None and current_span.containing_transaction is not None ) diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index c4e2229938..a1db79763f 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -7,6 +7,7 @@ from sentry_sdk.consts import INSTRUMENTER from sentry_sdk.scope import Scope, _ScopeManager, new_scope, isolation_scope from sentry_sdk.tracing import NoOpSpan, Transaction, trace +from sentry_sdk.traces import StreamedSpan from sentry_sdk.crons import monitor from typing import TYPE_CHECKING @@ -385,7 +386,9 @@ def set_measurement(name: str, value: float, unit: "MeasurementUnit" = "") -> No transaction.set_measurement(name, value, unit) -def get_current_span(scope: "Optional[Scope]" = None) -> "Optional[Span]": +def get_current_span( + scope: "Optional[Scope]" = None, +) -> "Optional[Union[Span, StreamedSpan]]": """ Returns the currently active span if there is one running, otherwise `None` """ @@ -501,6 +504,16 @@ def update_current_span( if current_span is None: return + if isinstance(current_span, StreamedSpan): + warnings.warn( + "The `update_current_span` API isn't available in streaming mode. " + "Retrieve the current span with get_current_span() and use its API " + "directly.", + DeprecationWarning, + stacklevel=2, + ) + return + if op is not None: current_span.op = op diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index fb14d8e36a..06de3f7c0a 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -11,6 +11,7 @@ import sentry_sdk from sentry_sdk._compat import PY37, check_uwsgi_thread_support from sentry_sdk._metrics_batcher import MetricsBatcher +from sentry_sdk._span_batcher import SpanBatcher from sentry_sdk.utils import ( AnnotatedValue, ContextVar, @@ -31,6 +32,7 @@ ) from sentry_sdk.serializer import serialize from sentry_sdk.tracing import trace +from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.transport import BaseHttpTransport, make_transport from sentry_sdk.consts import ( SPANDATA, @@ -67,6 +69,7 @@ from sentry_sdk.scope import Scope from sentry_sdk.session import Session from sentry_sdk.spotlight import SpotlightClient + from sentry_sdk.traces import StreamedSpan from sentry_sdk.transport import Transport, Item from sentry_sdk._log_batcher import LogBatcher from sentry_sdk._metrics_batcher import MetricsBatcher @@ -188,6 +191,7 @@ def __init__(self, options: "Optional[Dict[str, Any]]" = None) -> None: self.monitor: "Optional[Monitor]" = None self.log_batcher: "Optional[LogBatcher]" = None self.metrics_batcher: "Optional[MetricsBatcher]" = None + self.span_batcher: "Optional[SpanBatcher]" = None self.integrations: "dict[str, Integration]" = {} def __getstate__(self, *args: "Any", **kwargs: "Any") -> "Any": @@ -224,6 +228,9 @@ def _capture_log(self, log: "Log", scope: "Scope") -> None: def _capture_metric(self, metric: "Metric", scope: "Scope") -> None: pass + def _capture_span(self, span: "StreamedSpan", scope: "Scope") -> None: + pass + def capture_session(self, *args: "Any", **kwargs: "Any") -> None: return None @@ -399,6 +406,13 @@ def _record_lost_event( record_lost_func=_record_lost_event, ) + self.span_batcher = None + if has_span_streaming_enabled(self.options): + self.span_batcher = SpanBatcher( + capture_func=_capture_envelope, + record_lost_func=_record_lost_event, + ) + max_request_body_size = ("always", "never", "small", "medium") if self.options["max_request_body_size"] not in max_request_body_size: raise ValueError( @@ -909,7 +923,10 @@ def capture_event( return return_value def _capture_telemetry( - self, telemetry: "Optional[Union[Log, Metric]]", ty: str, scope: "Scope" + self, + telemetry: "Optional[Union[Log, Metric, StreamedSpan]]", + ty: str, + scope: "Scope", ) -> None: # Capture attributes-based telemetry (logs, metrics, spansV2) if telemetry is None: @@ -922,6 +939,7 @@ def _capture_telemetry( before_send = get_before_send_log(self.options) elif ty == "metric": before_send = get_before_send_metric(self.options) # type: ignore + # no before_send for spans if before_send is not None: telemetry = before_send(telemetry, {}) # type: ignore @@ -934,6 +952,8 @@ def _capture_telemetry( batcher = self.log_batcher elif ty == "metric": batcher = self.metrics_batcher # type: ignore + elif ty == "span": + batcher = self.span_batcher # type: ignore if batcher is not None: batcher.add(telemetry) # type: ignore @@ -944,6 +964,9 @@ def _capture_log(self, log: "Optional[Log]", scope: "Scope") -> None: def _capture_metric(self, metric: "Optional[Metric]", scope: "Scope") -> None: self._capture_telemetry(metric, "metric", scope) + def _capture_span(self, span: "Optional[StreamedSpan]", scope: "Scope") -> None: + self._capture_telemetry(span, "span", scope) + def capture_session( self, session: "Session", @@ -993,6 +1016,8 @@ def close( self.log_batcher.kill() if self.metrics_batcher is not None: self.metrics_batcher.kill() + if self.span_batcher is not None: + self.span_batcher.kill() if self.monitor: self.monitor.kill() self.transport.kill() @@ -1018,6 +1043,8 @@ def flush( self.log_batcher.flush() if self.metrics_batcher is not None: self.metrics_batcher.flush() + if self.span_batcher is not None: + self.span_batcher.flush() self.transport.flush(timeout=timeout, callback=callback) def __enter__(self) -> "_Client": diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index 4b61a317fb..d787ca2532 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -82,6 +82,7 @@ class CompressionAlgo(Enum): "before_send_log": Optional[Callable[[Log, Hint], Optional[Log]]], "enable_metrics": Optional[bool], "before_send_metric": Optional[Callable[[Metric, Hint], Optional[Metric]]], + "trace_lifecycle": Optional[Literal["static", "stream"]], }, total=False, ) diff --git a/sentry_sdk/envelope.py b/sentry_sdk/envelope.py index 307fb26fd6..5e52c6196f 100644 --- a/sentry_sdk/envelope.py +++ b/sentry_sdk/envelope.py @@ -253,6 +253,8 @@ def data_category(self) -> "EventDataCategory": return "session" elif ty == "attachment": return "attachment" + elif ty == "span": + return "span" elif ty == "transaction": return "transaction" elif ty == "event": diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 6983af89ed..cab5020fda 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -25,7 +25,9 @@ from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, TransactionSource, + Span, ) +from sentry_sdk.traces import StreamedSpan from sentry_sdk.utils import ( ContextVar, event_from_exception, @@ -36,16 +38,20 @@ _get_installed_modules, ) from sentry_sdk.tracing import Transaction +from sentry_sdk.tracing_utils import has_span_streaming_enabled from typing import TYPE_CHECKING if TYPE_CHECKING: from typing import Any from typing import Dict + from typing import ContextManager from typing import Optional from typing import Tuple + from typing import Union from sentry_sdk._types import Event, Hint + from sentry_sdk.tracing import NoOpSpan _asgi_middleware_applied = ContextVar("sentry_asgi_middleware_applied") @@ -185,6 +191,9 @@ async def _run_app( self._capture_lifespan_exception(exc) raise exc from None + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + _asgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as sentry_scope: @@ -204,48 +213,81 @@ async def _run_app( ) method = scope.get("method", "").upper() - transaction = None - if ty in ("http", "websocket"): - if ty == "websocket" or method in self.http_methods_to_capture: - transaction = continue_trace( - _get_headers(scope), - op="{}.server".format(ty), + + span_ctx: "ContextManager[Union[Span, StreamedSpan, None]]" + if span_streaming: + segment: "Optional[StreamedSpan]" = None + if ty in ("http", "websocket"): + if ( + ty == "websocket" + or method in self.http_methods_to_capture + ): + sentry_sdk.traces.continue_trace(_get_headers(scope)) + segment = sentry_sdk.traces.start_span( + name=transaction_name + ) + segment.set_op(f"{ty}.server") + else: + sentry_sdk.traces.new_trace() + segment = sentry_sdk.traces.start_span( + name=transaction_name, + ) + segment.set_op(OP.HTTP_SERVER) + + if segment is not None: + segment.set_source(transaction_source) + segment.set_origin(self.span_origin) + segment.set_attribute("asgi.type", ty) + + span_ctx = segment or nullcontext() + + else: + transaction = None + if ty in ("http", "websocket"): + if ( + ty == "websocket" + or method in self.http_methods_to_capture + ): + transaction = continue_trace( + _get_headers(scope), + op="{}.server".format(ty), + name=transaction_name, + source=transaction_source, + origin=self.span_origin, + ) + else: + transaction = Transaction( + op=OP.HTTP_SERVER, name=transaction_name, source=transaction_source, origin=self.span_origin, ) - else: - transaction = Transaction( - op=OP.HTTP_SERVER, - name=transaction_name, - source=transaction_source, - origin=self.span_origin, - ) - if transaction: - transaction.set_tag("asgi.type", ty) + if transaction: + transaction.set_tag("asgi.type", ty) - transaction_context = ( - sentry_sdk.start_transaction( - transaction, - custom_sampling_context={"asgi_scope": scope}, + span_ctx = ( + sentry_sdk.start_transaction( + transaction, + custom_sampling_context={"asgi_scope": scope}, + ) + if transaction is not None + else nullcontext() ) - if transaction is not None - else nullcontext() - ) - with transaction_context: + + with span_ctx as span: try: async def _sentry_wrapped_send( event: "Dict[str, Any]", ) -> "Any": - if transaction is not None: + if span is not None: is_http_response = ( event.get("type") == "http.response.start" and "status" in event ) if is_http_response: - transaction.set_http_status(event["status"]) + span.set_http_status(event["status"]) return await send(event) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index 7f3591154a..6de8a6c9ab 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -1,10 +1,11 @@ from __future__ import annotations import contextlib -from typing import Any, TypeVar, Callable, Awaitable, Iterator +from typing import Any, TypeVar, Callable, Awaitable, Iterator, Union import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import Span from sentry_sdk.tracing_utils import add_query_source, record_sql_queries from sentry_sdk.utils import ( @@ -96,7 +97,7 @@ def _record( params_list: "tuple[Any, ...] | None", *, executemany: bool = False, -) -> "Iterator[Span]": +) -> "Iterator[Union[StreamedSpan, Span]]": integration = sentry_sdk.get_client().get_integration(AsyncPGIntegration) if integration is not None and not integration._record_params: params_list = None @@ -146,7 +147,10 @@ def _inner(*args: "Any", **kwargs: "Any") -> "T": # noqa: N807 ) as span: _set_db_data(span, args[0]) res = f(*args, **kwargs) - span.set_data("db.cursor", res) + if isinstance(span, StreamedSpan): + span.set_attribute("db.cursor", res) # type: ignore + else: + span.set_data("db.cursor", res) return res @@ -190,21 +194,26 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T": return _inner -def _set_db_data(span: "Span", conn: "Any") -> None: - span.set_data(SPANDATA.DB_SYSTEM, "postgresql") +def _set_db_data(span: "Union[StreamedSpan, Span]", conn: "Any") -> None: + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + + set_on_span(SPANDATA.DB_SYSTEM, "postgresql") addr = conn._addr if addr: try: - span.set_data(SPANDATA.SERVER_ADDRESS, addr[0]) - span.set_data(SPANDATA.SERVER_PORT, addr[1]) + set_on_span(SPANDATA.SERVER_ADDRESS, addr[0]) + set_on_span(SPANDATA.SERVER_PORT, addr[1]) except IndexError: pass database = conn._params.database if database: - span.set_data(SPANDATA.DB_NAME, database) + set_on_span(SPANDATA.DB_NAME, database) user = conn._params.user if user: - span.set_data(SPANDATA.DB_USER, user) + set_on_span(SPANDATA.DB_USER, user) diff --git a/sentry_sdk/integrations/celery/__init__.py b/sentry_sdk/integrations/celery/__init__.py index 2baf250ae3..27afb639f7 100644 --- a/sentry_sdk/integrations/celery/__init__.py +++ b/sentry_sdk/integrations/celery/__init__.py @@ -14,8 +14,9 @@ ) from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch from sentry_sdk.integrations.logging import ignore_logger +from sentry_sdk.traces import StreamedSpan, SpanStatus from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, TransactionSource -from sentry_sdk.tracing_utils import Baggage +from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, @@ -100,7 +101,10 @@ def _set_status(status: str) -> None: with capture_internal_exceptions(): scope = sentry_sdk.get_current_scope() if scope.span is not None: - scope.span.set_status(status) + if isinstance(scope.span, StreamedSpan): + scope.span.set_status(SpanStatus.ERROR) + else: + scope.span.set_status(status) def _capture_exception(task: "Any", exc_info: "ExcInfo") -> None: @@ -160,7 +164,9 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]": def _update_celery_task_headers( - original_headers: "dict[str, Any]", span: "Optional[Span]", monitor_beat_tasks: bool + original_headers: "dict[str, Any]", + span: "Optional[Union[StreamedSpan, Span]]", + monitor_beat_tasks: bool, ) -> "dict[str, Any]": """ Updates the headers of the Celery task with the tracing information @@ -247,7 +253,8 @@ def _wrap_task_run(f: "F") -> "F": def apply_async(*args: "Any", **kwargs: "Any") -> "Any": # Note: kwargs can contain headers=None, so no setdefault! # Unsure which backend though. - integration = sentry_sdk.get_client().get_integration(CeleryIntegration) + client = sentry_sdk.get_client() + integration = client.get_integration(CeleryIntegration) if integration is None: return f(*args, **kwargs) @@ -266,17 +273,23 @@ def apply_async(*args: "Any", **kwargs: "Any") -> "Any": else: task_name = "" + span_streaming = has_span_streaming_enabled(client.options) + task_started_from_beat = sentry_sdk.get_isolation_scope()._name == "celery-beat" - span_mgr: "Union[Span, NoOpMgr]" = ( - sentry_sdk.start_span( - op=OP.QUEUE_SUBMIT_CELERY, - name=task_name, - origin=CeleryIntegration.origin, - ) - if not task_started_from_beat - else NoOpMgr() - ) + span_mgr: "Union[StreamedSpan, Span, NoOpMgr]" = NoOpMgr() + if span_streaming: + if not task_started_from_beat: + span_mgr = sentry_sdk.traces.start_span(name=task_name) + span_mgr.set_op(OP.QUEUE_SUBMIT_CELERY) + span_mgr.set_origin(CeleryIntegration.origin) + else: + if not task_started_from_beat: + span_mgr = sentry_sdk.start_span( + op=OP.QUEUE_SUBMIT_CELERY, + name=task_name, + origin=CeleryIntegration.origin, + ) with span_mgr as span: kwargs["headers"] = _update_celery_task_headers( @@ -295,50 +308,72 @@ def _wrap_tracer(task: "Any", f: "F") -> "F": # Also because in Celery 3, signal dispatch returns early if one handler # crashes. @wraps(f) - @ensure_integration_enabled(CeleryIntegration, f) def _inner(*args: "Any", **kwargs: "Any") -> "Any": + client = sentry_sdk.get_client() + if client.get_integration(CeleryIntegration) is None: + return f(*args, **kwargs) + + span_streaming = has_span_streaming_enabled(client.options) + with isolation_scope() as scope: scope._name = "celery" scope.clear_breadcrumbs() scope.add_event_processor(_make_event_processor(task, *args, **kwargs)) - transaction = None + transaction: "Optional[Union[Span, StreamedSpan]]" = None + span_ctx: "Union[Span, StreamedSpan]" # Celery task objects are not a thing to be trusted. Even # something such as attribute access can fail. with capture_internal_exceptions(): headers = args[3].get("headers") or {} - transaction = continue_trace( - headers, - op=OP.QUEUE_TASK_CELERY, - name="unknown celery task", - source=TransactionSource.TASK, - origin=CeleryIntegration.origin, - ) - transaction.name = task.name - transaction.set_status(SPANSTATUS.OK) + if span_streaming: + sentry_sdk.traces.continue_trace(headers) + transaction = sentry_sdk.traces.start_span( + name="unknown celery task" + ) + transaction.set_origin(CeleryIntegration.origin) + transaction.set_source(TransactionSource.TASK) + transaction.set_op(OP.QUEUE_TASK_CELERY) + + span_ctx = transaction + + else: + transaction = continue_trace( + headers, + op=OP.QUEUE_TASK_CELERY, + name="unknown celery task", + source=TransactionSource.TASK, + origin=CeleryIntegration.origin, + ) + transaction.name = task.name + transaction.set_status(SPANSTATUS.OK) + + span_ctx = sentry_sdk.start_transaction( + transaction, + custom_sampling_context={ + "celery_job": { + "task": task.name, + # for some reason, args[1] is a list if non-empty but a + # tuple if empty + "args": list(args[1]), + "kwargs": args[2], + } + }, + ) if transaction is None: return f(*args, **kwargs) - with sentry_sdk.start_transaction( - transaction, - custom_sampling_context={ - "celery_job": { - "task": task.name, - # for some reason, args[1] is a list if non-empty but a - # tuple if empty - "args": list(args[1]), - "kwargs": args[2], - } - }, - ): + with span_ctx: return f(*args, **kwargs) return _inner # type: ignore -def _set_messaging_destination_name(task: "Any", span: "Span") -> None: +def _set_messaging_destination_name( + task: "Any", span: "Union[StreamedSpan, Span]" +) -> None: """Set "messaging.destination.name" tag for span""" with capture_internal_exceptions(): delivery_info = task.request.delivery_info @@ -347,7 +382,10 @@ def _set_messaging_destination_name(task: "Any", span: "Span") -> None: if delivery_info.get("exchange") == "" and routing_key is not None: # Empty exchange indicates the default exchange, meaning the tasks # are sent to the queue with the same name as the routing key. - span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) + if isinstance(span, StreamedSpan): + span.set_attribute(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) + else: + span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) def _wrap_task_call(task: "Any", f: "F") -> "F": @@ -359,14 +397,32 @@ def _wrap_task_call(task: "Any", f: "F") -> "F": # but if we ever remove the @ensure_integration_enabled decorator, we need # to add @functools.wraps(f) here. # https://github.com/getsentry/sentry-python/issues/421 - @ensure_integration_enabled(CeleryIntegration, f) def _inner(*args: "Any", **kwargs: "Any") -> "Any": + client = sentry_sdk.get_client() + if client.get_integration(CeleryIntegration) is None: + return f(*args, **kwargs) + + span_streaming = has_span_streaming_enabled(client.options) + try: - with sentry_sdk.start_span( - op=OP.QUEUE_PROCESS, - name=task.name, - origin=CeleryIntegration.origin, - ) as span: + span: "Union[Span, StreamedSpan]" + if span_streaming: + span = sentry_sdk.traces.start_span(name=task.name) + span.set_op(OP.QUEUE_PROCESS) + span.set_origin(CeleryIntegration.origin) + else: + span = sentry_sdk.start_span( + op=OP.QUEUE_PROCESS, + name=task.name, + origin=CeleryIntegration.origin, + ) + + with span: + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + _set_messaging_destination_name(task, span) latency = None @@ -381,18 +437,18 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any": if latency is not None: latency *= 1000 # milliseconds - span.set_data(SPANDATA.MESSAGING_MESSAGE_RECEIVE_LATENCY, latency) + set_on_span(SPANDATA.MESSAGING_MESSAGE_RECEIVE_LATENCY, latency) with capture_internal_exceptions(): - span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task.request.id) + set_on_span(SPANDATA.MESSAGING_MESSAGE_ID, task.request.id) with capture_internal_exceptions(): - span.set_data( + set_on_span( SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, task.request.retries ) with capture_internal_exceptions(): - span.set_data( + set_on_span( SPANDATA.MESSAGING_SYSTEM, task.app.connection().transport.driver_type, ) @@ -467,8 +523,13 @@ def sentry_workloop(*args: "Any", **kwargs: "Any") -> "Any": def _patch_producer_publish() -> None: original_publish = Producer.publish - @ensure_integration_enabled(CeleryIntegration, original_publish) def sentry_publish(self: "Producer", *args: "Any", **kwargs: "Any") -> "Any": + client = sentry_sdk.get_client() + if client.get_integration(CeleryIntegration) is None: + return original_publish(self, *args, **kwargs) + + span_streaming = has_span_streaming_enabled(client.options) + kwargs_headers = kwargs.get("headers", {}) if not isinstance(kwargs_headers, Mapping): # Ensure kwargs_headers is a Mapping, so we can safely call get(). @@ -485,24 +546,37 @@ def sentry_publish(self: "Producer", *args: "Any", **kwargs: "Any") -> "Any": routing_key = kwargs.get("routing_key") exchange = kwargs.get("exchange") - with sentry_sdk.start_span( - op=OP.QUEUE_PUBLISH, - name=task_name, - origin=CeleryIntegration.origin, - ) as span: + span: "Union[StreamedSpan, Span]" + if span_streaming: + span = sentry_sdk.traces.start_span(name=task_name) + span.set_op(OP.QUEUE_PUBLISH) + span.set_origin(CeleryIntegration.origin) + else: + span = sentry_sdk.start_span( + op=OP.QUEUE_PUBLISH, + name=task_name, + origin=CeleryIntegration.origin, + ) + + with span: + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + if task_id is not None: - span.set_data(SPANDATA.MESSAGING_MESSAGE_ID, task_id) + set_on_span(SPANDATA.MESSAGING_MESSAGE_ID, task_id) if exchange == "" and routing_key is not None: # Empty exchange indicates the default exchange, meaning messages are # routed to the queue with the same name as the routing key. - span.set_data(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) + set_on_span(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) if retries is not None: - span.set_data(SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, retries) + set_on_span(SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, retries) with capture_internal_exceptions(): - span.set_data( + set_on_span( SPANDATA.MESSAGING_SYSTEM, self.connection.transport.driver_type ) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 2595c33ea8..e679740af3 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -8,6 +8,7 @@ from sentry_sdk.consts import OP, SPANDATA, SPANNAME from sentry_sdk.scope import add_global_event_processor, should_send_default_pii from sentry_sdk.serializer import add_global_repr_processor, add_repr_sequence_type +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource from sentry_sdk.tracing_utils import add_query_source, record_sql_queries from sentry_sdk.utils import ( @@ -720,14 +721,21 @@ def _rollback(self: "BaseDatabaseWrapper") -> None: def _set_db_data( - span: "Span", cursor_or_db: "Any", db_operation: "Optional[str]" = None + span: "Union[StreamedSpan, Span]", + cursor_or_db: "Any", + db_operation: "Optional[str]" = None, ) -> None: + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + db = cursor_or_db.db if hasattr(cursor_or_db, "db") else cursor_or_db vendor = db.vendor - span.set_data(SPANDATA.DB_SYSTEM, vendor) + set_on_span(SPANDATA.DB_SYSTEM, vendor) if db_operation is not None: - span.set_data(SPANDATA.DB_OPERATION, db_operation) + set_on_span(SPANDATA.DB_OPERATION, db_operation) # Some custom backends override `__getattr__`, making it look like `cursor_or_db` # actually has a `connection` and the `connection` has a `get_dsn_parameters` @@ -760,19 +768,19 @@ def _set_db_data( db_name = connection_params.get("dbname") or connection_params.get("database") if db_name is not None: - span.set_data(SPANDATA.DB_NAME, db_name) + set_on_span(SPANDATA.DB_NAME, db_name) server_address = connection_params.get("host") if server_address is not None: - span.set_data(SPANDATA.SERVER_ADDRESS, server_address) + set_on_span(SPANDATA.SERVER_ADDRESS, server_address) server_port = connection_params.get("port") if server_port is not None: - span.set_data(SPANDATA.SERVER_PORT, str(server_port)) + set_on_span(SPANDATA.SERVER_PORT, str(server_port)) server_socket_address = connection_params.get("unix_socket") if server_socket_address is not None: - span.set_data(SPANDATA.SERVER_SOCKET_ADDRESS, server_socket_address) + set_on_span(SPANDATA.SERVER_SOCKET_ADDRESS, server_socket_address) def add_template_context_repr_sequence() -> None: diff --git a/sentry_sdk/integrations/graphene.py b/sentry_sdk/integrations/graphene.py index 5a61ca5c78..29f972d185 100644 --- a/sentry_sdk/integrations/graphene.py +++ b/sentry_sdk/integrations/graphene.py @@ -4,6 +4,7 @@ from sentry_sdk.consts import OP from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, @@ -25,6 +26,8 @@ from graphql.execution import ExecutionResult from graphql.type import GraphQLSchema from sentry_sdk._types import Event + from sentry_sdk.tracing import Span + from sentry_sdk.traces import StreamedSpan class GrapheneIntegration(Integration): @@ -141,15 +144,21 @@ def graphql_span( }, ) - scope = sentry_sdk.get_current_scope() - if scope.span: - _graphql_span = scope.span.start_child(op=op, name=operation_name) + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + _graphql_span: "Union[Span, StreamedSpan]" + if span_streaming: + _graphql_span = sentry_sdk.traces.start_span(name=operation_name or "operation") + _graphql_span.set_op(op) + _graphql_span.set_attribute("graphql.document", source) + if operation_name: + _graphql_span.set_attribute("graphql.operation.name", operation_name) + _graphql_span.set_attribute("graphql.operation.type", operation_type) else: _graphql_span = sentry_sdk.start_span(op=op, name=operation_name) - - _graphql_span.set_data("graphql.document", source) - _graphql_span.set_data("graphql.operation.name", operation_name) - _graphql_span.set_data("graphql.operation.type", operation_type) + _graphql_span.set_data("graphql.document", source) + _graphql_span.set_data("graphql.operation.name", operation_name) + _graphql_span.set_data("graphql.operation.type", operation_type) try: yield diff --git a/sentry_sdk/integrations/httpx.py b/sentry_sdk/integrations/httpx.py index 38c4f437bc..c7f67d96c3 100644 --- a/sentry_sdk/integrations/httpx.py +++ b/sentry_sdk/integrations/httpx.py @@ -2,11 +2,13 @@ from sentry_sdk import start_span from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import Integration, DidNotEnable -from sentry_sdk.tracing import BAGGAGE_HEADER_NAME +from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing_utils import ( should_propagate_trace, add_http_request_source, add_sentry_baggage_to_headers, + has_span_streaming_enabled, ) from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, @@ -19,7 +21,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any + from typing import Any, Optional, Union try: @@ -47,26 +49,49 @@ def setup_once() -> None: def _install_httpx_client() -> None: real_send = Client.send - @ensure_integration_enabled(HttpxIntegration, real_send) def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": + client = sentry_sdk.get_client() + if client.get_integration(HttpxIntegration) is None: + return real_send(self, request, **kwargs) + + span_streaming = has_span_streaming_enabled(client.options) + parsed_url = None with capture_internal_exceptions(): parsed_url = parse_url(str(request.url), sanitize=False) - with start_span( - op=OP.HTTP_CLIENT, - name="%s %s" - % ( - request.method, - parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, - ), - origin=HttpxIntegration.origin, - ) as span: - span.set_data(SPANDATA.HTTP_METHOD, request.method) - if parsed_url is not None: - span.set_data("url", parsed_url.url) - span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) - span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + span_ctx: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span_ctx = sentry_sdk.traces.start_span( + name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}" + ) + else: + span_ctx = start_span( + op=OP.HTTP_CLIENT, + name="%s %s" + % ( + request.method, + parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, + ), + origin=HttpxIntegration.origin, + ) + + with span_ctx as span: + if isinstance(span, StreamedSpan): + span.set_op(OP.HTTP_CLIENT) + span.set_origin(HttpxIntegration.origin) + + span.set_attribute(SPANDATA.HTTP_METHOD, request.method) + if parsed_url is not None: + span.set_attribute("url", parsed_url.url) + span.set_attribute(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_attribute(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + else: + span.set_data(SPANDATA.HTTP_METHOD, request.method) + if parsed_url is not None: + span.set_data("url", parsed_url.url) + span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) if should_propagate_trace(sentry_sdk.get_client(), str(request.url)): for ( @@ -87,7 +112,10 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": rv = real_send(self, request, **kwargs) span.set_http_status(rv.status_code) - span.set_data("reason", rv.reason_phrase) + if isinstance(span, StreamedSpan): + span.set_attribute("reason", rv.reason_phrase) + else: + span.set_data("reason", rv.reason_phrase) with capture_internal_exceptions(): add_http_request_source(span) @@ -103,29 +131,49 @@ def _install_httpx_async_client() -> None: async def send( self: "AsyncClient", request: "Request", **kwargs: "Any" ) -> "Response": - if sentry_sdk.get_client().get_integration(HttpxIntegration) is None: + client = sentry_sdk.get_client() + if client.get_integration(HttpxIntegration) is None: return await real_send(self, request, **kwargs) + span_streaming = has_span_streaming_enabled(client.options) + parsed_url = None with capture_internal_exceptions(): parsed_url = parse_url(str(request.url), sanitize=False) - with start_span( - op=OP.HTTP_CLIENT, - name="%s %s" - % ( - request.method, - parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, - ), - origin=HttpxIntegration.origin, - ) as span: - span.set_data(SPANDATA.HTTP_METHOD, request.method) - if parsed_url is not None: - span.set_data("url", parsed_url.url) - span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) - span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) - - if should_propagate_trace(sentry_sdk.get_client(), str(request.url)): + span_ctx: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span_ctx = sentry_sdk.traces.start_span( + name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}" + ) + else: + span_ctx = start_span( + op=OP.HTTP_CLIENT, + name="%s %s" + % ( + request.method, + parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, + ), + origin=HttpxIntegration.origin, + ) + + with span_ctx as span: + if isinstance(span, StreamedSpan): + span.set_op(OP.HTTP_CLIENT) + span.set_origin(HttpxIntegration.origin) + span.set_attribute(SPANDATA.HTTP_METHOD, request.method) + if parsed_url is not None: + span.set_attribute("url", parsed_url.url) + span.set_attribute(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_attribute(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + else: + span.set_data(SPANDATA.HTTP_METHOD, request.method) + if parsed_url is not None: + span.set_data("url", parsed_url.url) + span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + + if should_propagate_trace(client, str(request.url)): for ( key, value, @@ -146,7 +194,10 @@ async def send( rv = await real_send(self, request, **kwargs) span.set_http_status(rv.status_code) - span.set_data("reason", rv.reason_phrase) + if isinstance(span, StreamedSpan): + span.set_attribute("reason", rv.reason_phrase) + else: + span.set_data("reason", rv.reason_phrase) with capture_internal_exceptions(): add_http_request_source(span) diff --git a/sentry_sdk/integrations/huggingface_hub.py b/sentry_sdk/integrations/huggingface_hub.py index 8509cadefa..c6477f558e 100644 --- a/sentry_sdk/integrations/huggingface_hub.py +++ b/sentry_sdk/integrations/huggingface_hub.py @@ -8,7 +8,9 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing_utils import set_span_errored +from sentry_sdk.traces import StreamedSpan +from sentry_sdk.tracing import Span +from sentry_sdk.tracing_utils import has_span_streaming_enabled, set_span_errored from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -18,7 +20,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any, Callable, Iterable + from typing import Any, Callable, Iterable, Union try: import huggingface_hub.inference._client @@ -66,7 +68,8 @@ def _capture_exception(exc: "Any") -> None: def _wrap_huggingface_task(f: "Callable[..., Any]", op: str) -> "Callable[..., Any]": @wraps(f) def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": - integration = sentry_sdk.get_client().get_integration(HuggingfaceHubIntegration) + sentry_client = sentry_sdk.get_client() + integration = sentry_client.get_integration(HuggingfaceHubIntegration) if integration is None: return f(*args, **kwargs) @@ -87,17 +90,30 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": model = client.model or kwargs.get("model") or "" operation_name = op.split(".")[-1] - span = sentry_sdk.start_span( - op=op, - name=f"{operation_name} {model}", - origin=HuggingfaceHubIntegration.origin, - ) + span_streaming = has_span_streaming_enabled(sentry_client.options) + span: "Union[StreamedSpan, Span]" + if span_streaming: + span = sentry_sdk.traces.start_span(name=f"{operation_name} {model}") + span.set_op(op) + span.set_origin(HuggingfaceHubIntegration.origin) + else: + span = sentry_sdk.start_span( + op=op, + name=f"{operation_name} {model}", + origin=HuggingfaceHubIntegration.origin, + ) + span.__enter__() - span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, operation_name) + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + + set_on_span(SPANDATA.GEN_AI_OPERATION_NAME, operation_name) if model: - span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model) + set_on_span(SPANDATA.GEN_AI_REQUEST_MODEL, model) # Input attributes if should_send_default_pii() and integration.include_prompts: @@ -120,7 +136,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": value = kwargs.get(attribute, None) if value is not None: if isinstance(value, (int, float, bool, str)): - span.set_data(span_attribute, value) + set_on_span(span_attribute, value) else: set_data_normalized(span, span_attribute, value, unpack=False) @@ -181,7 +197,7 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any": response_text_buffer.append(choice.message.content) if response_model is not None: - span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) + set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) if finish_reason is not None: set_data_normalized( @@ -332,9 +348,7 @@ def new_iterator() -> "Iterable[str]": yield chunk if response_model is not None: - span.set_data( - SPANDATA.GEN_AI_RESPONSE_MODEL, response_model - ) + set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) if finish_reason is not None: set_data_normalized( diff --git a/sentry_sdk/integrations/openai_agents/utils.py b/sentry_sdk/integrations/openai_agents/utils.py index a24d0e909d..dbbdd66792 100644 --- a/sentry_sdk/integrations/openai_agents/utils.py +++ b/sentry_sdk/integrations/openai_agents/utils.py @@ -9,13 +9,14 @@ from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP from sentry_sdk.integrations import DidNotEnable from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing_utils import set_span_errored from sentry_sdk.utils import event_from_exception, safe_serialize from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any + from typing import Any, Union from agents import Usage from sentry_sdk.tracing import Span @@ -38,17 +39,24 @@ def _capture_exception(exc: "Any") -> None: sentry_sdk.capture_event(event, hint=hint) -def _record_exception_on_span(span: "Span", error: Exception) -> "Any": +def _record_exception_on_span( + span: "Union[StreamedSpan, Span]", error: Exception +) -> "Any": set_span_errored(span) - span.set_data("span.status", "error") + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + + set_on_span("span.status", "error") # Optionally capture the error details if we have them if hasattr(error, "__class__"): - span.set_data("error.type", error.__class__.__name__) + set_on_span("error.type", error.__class__.__name__) if hasattr(error, "__str__"): error_message = str(error) if error_message: - span.set_data("error.message", error_message) + set_on_span("error.message", error_message) def _set_agent_data(span: "sentry_sdk.tracing.Span", agent: "agents.Agent") -> None: diff --git a/sentry_sdk/integrations/rust_tracing.py b/sentry_sdk/integrations/rust_tracing.py index e2b203a286..6d7799583d 100644 --- a/sentry_sdk/integrations/rust_tracing.py +++ b/sentry_sdk/integrations/rust_tracing.py @@ -32,15 +32,19 @@ import json from enum import Enum, auto -from typing import Any, Callable, Dict, Tuple, Optional +from typing import Any, Callable, Dict, Tuple, Optional, Union import sentry_sdk from sentry_sdk.integrations import Integration from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing import Span as SentrySpan +from sentry_sdk.traces import StreamedSpan +from sentry_sdk.tracing import Span +from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE -TraceState = Optional[Tuple[Optional[SentrySpan], SentrySpan]] +TraceState = Optional[ + Tuple[Optional[Union[StreamedSpan, Span]], Union[StreamedSpan, Span]] +] class RustTracingLevel(Enum): @@ -204,25 +208,49 @@ def on_new_span(self, attrs: str, span_id: str) -> "TraceState": else: sentry_span_name = "" - kwargs = { - "op": "function", - "name": sentry_span_name, - "origin": self.origin, - } - scope = sentry_sdk.get_current_scope() parent_sentry_span = scope.span + sentry_span: "Union[StreamedSpan, Span]" if parent_sentry_span: - sentry_span = parent_sentry_span.start_child(**kwargs) + if isinstance(parent_sentry_span, StreamedSpan): + sentry_span = sentry_sdk.traces.start_span( + name=sentry_span_name, parent_span=parent_sentry_span + ) + sentry_span.set_op("function") + sentry_span.set_origin("origin") + else: + sentry_span = parent_sentry_span.start_child( + op="function", + name=sentry_span_name, + origin=self.origin, + ) + else: + client = sentry_sdk.get_client() + + if has_span_streaming_enabled(client.options): + sentry_span = sentry_sdk.traces.start_span( + name=sentry_span_name, + ) + sentry_span.set_op("function") + sentry_span.set_origin(self.origin) + else: + sentry_span = sentry_sdk.start_span( + op="function", + name=sentry_span_name, + origin=self.origin, + ) + + if isinstance(sentry_span, StreamedSpan): + set_on_span = sentry_span.set_attribute else: - sentry_span = scope.start_span(**kwargs) + set_on_span = sentry_span.set_data fields = metadata.get("fields", []) for field in fields: if self._include_tracing_fields(): - sentry_span.set_data(field, attrs.get(field)) + set_on_span(field, attrs.get(field)) else: - sentry_span.set_data(field, SENSITIVE_DATA_SUBSTITUTE) + set_on_span(field, SENSITIVE_DATA_SUBSTITUTE) scope.span = sentry_span return (parent_sentry_span, sentry_span) @@ -240,12 +268,17 @@ def on_record(self, span_id: str, values: str, span_state: "TraceState") -> None return _parent_sentry_span, sentry_span = span_state + if isinstance(sentry_span, StreamedSpan): + set_on_span = sentry_span.set_attribute + else: + set_on_span = sentry_span.set_data + deserialized_values = json.loads(values) for key, value in deserialized_values.items(): if self._include_tracing_fields(): - sentry_span.set_data(key, value) + set_on_span(key, value) else: - sentry_span.set_data(key, SENSITIVE_DATA_SUBSTITUTE) + set_on_span(key, SENSITIVE_DATA_SUBSTITUTE) class RustTracingIntegration(Integration): diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 7d3ed95373..754b452318 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -1,5 +1,6 @@ from sentry_sdk.consts import SPANSTATUS, SPANDATA from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable +from sentry_sdk.traces import StreamedSpan, SpanStatus from sentry_sdk.tracing_utils import add_query_source, record_sql_queries from sentry_sdk.utils import ( capture_internal_exceptions, @@ -20,6 +21,7 @@ from typing import Any from typing import ContextManager from typing import Optional + from typing import Union from sentry_sdk.tracing import Span @@ -96,7 +98,10 @@ def _handle_error(context: "Any", *args: "Any") -> None: span: "Optional[Span]" = getattr(execution_context, "_sentry_sql_span", None) if span is not None: - span.set_status(SPANSTATUS.INTERNAL_ERROR) + if isinstance(span, StreamedSpan): + span.set_status(SpanStatus.ERROR) + else: + span.set_status(SPANSTATUS.INTERNAL_ERROR) # _after_cursor_execute does not get called for crashing SQL stmts. Judging # from SQLAlchemy codebase it does seem like any error coming into this @@ -132,22 +137,27 @@ def _get_db_system(name: str) -> "Optional[str]": return None -def _set_db_data(span: "Span", conn: "Any") -> None: +def _set_db_data(span: "Union[Span, StreamedSpan]", conn: "Any") -> None: + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + db_system = _get_db_system(conn.engine.name) if db_system is not None: - span.set_data(SPANDATA.DB_SYSTEM, db_system) + set_on_span(SPANDATA.DB_SYSTEM, db_system) if conn.engine.url is None: return db_name = conn.engine.url.database if db_name is not None: - span.set_data(SPANDATA.DB_NAME, db_name) + set_on_span(SPANDATA.DB_NAME, db_name) server_address = conn.engine.url.host if server_address is not None: - span.set_data(SPANDATA.SERVER_ADDRESS, server_address) + set_on_span(SPANDATA.SERVER_ADDRESS, server_address) server_port = conn.engine.url.port if server_port is not None: - span.set_data(SPANDATA.SERVER_PORT, server_port) + set_on_span(SPANDATA.SERVER_PORT, server_port) diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 0b797ebcde..b90a8e4f0d 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -24,6 +24,7 @@ SOURCE_FOR_STYLE, TransactionSource, ) +from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.utils import ( AnnotatedValue, capture_internal_exceptions, @@ -39,6 +40,8 @@ from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union from sentry_sdk._types import Event, HttpStatusCodeRange + from sentry_sdk.tracing import Span + from sentry_sdk.traces import StreamedSpan try: import starlette # type: ignore @@ -147,10 +150,13 @@ async def _create_span_call( send: "Callable[[Dict[str, Any]], Awaitable[None]]", **kwargs: "Any", ) -> None: - integration = sentry_sdk.get_client().get_integration(StarletteIntegration) + client = sentry_sdk.get_client() + integration = client.get_integration(StarletteIntegration) if integration is None: return await old_call(app, scope, receive, send, **kwargs) + span_streaming = has_span_streaming_enabled(client.options) + # Update transaction name with middleware name name, source = _get_transaction_from_middleware(app, scope, integration) @@ -165,21 +171,40 @@ async def _create_span_call( middleware_name = app.__class__.__name__ - with sentry_sdk.start_span( - op=OP.MIDDLEWARE_STARLETTE, - name=middleware_name, - origin=StarletteIntegration.origin, - ) as middleware_span: + middleware_span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + middleware_span = sentry_sdk.traces.start_span(name=middleware_name) + middleware_span.set_op(OP.MIDDLEWARE_STARLETTE) + middleware_span.set_origin(StarletteIntegration.origin) + middleware_span.set_attribute("starlette.middleware_name", middleware_name) + else: + middleware_span = sentry_sdk.start_span( + op=OP.MIDDLEWARE_STARLETTE, + name=middleware_name, + origin=StarletteIntegration.origin, + ) middleware_span.set_tag("starlette.middleware_name", middleware_name) + with middleware_span: # Creating spans for the "receive" callback async def _sentry_receive(*args: "Any", **kwargs: "Any") -> "Any": - with sentry_sdk.start_span( - op=OP.MIDDLEWARE_STARLETTE_RECEIVE, - name=getattr(receive, "__qualname__", str(receive)), - origin=StarletteIntegration.origin, - ) as span: + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span( + name=getattr(receive, "__qualname__", str(receive)), + ) + span.set_origin(StarletteIntegration.origin) + span.set_op(OP.MIDDLEWARE_STARLETTE_RECEIVE) + span.set_attribute("starlette.middleware_name", middleware_name) + else: + span = sentry_sdk.start_span( + op=OP.MIDDLEWARE_STARLETTE_RECEIVE, + name=getattr(receive, "__qualname__", str(receive)), + origin=StarletteIntegration.origin, + ) span.set_tag("starlette.middleware_name", middleware_name) + + with span: return await receive(*args, **kwargs) receive_name = getattr(receive, "__name__", str(receive)) @@ -188,12 +213,23 @@ async def _sentry_receive(*args: "Any", **kwargs: "Any") -> "Any": # Creating spans for the "send" callback async def _sentry_send(*args: "Any", **kwargs: "Any") -> "Any": - with sentry_sdk.start_span( - op=OP.MIDDLEWARE_STARLETTE_SEND, - name=getattr(send, "__qualname__", str(send)), - origin=StarletteIntegration.origin, - ) as span: + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span( + name=getattr(send, "__qualname__", str(send)), + ) + span.set_op(OP.MIDDLEWARE_STARLETTE_SEND) + span.set_origin(StarletteIntegration.origin) + span.set_attribute("starlette.middleware_name", middleware_name) + else: + span = sentry_sdk.start_span( + op=OP.MIDDLEWARE_STARLETTE_SEND, + name=getattr(send, "__qualname__", str(send)), + origin=StarletteIntegration.origin, + ) span.set_tag("starlette.middleware_name", middleware_name) + + with span: return await send(*args, **kwargs) send_name = getattr(send, "__name__", str(send)) diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index e3120a3b32..8b42b93b1e 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -8,10 +8,13 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import Integration from sentry_sdk.scope import add_global_event_processor +from sentry_sdk.tracing import Span +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing_utils import ( EnvironHeaders, should_propagate_trace, add_http_request_source, + has_span_streaming_enabled, ) from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, @@ -31,6 +34,7 @@ from typing import Dict from typing import Optional from typing import List + from typing import Union from sentry_sdk._types import Event, Hint @@ -86,6 +90,8 @@ def putrequest( ): return real_putrequest(self, method, url, *args, **kwargs) + span_streaming = has_span_streaming_enabled(client.options) + real_url = url if real_url is None or not real_url.startswith(("http://", "https://")): real_url = "%s://%s%s%s" % ( @@ -99,22 +105,45 @@ def putrequest( with capture_internal_exceptions(): parsed_url = parse_url(real_url, sanitize=False) - span = sentry_sdk.start_span( - op=OP.HTTP_CLIENT, - name="%s %s" - % (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE), - origin="auto.http.stdlib.httplib", - ) - span.set_data(SPANDATA.HTTP_METHOD, method) - if parsed_url is not None: - span.set_data("url", parsed_url.url) - span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) - span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) - - # for proxies, these point to the proxy host/port - if tunnel_host: - span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, self.host) - span.set_data(SPANDATA.NETWORK_PEER_PORT, self.port) + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span( + name=f"{method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}" + ) + span.set_op(OP.HTTP_CLIENT) + span.set_origin("auto.http.stdlib.httplib") + + span.set_attribute(SPANDATA.HTTP_METHOD, method) + if parsed_url is not None: + span.set_attribute("url", parsed_url.url) + span.set_attribute(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_attribute(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + + # for proxies, these point to the proxy host/port + if tunnel_host: + span.set_attribute(SPANDATA.NETWORK_PEER_ADDRESS, self.host) + span.set_attribute(SPANDATA.NETWORK_PEER_PORT, self.port) + + span.start() + + else: + span = sentry_sdk.start_span( + op=OP.HTTP_CLIENT, + name="%s %s" + % (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE), + origin="auto.http.stdlib.httplib", + ) + + span.set_data(SPANDATA.HTTP_METHOD, method) + if parsed_url is not None: + span.set_data("url", parsed_url.url) + span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + + # for proxies, these point to the proxy host/port + if tunnel_host: + span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, self.host) + span.set_data(SPANDATA.NETWORK_PEER_PORT, self.port) rv = real_putrequest(self, method, url, *args, **kwargs) @@ -146,7 +175,10 @@ def getresponse(self: "HTTPConnection", *args: "Any", **kwargs: "Any") -> "Any": rv = real_getresponse(self, *args, **kwargs) span.set_http_status(int(rv.status)) - span.set_data("reason", rv.reason) + if isinstance(span, StreamedSpan): + span.set_attribute("reason", rv.reason) + else: + span.set_data("reason", rv.reason) finally: span.finish() @@ -226,11 +258,24 @@ def sentry_patched_popen_init( env = None - with sentry_sdk.start_span( - op=OP.SUBPROCESS, - name=description, - origin="auto.subprocess.stdlib.subprocess", - ) as span: + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span( + name=description, + ) + span.set_op(OP.SUBPROCESS) + span.set_origin("auto.subprocess.stdlib.subprocess") + else: + span = sentry_sdk.start_span( + op=OP.SUBPROCESS, + name=description, + origin="auto.subprocess.stdlib.subprocess", + ) + + with span: for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers( span=span ): @@ -245,11 +290,18 @@ def sentry_patched_popen_init( env["SUBPROCESS_" + k.upper().replace("-", "_")] = v if cwd: - span.set_data("subprocess.cwd", cwd) + if isinstance(span, StreamedSpan): + span.set_attribute("subprocess.cwd", cwd) + else: + span.set_data("subprocess.cwd", cwd) rv = old_popen_init(self, *a, **kw) - span.set_tag("subprocess.pid", self.pid) + if isinstance(span, StreamedSpan): + span.set_attribute("subprocess.pid", self.pid) + else: + span.set_tag("subprocess.pid", self.pid) + return rv subprocess.Popen.__init__ = sentry_patched_popen_init # type: ignore @@ -260,11 +312,23 @@ def sentry_patched_popen_init( def sentry_patched_popen_wait( self: "subprocess.Popen[Any]", *a: "Any", **kw: "Any" ) -> "Any": - with sentry_sdk.start_span( - op=OP.SUBPROCESS_WAIT, - origin="auto.subprocess.stdlib.subprocess", - ) as span: + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span(name="subprocess popen") + span.set_op(OP.SUBPROCESS_WAIT) + span.set_origin("auto.subprocess.stdlib.subprocess") + span.set_attribute("subprocess.pid", self.pid) + else: + span = sentry_sdk.start_span( + op=OP.SUBPROCESS_WAIT, + origin="auto.subprocess.stdlib.subprocess", + ) span.set_tag("subprocess.pid", self.pid) + + with span: return old_popen_wait(self, *a, **kw) subprocess.Popen.wait = sentry_patched_popen_wait # type: ignore @@ -275,11 +339,25 @@ def sentry_patched_popen_wait( def sentry_patched_popen_communicate( self: "subprocess.Popen[Any]", *a: "Any", **kw: "Any" ) -> "Any": - with sentry_sdk.start_span( - op=OP.SUBPROCESS_COMMUNICATE, - origin="auto.subprocess.stdlib.subprocess", - ) as span: + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span( + name="subprocess communicate", + ) + span.set_op(OP.SUBPROCESS_COMMUNICATE) + span.set_origin("auto.subprocess.stdlib.subprocess") + span.set_attribute("subprocess.pid", self.pid) + else: + span = sentry_sdk.start_span( + op=OP.SUBPROCESS_COMMUNICATE, + origin="auto.subprocess.stdlib.subprocess", + ) span.set_tag("subprocess.pid", self.pid) + + with span: return old_popen_communicate(self, *a, **kw) subprocess.Popen.communicate = sentry_patched_popen_communicate # type: ignore diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index da3c31a967..f25de3a0ef 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -8,7 +8,9 @@ from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable from sentry_sdk.integrations.logging import ignore_logger from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import TransactionSource +from sentry_sdk.tracing_utils import has_span_streaming_enabled from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, @@ -49,11 +51,12 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from typing import Any, Callable, Generator, List, Optional + from typing import Any, Callable, Generator, List, Optional, Union from graphql import GraphQLError, GraphQLResolveInfo from strawberry.http import GraphQLHTTPResponse from strawberry.types import ExecutionContext from sentry_sdk._types import Event, EventProcessor + from sentry_sdk.tracing import Span ignore_logger("strawberry.execution") @@ -181,12 +184,27 @@ def on_operation(self) -> "Generator[None, None, None]": event_processor = _make_request_event_processor(self.execution_context) scope.add_event_processor(event_processor) - span = sentry_sdk.get_current_span() - if span: - self.graphql_span = span.start_child( - op=op, + client = sentry_sdk.get_client() + span_streaming = has_span_streaming_enabled(client.options) + + self.graphql_span: "Union[Span, StreamedSpan]" + if span_streaming: + self.graphql_span = sentry_sdk.traces.start_span( name=description, - origin=StrawberryIntegration.origin, + ) + self.graphql_span.set_op(op) + self.graphql_span.set_origin(StrawberryIntegration.origin) + + self.graphql_span.set_attribute("graphql.operation.type", operation_type) + if self._operation_name: + self.graphql_span.set_attribute( + "graphql.operation.name", self._operation_name + ) + self.graphql_span.set_attribute( + "graphql.document", self.execution_context.query + ) + self.graphql_span.set_attribute( + "graphql.resource_name", self._resource_name ) else: self.graphql_span = sentry_sdk.start_span( @@ -195,38 +213,62 @@ def on_operation(self) -> "Generator[None, None, None]": origin=StrawberryIntegration.origin, ) - self.graphql_span.set_data("graphql.operation.type", operation_type) - self.graphql_span.set_data("graphql.operation.name", self._operation_name) - self.graphql_span.set_data("graphql.document", self.execution_context.query) - self.graphql_span.set_data("graphql.resource_name", self._resource_name) + self.graphql_span.set_data("graphql.operation.type", operation_type) + self.graphql_span.set_data("graphql.operation.name", self._operation_name) + self.graphql_span.set_data("graphql.document", self.execution_context.query) + self.graphql_span.set_data("graphql.resource_name", self._resource_name) yield - transaction = self.graphql_span.containing_transaction - if transaction and self.execution_context.operation_name: - transaction.name = self.execution_context.operation_name - transaction.source = TransactionSource.COMPONENT - transaction.op = op + if self.execution_context.operation_name: + sentry_sdk.get_current_scope().set_transaction_name( + self.execution_context.operation_name, + TransactionSource.COMPONENT, + ) + if isinstance(self.graphql_span, StreamedSpan): + self.graphql_span.segment.set_op(op) + else: + if self.graphql_span.containing_transaction: + self.graphql_span.containing_transaction.op = op self.graphql_span.finish() def on_validate(self) -> "Generator[None, None, None]": - self.validation_span = self.graphql_span.start_child( - op=OP.GRAPHQL_VALIDATE, - name="validation", - origin=StrawberryIntegration.origin, - ) + self.validation_span: "Union[StreamedSpan, Span]" + if isinstance(self.graphql_span, StreamedSpan): + self.validation_span = sentry_sdk.traces.start_span( + parent_span=self.graphql_span, + name="validation", + ) + self.validation_span.set_op(OP.GRAPHQL_VALIDATE) + self.validation_span.set_origin( + StrawberryIntegration.origin, + ) + else: + self.validation_span = self.graphql_span.start_child( + op=OP.GRAPHQL_VALIDATE, + name="validation", + origin=StrawberryIntegration.origin, + ) yield self.validation_span.finish() def on_parse(self) -> "Generator[None, None, None]": - self.parsing_span = self.graphql_span.start_child( - op=OP.GRAPHQL_PARSE, - name="parsing", - origin=StrawberryIntegration.origin, - ) + self.parsing_span: "Union[StreamedSpan, Span]" + if isinstance(self.graphql_span, StreamedSpan): + self.parsing_span = sentry_sdk.traces.start_span( + name="parsing", + ) + self.parsing_span.set_op(OP.GRAPHQL_PARSE) + self.parsing_span.set_origin(StrawberryIntegration.origin) + else: + self.parsing_span = self.graphql_span.start_child( + op=OP.GRAPHQL_PARSE, + name="parsing", + origin=StrawberryIntegration.origin, + ) yield @@ -267,16 +309,27 @@ async def resolve( field_path = "{}.{}".format(info.parent_type, info.field_name) - with self.graphql_span.start_child( - op=OP.GRAPHQL_RESOLVE, - name="resolving {}".format(field_path), - origin=StrawberryIntegration.origin, - ) as span: + span: "Union[StreamedSpan, Span]" + if isinstance(self.graphql_span, StreamedSpan): + span = sentry_sdk.traces.start_span( + parent_span=self.graphql_span, name=f"resolving {field_path}" + ) + span.set_attribute("graphql.field_name", info.field_name) + span.set_attribute("graphql.parent_type", info.parent_type.name) + span.set_attribute("graphql.field_path", field_path) + span.set_attribute("graphql.path", ".".join(map(str, info.path.as_list()))) + else: + span = self.graphql_span.start_child( + op=OP.GRAPHQL_RESOLVE, + name="resolving {}".format(field_path), + origin=StrawberryIntegration.origin, + ) span.set_data("graphql.field_name", info.field_name) span.set_data("graphql.parent_type", info.parent_type.name) span.set_data("graphql.field_path", field_path) span.set_data("graphql.path", ".".join(map(str, info.path.as_list()))) + with span: return await self._resolve(_next, root, info, *args, **kwargs) @@ -294,16 +347,30 @@ def resolve( field_path = "{}.{}".format(info.parent_type, info.field_name) - with self.graphql_span.start_child( - op=OP.GRAPHQL_RESOLVE, - name="resolving {}".format(field_path), - origin=StrawberryIntegration.origin, - ) as span: + span: "Union[StreamedSpan, Span]" + if isinstance(self.graphql_span, StreamedSpan): + span = sentry_sdk.traces.start_span( + parent_span=self.graphql_span, + name="resolving {field_path}", + ) + span.set_op(OP.GRAPHQL_RESOLVE) + span.set_origin(StrawberryIntegration.origin) + span.set_attribute("graphql.field_name", info.field_name) + span.set_attribute("graphql.parent_type", info.parent_type.name) + span.set_attribute("graphql.field_path", field_path) + span.set_attribute("graphql.path", ".".join(map(str, info.path.as_list()))) + else: + span = self.graphql_span.start_child( + op=OP.GRAPHQL_RESOLVE, + name="resolving {}".format(field_path), + origin=StrawberryIntegration.origin, + ) span.set_data("graphql.field_name", info.field_name) span.set_data("graphql.parent_type", info.parent_type.name) span.set_data("graphql.field_path", field_path) span.set_data("graphql.path", ".".join(map(str, info.path.as_list()))) + with span: return _next(root, info, *args, **kwargs) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 6df26690c8..4b064427d2 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -29,9 +29,11 @@ from sentry_sdk.tracing_utils import ( Baggage, has_tracing_enabled, + has_span_streaming_enabled, normalize_incoming_data, PropagationContext, ) +from sentry_sdk.traces import StreamedSpan from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, @@ -601,6 +603,14 @@ def get_trace_context(self) -> "Dict[str, Any]": Returns the Sentry "trace" context from the Propagation Context. """ if has_tracing_enabled(self.get_client().options) and self._span is not None: + if isinstance(self._span, StreamedSpan): + warnings.warn( + "Scope.get_trace_context is not available in streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return {} + return self._span.get_trace_context() # if we are tracing externally (otel), those values take precedence @@ -706,7 +716,7 @@ def clear(self) -> None: self.clear_breadcrumbs() self._should_capture: bool = True - self._span: "Optional[Span]" = None + self._span: "Optional[Union[Span, StreamedSpan]]" = None self._session: "Optional[Session]" = None self._force_auto_session_tracking: "Optional[bool]" = None @@ -758,6 +768,14 @@ def transaction(self) -> "Any": if self._span is None: return None + if isinstance(self._span, StreamedSpan): + warnings.warn( + "Scope.transaction is not available in streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return None + # there is an orphan span on the scope if self._span.containing_transaction is None: return None @@ -787,17 +805,31 @@ def transaction(self, value: "Any") -> None: "Assigning to scope.transaction directly is deprecated: use scope.set_transaction_name() instead." ) self._transaction = value - if self._span and self._span.containing_transaction: - self._span.containing_transaction.name = value + if self._span: + if isinstance(self._span, StreamedSpan): + warnings.warn( + "Scope.transaction is not available in streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return None + + if self._span.containing_transaction: + self._span.containing_transaction.name = value def set_transaction_name(self, name: str, source: "Optional[str]" = None) -> None: """Set the transaction name and optionally the transaction source.""" self._transaction = name + if self._span: + if isinstance(self._span, StreamedSpan): + self._span.segment.name = name + if source: + self._span.segment.set_source(source) - if self._span and self._span.containing_transaction: - self._span.containing_transaction.name = name - if source: - self._span.containing_transaction.source = source + elif self._span.containing_transaction: + self._span.containing_transaction.name = name + if source: + self._span.containing_transaction.source = source if source: self._transaction_info["source"] = source @@ -820,12 +852,12 @@ def set_user(self, value: "Optional[Dict[str, Any]]") -> None: session.update(user=value) @property - def span(self) -> "Optional[Span]": + def span(self) -> "Optional[Union[Span, StreamedSpan]]": """Get/set current tracing span or transaction.""" return self._span @span.setter - def span(self, span: "Optional[Span]") -> None: + def span(self, span: "Optional[Union[Span, StreamedSpan]]") -> None: self._span = span # XXX: this differs from the implementation in JS, there Scope.setSpan # does not set Scope._transactionName. @@ -1114,6 +1146,15 @@ def start_span( be removed in the next major version. Going forward, it should only be used by the SDK itself. """ + client = sentry_sdk.get_client() + if has_span_streaming_enabled(client.options): + warnings.warn( + "Scope.start_span is not available in streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return NoOpSpan() + if kwargs.get("description") is not None: warnings.warn( "The `description` parameter is deprecated. Please use `name` instead.", @@ -1133,6 +1174,9 @@ def start_span( # get current span or transaction span = self.span or self.get_isolation_scope().span + if isinstance(span, StreamedSpan): + # make mypy happy + return NoOpSpan() if span is None: # New spans get the `trace_id` from the scope @@ -1147,6 +1191,74 @@ def start_span( return span + def start_streamed_span( + self, + name: str, + attributes: "Optional[Attributes]" = None, + parent_span: "Optional[StreamedSpan]" = None, + ) -> "StreamedSpan": + # TODO: rename to start_span once we drop the old API + if parent_span is None: + # Get currently active span + # TODO[span-first]: should this be current scope? + parent_span = self.span or self.get_isolation_scope().span # type: ignore + + # If no specific parent_span provided and there is no currently + # active span, this is a segment + if parent_span is None: + propagation_context = self.get_active_propagation_context() + + span = StreamedSpan( + name=name, + attributes=attributes, + scope=self, + segment=None, + trace_id=propagation_context.trace_id, + parent_span_id=propagation_context.parent_span_id, + parent_sampled=propagation_context.parent_sampled, + baggage=propagation_context.baggage, + ) + + return span + + # This is a child span; take propagation context from the parent span + with new_scope(): + span = StreamedSpan( + name=name, + attributes=attributes, + scope=self, + trace_id=parent_span.trace_id, + parent_span_id=parent_span.span_id, + parent_sampled=parent_span.sampled, + segment=parent_span.segment, + # XXX[span-first]: baggage? + ) + + return span + + def _start_profile_on_segment(self, span: "StreamedSpan") -> None: + try_autostart_continuous_profiler() + + if not span.sampled: + return + + span._continuous_profile = try_profile_lifecycle_trace_start() + + # Typically, the profiler is set when the segment is created. But when + # using the auto lifecycle, the profiler isn't running when the first + # segment is started. So make sure we update the profiler id on it. + if span._continuous_profile is not None: + span._set_profile_id(get_profiler_id()) + + def _update_sample_rate_from_segment(self, span: "StreamedSpan") -> None: + # If we had to adjust the sample rate when setting the sampling decision + # for the spans, it needs to be updated in the propagation context too + propagation_context = self.get_active_propagation_context() + baggage = propagation_context.baggage + + if baggage is not None: + baggage.sentry_items["sample_rate"] = str(span.sample_rate) + def continue_trace( self, environ_or_headers: "Dict[str, Any]", @@ -1180,6 +1292,9 @@ def continue_trace( **optional_kwargs, ) + def set_propagation_context(self, environ_or_headers: "dict[str, Any]") -> None: + self.generate_propagation_context(environ_or_headers) + def capture_event( self, event: "Event", @@ -1253,6 +1368,17 @@ def _capture_metric(self, metric: "Optional[Metric]") -> None: client._capture_metric(metric, scope=merged_scope) + def _capture_span(self, span: "Optional[StreamedSpan]") -> None: + if span is None: + return + + client = self.get_client() + if not has_span_streaming_enabled(client.options): + return + + merged_scope = self._merge_scopes() + client._capture_span(span, scope=merged_scope) + def capture_message( self, message: str, @@ -1497,16 +1623,25 @@ def _apply_flags_to_event( ) def _apply_scope_attributes_to_telemetry( - self, telemetry: "Union[Log, Metric]" + self, telemetry: "Union[Log, Metric, StreamedSpan]" ) -> None: + # TODO: turn Logs, Metrics into actual classes + if isinstance(telemetry, dict): + attributes = telemetry["attributes"] + else: + attributes = telemetry.attributes + for attribute, value in self._attributes.items(): - if attribute not in telemetry["attributes"]: - telemetry["attributes"][attribute] = value + if attribute not in attributes: + attributes[attribute] = value def _apply_user_attributes_to_telemetry( - self, telemetry: "Union[Log, Metric]" + self, telemetry: "Union[Log, Metric, StreamedSpan]" ) -> None: - attributes = telemetry["attributes"] + if isinstance(telemetry, dict): + attributes = telemetry["attributes"] + else: + attributes = telemetry.attributes if not should_send_default_pii() or self._user is None: return @@ -1626,16 +1761,19 @@ def apply_to_event( return event @_disable_capture - def apply_to_telemetry(self, telemetry: "Union[Log, Metric]") -> None: + def apply_to_telemetry(self, telemetry: "Union[Log, Metric, StreamedSpan]") -> None: # Attributes-based events and telemetry go through here (logs, metrics, # spansV2) - trace_context = self.get_trace_context() - trace_id = trace_context.get("trace_id") - if telemetry.get("trace_id") is None: - telemetry["trace_id"] = trace_id or "00000000-0000-0000-0000-000000000000" - span_id = trace_context.get("span_id") - if telemetry.get("span_id") is None and span_id: - telemetry["span_id"] = span_id + if not isinstance(telemetry, StreamedSpan): + trace_context = self.get_trace_context() + trace_id = trace_context.get("trace_id") + if telemetry.get("trace_id") is None: + telemetry["trace_id"] = ( + trace_id or "00000000-0000-0000-0000-000000000000" + ) + span_id = trace_context.get("span_id") + if telemetry.get("span_id") is None and span_id: + telemetry["span_id"] = span_id self._apply_scope_attributes_to_telemetry(telemetry) self._apply_user_attributes_to_telemetry(telemetry) diff --git a/sentry_sdk/traces.py b/sentry_sdk/traces.py new file mode 100644 index 0000000000..2a0a1dd962 --- /dev/null +++ b/sentry_sdk/traces.py @@ -0,0 +1,699 @@ +""" +The API in this file is only meant to be used in span streaming mode. + +You can enable span streaming mode via +sentry_sdk.init(_experiments={"trace_lifecycle": "stream"}). +""" + +import uuid +from datetime import datetime, timedelta, timezone +from enum import Enum +from typing import TYPE_CHECKING + +import sentry_sdk +from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.profiler.continuous_profiler import get_profiler_id +from sentry_sdk.tracing_utils import ( + Baggage, + _generate_sample_rand, + has_span_streaming_enabled, + has_tracing_enabled, +) +from sentry_sdk.utils import ( + capture_internal_exceptions, + format_attribute, + get_current_thread_meta, + is_valid_sample_rate, + logger, + nanosecond_time, + should_be_treated_as_error, +) + +if TYPE_CHECKING: + from typing import Any, Callable, Iterator, Optional, ParamSpec, TypeVar, Union + from sentry_sdk._types import Attributes, AttributeValue, SamplingContext + from sentry_sdk.profiler.continuous_profiler import ContinuousProfile + + P = ParamSpec("P") + R = TypeVar("R") + + +FLAGS_CAPACITY = 10 + +BAGGAGE_HEADER_NAME = "baggage" +SENTRY_TRACE_HEADER_NAME = "sentry-trace" + + +class SpanStatus(str, Enum): + OK = "ok" + ERROR = "error" + + def __str__(self) -> str: + return self.value + + +# Segment source, see +# https://getsentry.github.io/sentry-conventions/generated/attributes/sentry.html#sentryspansource +class SegmentSource(str, Enum): + COMPONENT = "component" + CUSTOM = "custom" + ROUTE = "route" + TASK = "task" + URL = "url" + VIEW = "view" + + def __str__(self) -> str: + return self.value + + +# These are typically high cardinality and the server hates them +LOW_QUALITY_SEGMENT_SOURCES = [ + SegmentSource.URL, +] + + +SOURCE_FOR_STYLE = { + "endpoint": SegmentSource.COMPONENT, + "function_name": SegmentSource.COMPONENT, + "handler_name": SegmentSource.COMPONENT, + "method_and_path_pattern": SegmentSource.ROUTE, + "path": SegmentSource.URL, + "route_name": SegmentSource.COMPONENT, + "route_pattern": SegmentSource.ROUTE, + "uri_template": SegmentSource.ROUTE, + "url": SegmentSource.ROUTE, +} + +""" +TODO[span-first] / notes +- redis, http, subprocess breadcrumbs (maybe_create_breadcrumbs_from_span) work + on op, change or ignore? +- tags +- initial status: OK? or unset? -> OK +- dropped spans are not migrated +- recheck transaction.finish <-> Streamedspan.end +- profiling: drop transaction based +- profiling: actually send profiles +- maybe: use getters/setter OR properties but not both +- add size-based flushing to buffer(s) +- migrate transaction sample_rand logic +- remove deprecated profiler impl +- custom_sampling_context? + - store on scope/propagation context instead? + - function to set on propagation context +- noop spans +- iso +- check where we're auto filtering out spans in integrations (health checks etc?) + +Notes: +- removed ability to provide a start_timestamp +- moved _flags_capacity to a const +""" + + +def start_span( + name: str, + attributes: "Optional[Attributes]" = None, + parent_span: "Optional[StreamedSpan]" = None, +) -> "StreamedSpan": + """ + Start a span. + + The span's parent, unless provided explicitly via the `parent_span` argument, + will be the currently active span, if any. + + `start_span()` can either be used as context manager or you can use the span + object it returns and explicitly start and end it via the `span.start()` and + `span.end()` interface. The following is equivalent: + + ```python + import sentry_sdk + + with sentry_sdk.traces.start_span(name="My Span"): + # do something + + # The span automatically finishes once the `with` block is exited + ``` + + ```python + import sentry_sdk + + span = sentry_sdk.traces.start_span(name="My Span") + span.start() + # do something + span.end() + ``` + + To continue a trace from another service, call + sentry_sdk.traces.continue_trace() prior to creating the top-level span. + + :param name: The name to identify this span by. + :type name: str + :param attributes: Key-value attributes to set on the span from the start. + When provided via the `start_span()` function, these will also be + accessible in the traces sampler. + :type attributes: "Optional[Attributes]" + :param parent_span: A span instance that the new span should be parented to. + If not provided, the parent will be set to the currently active span, + if any. + :type parent_span: "Optional[StreamedSpan]" + :return: A span. + :rtype: StreamedSpan + """ + return sentry_sdk.get_current_scope().start_streamed_span( + name, attributes, parent_span + ) + + +def continue_trace(incoming: "dict[str, Any]") -> None: + """ + Continue a trace from headers or environment variables. + + This function sets the propagation context on the scope. Any span started + in the updated scope will belong under the trace extracted from the + provided propagation headers or environment variables. + + continue_trace() doesn't start any spans on its own. + """ + # This is set both on the isolation and the current scope for compatibility + # reasons. Conceptually, it belongs on the isolation scope, and it also + # used to be set there in non-span-first mode. But in span first mode, we + # start segments on the current span, like JS does, so we need to set the + # propagation context there. + sentry_sdk.get_isolation_scope().generate_propagation_context( + incoming, + ) + return sentry_sdk.get_current_scope().generate_propagation_context( + incoming, + ) + + +def new_trace() -> None: + """ + Resets the propagation context, forcing a new trace. + + This function sets the propagation context on the scope. Any span started + in the updated scope will start its own trace. + + new_trace() doesn't start any spans on its own. + """ + sentry_sdk.get_current_scope().set_new_propagation_context() + + +class NoOpStreamedSpan: + pass + + +class StreamedSpan: + """ + A span holds timing information of a block of code. + + Spans can have multiple child spans thus forming a span tree. + + This is the Span First span implementation. The original transaction-based + span implementation lives in tracing.Span. + """ + + __slots__ = ( + "name", + "attributes", + "_span_id", + "_trace_id", + "parent_span_id", + "segment", + "_sampled", + "parent_sampled", + "start_timestamp", + "timestamp", + "status", + "_start_timestamp_monotonic_ns", + "_scope", + "_flags", + "_context_manager_state", + "_profile", + "_continuous_profile", + "_baggage", + "sample_rate", + "_sample_rand", + "_finished", + ) + + def __init__( + self, + *, + name: str, + scope: "sentry_sdk.Scope", + attributes: "Optional[Attributes]" = None, + # TODO[span-first]: would be good to actually take this propagation + # context stuff directly from the PropagationContext, but for that + # we'd actually need to refactor PropagationContext to stay in sync + # with what's going on (e.g. update the current span_id) and not just + # update when a trace is continued + trace_id: "Optional[str]" = None, + parent_span_id: "Optional[str]" = None, + parent_sampled: "Optional[bool]" = None, + baggage: "Optional[Baggage]" = None, + segment: "Optional[StreamedSpan]" = None, + ) -> None: + self._scope = scope + + self.name: str = name + self.attributes: "Attributes" = attributes or {} + + self._trace_id = trace_id + self.parent_span_id = parent_span_id + self.parent_sampled = parent_sampled + self.segment = segment or self + + self.start_timestamp = datetime.now(timezone.utc) + + try: + # profiling depends on this value and requires that + # it is measured in nanoseconds + self._start_timestamp_monotonic_ns = nanosecond_time() + except AttributeError: + pass + + self.timestamp: "Optional[datetime]" = None + self._finished: bool = False + self._span_id: "Optional[str]" = None + + self.status: SpanStatus = SpanStatus.OK + self.set_source(SegmentSource.CUSTOM) + # XXX[span-first] ^ populate this correctly + + self._sampled: "Optional[bool]" = None + self.sample_rate: "Optional[float]" = None + + # XXX[span-first]: just do this for segments? + self._baggage = baggage + baggage_sample_rand = ( + None if self._baggage is None else self._baggage._sample_rand() + ) + if baggage_sample_rand is not None: + self._sample_rand = baggage_sample_rand + else: + self._sample_rand = _generate_sample_rand(self.trace_id) + + self._flags: dict[str, bool] = {} + self._profile = None + self._continuous_profile: "Optional[ContinuousProfile]" = None + + self._update_active_thread() + self._set_profile_id(get_profiler_id()) + + def __repr__(self) -> str: + return ( + f"<{self.__class__.__name__}(" + f"name={self.name}, " + f"trace_id={self.trace_id}, " + f"span_id={self.span_id}, " + f"parent_span_id={self.parent_span_id}, " + f"sampled={self.sampled})>" + ) + + def __enter__(self) -> "StreamedSpan": + scope = self._scope or sentry_sdk.get_current_scope() + old_span = scope.span + scope.span = self + self._context_manager_state = (scope, old_span) + + if self.is_segment(): + sampling_context = { + "transaction_context": { + "trace_id": self.trace_id, + "span_id": self.span_id, + "parent_span_id": self.parent_span_id, + }, + "parent_sampled": self.parent_sampled, + "attributes": self.attributes, + } + # Use traces_sample_rate, traces_sampler, and/or inheritance to make a + # sampling decision + self._set_sampling_decision(sampling_context=sampling_context) + + scope._update_sample_rate_from_segment(self) + scope._start_profile_on_segment(self) + + return self + + def __exit__( + self, ty: "Optional[Any]", value: "Optional[Any]", tb: "Optional[Any]" + ) -> None: + if self.is_segment(): + if self._profile is not None: + self._profile.__exit__(ty, value, tb) + + if self._continuous_profile is not None: + self._continuous_profile.stop() + + if value is not None and should_be_treated_as_error(ty, value): + self.set_status(SpanStatus.ERROR) + + with capture_internal_exceptions(): + scope, old_span = self._context_manager_state + del self._context_manager_state + self._end(scope=scope) + scope.span = old_span + + def start(self) -> "StreamedSpan": + """ + Start this span. + + Only usable if the span was not started via the `with start_span():` + context manager, since that starts it automatically. + """ + return self.__enter__() + + def finish(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None: + """ + Finish this span and queue it for sending. + + :param end_timestamp: End timestamp to use instead of current time. + :type end_timestamp: "Optional[Union[float, datetime]]" + """ + try: + if end_timestamp: + if isinstance(end_timestamp, float): + end_timestamp = datetime.fromtimestamp(end_timestamp, timezone.utc) + self.timestamp = end_timestamp + except AttributeError: + pass + + self.__exit__(None, None, None) + + def _end( + self, + scope: "Optional[sentry_sdk.Scope]" = None, + ) -> None: + client = sentry_sdk.get_client() + if not client.is_active(): + return + + self._set_segment_attributes() + + scope: "Optional[sentry_sdk.Scope]" = ( + scope or self._scope or sentry_sdk.get_current_scope() + ) + + # Explicit check against False needed because self.sampled might be None + if self.sampled is False: + logger.debug("Discarding span because sampled = False") + + # This is not entirely accurate because discards here are not + # exclusively based on sample rate but also traces sampler, but + # we handle this the same here. + if client.transport and has_tracing_enabled(client.options): + if client.monitor and client.monitor.downsample_factor > 0: + reason = "backpressure" + else: + reason = "sample_rate" + + client.transport.record_lost_event(reason, data_category="span") + + return + + if self.sampled is None: + logger.warning("Discarding transaction without sampling decision.") + + if self._finished is True: + # This span is already finished, ignore. + return + + if self.timestamp is None: + try: + elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns + self.timestamp = self.start_timestamp + timedelta( + microseconds=elapsed / 1000 + ) + except AttributeError: + self.timestamp = datetime.now(timezone.utc) + + if self.segment.sampled: # XXX this should just use its own sampled + sentry_sdk.get_current_scope()._capture_span(self) + + self._finished = True + + def get_attributes(self) -> "Attributes": + return self.attributes + + def set_attribute(self, key: str, value: "AttributeValue") -> None: + self.attributes[key] = format_attribute(value) + + def set_attributes(self, attributes: "Attributes") -> None: + for key, value in attributes.items(): + self.set_attribute(key, value) + + def set_status(self, status: SpanStatus) -> None: + self.status = status + + def get_name(self) -> str: + return self.name + + def set_name(self, name: str) -> None: + self.name = name + + def set_flag(self, flag: str, result: bool) -> None: + if len(self._flags) < FLAGS_CAPACITY: + self._flags[flag] = result + + def set_op(self, op: str) -> None: + self.set_attribute("sentry.op", op) + + def set_origin(self, origin: str) -> None: + self.set_attribute("sentry.origin", origin) + + def set_source(self, source: "Union[str, SegmentSource]") -> None: + if isinstance(source, Enum): + source = source.value + + self.set_attribute("sentry.span.source", source) + + def is_segment(self) -> bool: + return self.segment == self + + @property + def span_id(self) -> str: + if not self._span_id: + self._span_id = uuid.uuid4().hex[16:] + + return self._span_id + + @property + def trace_id(self) -> str: + if not self._trace_id: + self._trace_id = uuid.uuid4().hex + + return self._trace_id + + @property + def sampled(self) -> "Optional[bool]": + if self._sampled is not None: + return self._sampled + + if not self.is_segment(): + self._sampled = self.parent_sampled + + return self._sampled + + def dynamic_sampling_context(self) -> "dict[str, str]": + return self.segment.get_baggage().dynamic_sampling_context() + + def to_traceparent(self) -> str: + if self.sampled is True: + sampled = "1" + elif self.sampled is False: + sampled = "0" + else: + sampled = None + + traceparent = "%s-%s" % (self.trace_id, self.span_id) + if sampled is not None: + traceparent += "-%s" % (sampled,) + + return traceparent + + def to_baggage(self) -> "Optional[Baggage]": + if self.segment: + return self.segment.get_baggage() + return None + + def iter_headers(self) -> "Iterator[tuple[str, str]]": + if not self.segment: + return + + yield SENTRY_TRACE_HEADER_NAME, self.to_traceparent() + + baggage = self.segment.get_baggage().serialize() + if baggage: + yield BAGGAGE_HEADER_NAME, baggage + + def _update_active_thread(self) -> None: + thread_id, thread_name = get_current_thread_meta() + self._set_thread(thread_id, thread_name) + + def _set_thread( + self, thread_id: "Optional[int]", thread_name: "Optional[str]" + ) -> None: + if thread_id is not None: + self.set_attribute(SPANDATA.THREAD_ID, str(thread_id)) + + if thread_name is not None: + self.set_attribute(SPANDATA.THREAD_NAME, thread_name) + + def _set_profile_id(self, profiler_id: "Optional[str]") -> None: + if profiler_id is not None: + self.set_attribute("sentry.profiler_id", profiler_id) + + def set_http_status(self, http_status: int) -> None: + self.set_attribute(SPANDATA.HTTP_STATUS_CODE, http_status) + + if http_status >= 400: + self.set_status(SpanStatus.ERROR) + else: + self.set_status(SpanStatus.OK) + + def get_baggage(self) -> "Baggage": + """ + Return the :py:class:`~sentry_sdk.tracing_utils.Baggage` associated with + the segment. + + The first time a new baggage with Sentry items is made, it will be frozen. + """ + if not self._baggage or self._baggage.mutable: + self._baggage = Baggage.populate_from_segment(self) + + return self._baggage + + def _set_sampling_decision(self, sampling_context: "SamplingContext") -> None: + """ + Set the segment's sampling decision, inherited by all child spans. + """ + client = sentry_sdk.get_client() + + # nothing to do if tracing is disabled + if not has_tracing_enabled(client.options): + self._sampled = False + return + + if not self.is_segment(): + return + + traces_sampler_defined = callable(client.options.get("traces_sampler")) + + # We would have bailed already if neither `traces_sampler` nor + # `traces_sample_rate` were defined, so one of these should work; prefer + # the hook if so + if traces_sampler_defined: + sample_rate = client.options["traces_sampler"](sampling_context) + else: + if sampling_context["parent_sampled"] is not None: + sample_rate = sampling_context["parent_sampled"] + else: + sample_rate = client.options["traces_sample_rate"] + + # Since this is coming from the user (or from a function provided by the + # user), who knows what we might get. (The only valid values are + # booleans or numbers between 0 and 1.) + if not is_valid_sample_rate(sample_rate, source="Tracing"): + logger.warning( + f"[Tracing] Discarding {self.name} because of invalid sample rate." + ) + self._sampled = False + return + + self.sample_rate = float(sample_rate) + + if client.monitor: + self.sample_rate /= 2**client.monitor.downsample_factor + + # if the function returned 0 (or false), or if `traces_sample_rate` is + # 0, it's a sign the transaction should be dropped + if not self.sample_rate: + if traces_sampler_defined: + reason = "traces_sampler returned 0 or False" + else: + reason = "traces_sample_rate is set to 0" + + logger.debug(f"[Tracing] Discarding {self.name} because {reason}") + self._sampled = False + return + + # Now we roll the dice. + self._sampled = self._sample_rand < self.sample_rate + + if self.sampled: + logger.debug(f"[Tracing] Starting {self.name}") + else: + logger.debug( + f"[Tracing] Discarding {self.name} because it's not included in the random sample (sampling rate = {self.sample_rate})" + ) + + def _set_segment_attributes(self) -> None: + if not self.is_segment(): + self.set_attribute("sentry.segment.id", self.segment.span_id) + + self.set_attribute("sentry.segment.name", self.segment.name) + + +def trace( + func: "Optional[Callable[P, R]]" = None, + *, + name: "Optional[str]" = None, + attributes: "Optional[dict[str, Any]]" = None, +) -> "Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]": + """ + Decorator to start a span around a function call. + + This decorator automatically creates a new span when the decorated function + is called, and finishes the span when the function returns or raises an exception. + + :param func: The function to trace. When used as a decorator without parentheses, + this is the function being decorated. When used with parameters (e.g., + ``@trace(op="custom")``, this should be None. + :type func: Callable or None + + :param name: The human-readable name/description for the span. If not provided, + defaults to the function name. This provides more specific details about + what the span represents (e.g., "GET /api/users", "process_user_data"). + :type name: str or None + + :param attributes: A dictionary of key-value pairs to add as attributes to the span. + Attribute values must be strings, integers, floats, or booleans. These + attributes provide additional context about the span's execution. + :type attributes: dict[str, Any] or None + + :returns: When used as ``@trace``, returns the decorated function. When used as + ``@trace(...)`` with parameters, returns a decorator function. + :rtype: Callable or decorator function + + Example:: + + import sentry_sdk + + # Simple usage with default values + @sentry_sdk.trace + def process_data(): + # Function implementation + pass + + # With custom parameters + @sentry_sdk.trace( + name="Get user data", + attributes={"postgres": True} + ) + def make_db_query(sql): + # Function implementation + pass + """ + from sentry_sdk.tracing_utils import create_streaming_span_decorator + + decorator = create_streaming_span_decorator( + name=name, + attributes=attributes, + ) + + if func: + return decorator(func) + else: + return decorator diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 742582423b..c127d96f75 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -4,11 +4,12 @@ import os import re import sys +import uuid +import warnings from collections.abc import Mapping, MutableMapping from datetime import timedelta from random import Random from urllib.parse import quote, unquote -import uuid import sentry_sdk from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS, SPANTEMPLATE @@ -106,6 +107,13 @@ def has_tracing_enabled(options: "Optional[Dict[str, Any]]") -> bool: ) +def has_span_streaming_enabled(options: "Optional[dict[str, Any]]") -> bool: + if options is None: + return False + + return (options.get("_experiments") or {}).get("trace_lifecycle") == "stream" + + @contextlib.contextmanager def record_sql_queries( cursor: "Any", @@ -115,9 +123,10 @@ def record_sql_queries( executemany: bool, record_cursor_repr: bool = False, span_origin: str = "manual", -) -> "Generator[sentry_sdk.tracing.Span, None, None]": +) -> "Generator[Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan], None, None]": # TODO: Bring back capturing of params by default - if sentry_sdk.get_client().options["_experiments"].get("record_sql_params", False): + client = sentry_sdk.get_client() + if client.options["_experiments"].get("record_sql_params", False): if not params_list or params_list == [None]: params_list = None @@ -127,6 +136,8 @@ def record_sql_queries( params_list = None paramstyle = None + span_streaming = has_span_streaming_enabled(client.options) + query = _format_sql(cursor, query) data = {} @@ -142,13 +153,24 @@ def record_sql_queries( with capture_internal_exceptions(): sentry_sdk.add_breadcrumb(message=query, category="query", data=data) - with sentry_sdk.start_span( - op=OP.DB, - name=query, - origin=span_origin, - ) as span: + span: "Optional[Union[Span, StreamedSpan]]" = None + if span_streaming: + span = sentry_sdk.traces.start_span(name=query or "query") + span.set_op(OP.DB) + span.set_origin(span_origin) + else: + span = sentry_sdk.start_span( + op=OP.DB, + name=query, + origin=span_origin, + ) + + with span: for k, v in data.items(): - span.set_data(k, v) + if isinstance(span, StreamedSpan): + span.set_attribute(k, v) + else: + span.set_data(k, v) yield span @@ -212,7 +234,7 @@ def _should_be_included( def add_source( - span: "sentry_sdk.tracing.Span", + span: "Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan]", project_root: "Optional[str]", in_app_include: "Optional[list[str]]", in_app_exclude: "Optional[list[str]]", @@ -250,20 +272,25 @@ def add_source( frame = None # Set the data + if isinstance(span, StreamedSpan): + set_on_span = span.set_attribute + else: + set_on_span = span.set_data + if frame is not None: try: lineno = frame.f_lineno except Exception: lineno = None if lineno is not None: - span.set_data(SPANDATA.CODE_LINENO, frame.f_lineno) + set_on_span(SPANDATA.CODE_LINENO, frame.f_lineno) try: namespace = frame.f_globals.get("__name__") except Exception: namespace = None if namespace is not None: - span.set_data(SPANDATA.CODE_NAMESPACE, namespace) + set_on_span(SPANDATA.CODE_NAMESPACE, namespace) filepath = _get_frame_module_abs_path(frame) if filepath is not None: @@ -273,7 +300,8 @@ def add_source( in_app_path = filepath.replace(project_root, "").lstrip(os.sep) else: in_app_path = filepath - span.set_data(SPANDATA.CODE_FILEPATH, in_app_path) + if in_app_path: + set_on_span(SPANDATA.CODE_FILEPATH, in_app_path) try: code_function = frame.f_code.co_name @@ -281,10 +309,12 @@ def add_source( code_function = None if code_function is not None: - span.set_data(SPANDATA.CODE_FUNCTION, frame.f_code.co_name) + set_on_span(SPANDATA.CODE_FUNCTION, frame.f_code.co_name) -def add_query_source(span: "sentry_sdk.tracing.Span") -> None: +def add_query_source( + span: "Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan]", +) -> None: """ Adds OTel compatible source code information to a database query span """ @@ -314,7 +344,9 @@ def add_query_source(span: "sentry_sdk.tracing.Span") -> None: ) -def add_http_request_source(span: "sentry_sdk.tracing.Span") -> None: +def add_http_request_source( + span: "Union[sentry_sdk.tracing.Span, sentry_sdk.traces.StreamedSpan]", +) -> None: """ Adds OTel compatible source code information to a span for an outgoing HTTP request """ @@ -742,6 +774,55 @@ def populate_from_transaction( return Baggage(sentry_items, mutable=False) + @classmethod + def populate_from_segment(cls, segment: "StreamedSpan") -> "Baggage": + """ + Populate fresh baggage entry with sentry_items and make it immutable + if this is the head SDK which originates traces. + """ + client = sentry_sdk.get_client() + sentry_items: "Dict[str, str]" = {} + + if not client.is_active(): + return Baggage(sentry_items) + + options = client.options or {} + + sentry_items["trace_id"] = segment.trace_id + sentry_items["sample_rand"] = f"{segment._sample_rand:.6f}" # noqa: E231 + + if options.get("environment"): + sentry_items["environment"] = options["environment"] + + if options.get("release"): + sentry_items["release"] = options["release"] + + if client.parsed_dsn: + sentry_items["public_key"] = client.parsed_dsn.public_key + if client.parsed_dsn.org_id: + sentry_items["org_id"] = client.parsed_dsn.org_id + + if ( + segment.get_attributes().get("sentry.span.source") + not in LOW_QUALITY_SEGMENT_SOURCES + ): + sentry_items["transaction"] = segment.name + + if segment.sample_rate is not None: + sentry_items["sample_rate"] = str(segment.sample_rate) + + if segment.sampled is not None: + sentry_items["sampled"] = "true" if segment.sampled else "false" + + # There's an existing baggage but it was mutable, which is why we are + # creating this new baggage. + # However, if by chance the user put some sentry items in there, give + # them precedence. + if segment._baggage and segment._baggage.sentry_items: + sentry_items.update(segment._baggage.sentry_items) + + return Baggage(sentry_items, mutable=False) + def freeze(self) -> None: self.mutable = False @@ -865,6 +946,14 @@ async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any": ) return await f(*args, **kwargs) + if isinstance(current_span, StreamedSpan): + warnings.warn( + "Use the @sentry_sdk.traces.trace decorator in span streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return await f(*args, **kwargs) + span_op = op or _get_span_op(template) function_name = name or qualname_from_function(f) or "" span_name = _get_span_name(template, function_name, kwargs) @@ -902,6 +991,14 @@ def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any": ) return f(*args, **kwargs) + if isinstance(current_span, StreamedSpan): + warnings.warn( + "Use the @sentry_sdk.traces.trace decorator in span streaming mode.", + DeprecationWarning, + stacklevel=2, + ) + return f(*args, **kwargs) + span_op = op or _get_span_op(template) function_name = name or qualname_from_function(f) or "" span_name = _get_span_name(template, function_name, kwargs) @@ -935,7 +1032,61 @@ def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any": return span_decorator -def get_current_span(scope: "Optional[sentry_sdk.Scope]" = None) -> "Optional[Span]": +def create_streaming_span_decorator( + name: "Optional[str]" = None, + attributes: "Optional[dict[str, Any]]" = None, +) -> "Any": + """ + Create a span decorator that can wrap both sync and async functions. + + :param name: The name of the span. + :type name: str or None + :param attributes: Additional attributes to set on the span. + :type attributes: dict or None + """ + from sentry_sdk.scope import should_send_default_pii + + def span_decorator(f: "Any") -> "Any": + """ + Decorator to create a span for the given function. + """ + + @functools.wraps(f) + async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any": + span_name = name or qualname_from_function(f) or "" + + with start_streaming_span(name=span_name, attributes=attributes): + result = await f(*args, **kwargs) + return result + + try: + async_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined] + except Exception: + pass + + @functools.wraps(f) + def sync_wrapper(*args: "Any", **kwargs: "Any") -> "Any": + span_name = name or qualname_from_function(f) or "" + + with start_streaming_span(name=span_name, attributes=attributes): + return f(*args, **kwargs) + + try: + sync_wrapper.__signature__ = inspect.signature(f) # type: ignore[attr-defined] + except Exception: + pass + + if inspect.iscoroutinefunction(f): + return async_wrapper + else: + return sync_wrapper + + return span_decorator + + +def get_current_span( + scope: "Optional[sentry_sdk.Scope]" = None, +) -> "Optional[Union[Span, StreamedSpan]]": """ Returns the currently active span if there is one running, otherwise `None` """ @@ -944,16 +1095,24 @@ def get_current_span(scope: "Optional[sentry_sdk.Scope]" = None) -> "Optional[Sp return current_span -def set_span_errored(span: "Optional[Span]" = None) -> None: +def set_span_errored(span: "Optional[Union[Span, StreamedSpan]]" = None) -> None: """ - Set the status of the current or given span to INTERNAL_ERROR. - Also sets the status of the transaction (root span) to INTERNAL_ERROR. + Set the status of the current or given span to error. + Also sets the status of the transaction (root span) to error. """ + from sentry_sdk.traces import StreamedSpan, SpanStatus + span = span or get_current_span() + if span is not None: - span.set_status(SPANSTATUS.INTERNAL_ERROR) - if span.containing_transaction is not None: - span.containing_transaction.set_status(SPANSTATUS.INTERNAL_ERROR) + if isinstance(span, Span): + span.set_status(SPANSTATUS.INTERNAL_ERROR) + if span.containing_transaction is not None: + span.containing_transaction.set_status(SPANSTATUS.INTERNAL_ERROR) + elif isinstance(span, StreamedSpan): + span.set_status(SpanStatus.ERROR) + if span.segment is not None: + span.segment.set_status(SpanStatus.ERROR) def _generate_sample_rand( @@ -1309,7 +1468,11 @@ def add_sentry_baggage_to_headers( BAGGAGE_HEADER_NAME, LOW_QUALITY_TRANSACTION_SOURCES, SENTRY_TRACE_HEADER_NAME, + Span, ) -if TYPE_CHECKING: - from sentry_sdk.tracing import Span +from sentry_sdk.traces import ( + LOW_QUALITY_SEGMENT_SOURCES, + start_span as start_streaming_span, + StreamedSpan, +) diff --git a/sentry_sdk/transport.py b/sentry_sdk/transport.py index cee4fa882b..48778839b1 100644 --- a/sentry_sdk/transport.py +++ b/sentry_sdk/transport.py @@ -445,7 +445,6 @@ def _send_envelope(self: "Self", envelope: "Envelope") -> None: envelope.items.append(client_report_item) content_encoding, body = self._serialize_envelope(envelope) - assert self.parsed_dsn is not None logger.debug( "Sending envelope [%s] project:%s host:%s", diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 2fbca486de..2a689d3df1 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -14,6 +14,7 @@ from collections import namedtuple from datetime import datetime, timezone from decimal import Decimal +from enum import Enum from functools import partial, partialmethod, wraps from numbers import Real from urllib.parse import parse_qs, unquote, urlencode, urlsplit, urlunsplit