|
| 1 | +import threading |
| 2 | +from collections import defaultdict |
| 3 | +from datetime import datetime, timezone |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from sentry_sdk._batcher import Batcher |
| 7 | +from sentry_sdk.consts import SPANSTATUS |
| 8 | +from sentry_sdk.envelope import Envelope, Item, PayloadRef |
| 9 | +from sentry_sdk.utils import format_timestamp, serialize_attribute, safe_repr |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from typing import Any, Callable, Optional |
| 13 | + from sentry_sdk.traces import StreamedSpan |
| 14 | + from sentry_sdk._types import SerializedAttributeValue |
| 15 | + |
| 16 | + |
| 17 | +class SpanBatcher(Batcher["StreamedSpan"]): |
| 18 | + # TODO[span-first]: size-based flushes |
| 19 | + # TODO[span-first]: adjust flush/drop defaults |
| 20 | + MAX_BEFORE_FLUSH = 1000 |
| 21 | + MAX_BEFORE_DROP = 5000 |
| 22 | + FLUSH_WAIT_TIME = 5.0 |
| 23 | + |
| 24 | + TYPE = "span" |
| 25 | + CONTENT_TYPE = "application/vnd.sentry.items.span.v2+json" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + capture_func: "Callable[[Envelope], None]", |
| 30 | + record_lost_func: "Callable[..., None]", |
| 31 | + ) -> None: |
| 32 | + # Spans from different traces cannot be emitted in the same envelope |
| 33 | + # since the envelope contains a shared trace header. That's why we bucket |
| 34 | + # by trace_id, so that we can then send the buckets each in its own |
| 35 | + # envelope. |
| 36 | + # trace_id -> span buffer |
| 37 | + self._span_buffer: dict[str, list["StreamedSpan"]] = defaultdict(list) |
| 38 | + self._capture_func = capture_func |
| 39 | + self._record_lost_func = record_lost_func |
| 40 | + self._running = True |
| 41 | + self._lock = threading.Lock() |
| 42 | + |
| 43 | + self._flush_event: "threading.Event" = threading.Event() |
| 44 | + |
| 45 | + self._flusher: "Optional[threading.Thread]" = None |
| 46 | + self._flusher_pid: "Optional[int]" = None |
| 47 | + |
| 48 | + def get_size(self) -> int: |
| 49 | + # caller is responsible for locking before checking this |
| 50 | + return sum(len(buffer) for buffer in self._span_buffer.values()) |
| 51 | + |
| 52 | + def add(self, span: "StreamedSpan") -> None: |
| 53 | + if not self._ensure_thread() or self._flusher is None: |
| 54 | + return None |
| 55 | + |
| 56 | + with self._lock: |
| 57 | + size = self.get_size() |
| 58 | + if size >= self.MAX_BEFORE_DROP: |
| 59 | + self._record_lost_func( |
| 60 | + reason="queue_overflow", |
| 61 | + data_category="span", |
| 62 | + quantity=1, |
| 63 | + ) |
| 64 | + return None |
| 65 | + |
| 66 | + self._span_buffer[span.trace_id].append(span) |
| 67 | + if size + 1 >= self.MAX_BEFORE_FLUSH: |
| 68 | + self._flush_event.set() |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def _to_transport_format(item: "StreamedSpan") -> "Any": |
| 72 | + # TODO[span-first] |
| 73 | + res: "dict[str, Any]" = {} |
| 74 | + return res |
| 75 | + |
| 76 | + def _flush(self) -> None: |
| 77 | + with self._lock: |
| 78 | + if len(self._span_buffer) == 0: |
| 79 | + return None |
| 80 | + |
| 81 | + envelopes = [] |
| 82 | + for trace_id, spans in self._span_buffer.items(): |
| 83 | + if spans: |
| 84 | + # TODO[span-first] |
| 85 | + # dsc = spans[0].dynamic_sampling_context() |
| 86 | + dsc = None |
| 87 | + |
| 88 | + envelope = Envelope( |
| 89 | + headers={ |
| 90 | + "sent_at": format_timestamp(datetime.now(timezone.utc)), |
| 91 | + "trace": dsc, |
| 92 | + } |
| 93 | + ) |
| 94 | + |
| 95 | + envelope.add_item( |
| 96 | + Item( |
| 97 | + type="span", |
| 98 | + content_type="application/vnd.sentry.items.span.v2+json", |
| 99 | + headers={ |
| 100 | + "item_count": len(spans), |
| 101 | + }, |
| 102 | + payload=PayloadRef( |
| 103 | + json={ |
| 104 | + "items": [ |
| 105 | + self._to_transport_format(span) |
| 106 | + for span in spans |
| 107 | + ] |
| 108 | + } |
| 109 | + ), |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + envelopes.append(envelope) |
| 114 | + |
| 115 | + self._span_buffer.clear() |
| 116 | + |
| 117 | + for envelope in envelopes: |
| 118 | + self._capture_func(envelope) |
0 commit comments