Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 660
[do not merge] feat: Span streaming & new span API#5317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
757ee12705790ab3f4f98758cdf397cf2910d2097bb01caabe946df6c47a0d0131f61c40878e33f985c4d9874dba173352fcb8ae5bc2fd7973c6b738519669d40367d89d60840e8ab894b5c205546c2f09b2b5920e198f2dca087068cf5d2dedfaf03ca46dc6673978e72d3cd016c341802c7e97c352613f2747cbe9094b33deedd6374a2a481e7b67ae020ae90c182a44bbf41d57bb6c1271c1ddbd34fd1a0261e4d2ab8e95434625e202d78708bc60c135cd7ae85fab49b1cc0bf5152383afaa53a1c23737b3c0d70d0481c3b1fb1e0d3f7d0705cb91a1cf371b1a51767b1c01f664f880e2c154830ec590ed1cb7e508e2a1dafbf916f33192c75aaef6512e0cf42573068872ea18ef0518f56abedeaaee2dbcd41a74156c7cf700f5b0594c8f8e8b9f148c82cce87565d3f435a808bbcae7b931cd6f956486967947889726dc6c1f8c395f284b4767c3cb104667cead8b75bfec12ca684c10f43eb1cb7e21bebc029806f1393d97e33320e71bf033da73afb0e7aaa1477a62cb1aab1ce50949c89aba615ff7c910eb0ef9f08d6247731a6e5d62d04903ea375ea3f0a07d938bc8d0384dcd106c74102013facd03ef7a39f938477b92f3d0cb3b288ea16af1d70365e021f3f7ec7e2d772d96371c8da4f8d0b013bc8bf63bf93c59b66c3d303dc2ca136a8f93b885ee199534da3000cf5f33d30File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,23 @@ | ||
| import json | ||
| 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 | ||
| from sentry_sdk.utils import format_timestamp, serialize_attribute | ||
| if TYPE_CHECKING: | ||
| from typing import Any, Callable, Optional | ||
| from sentry_sdk.traces import 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 | ||
| MAX_BEFORE_DROP = 1000 | ||
| MAX_BYTES_BEFORE_FLUSH = 5 * 1024 * 1024 # 5 MB | ||
| FLUSH_WAIT_TIME = 5.0 | ||
| TYPE = "span" | ||
| @@ -35,6 +34,7 @@ | ||
| # envelope. | ||
| # trace_id -> span buffer | ||
| self._span_buffer: dict[str, list["StreamedSpan"]] = defaultdict(list) | ||
| self._running_size: dict[str, int] = defaultdict(lambda: 0) | ||
| self._capture_func = capture_func | ||
| self._record_lost_func = record_lost_func | ||
| self._running = True | ||
| @@ -64,18 +64,39 @@ | ||
| return None | ||
| self._span_buffer[span.trace_id].append(span) | ||
| if size + 1 >= self.MAX_BEFORE_FLUSH: | ||
| self._flush_event.set() | ||
| return | ||
| self._running_size[span.trace_id] += self._estimate_size(span) | ||
| if self._running_size[span.trace_id] >= self.MAX_BYTES_BEFORE_FLUSH: | ||
| self._flush_event.set() | ||
| return | ||
| @staticmethod | ||
| def _estimate_size(item: "StreamedSpan") -> int: | ||
| # This is just a quick approximation | ||
| span_dict = SpanBatcher._to_transport_format(item) | ||
| return len(str(span_dict)) | ||
Check warning on line 81 in sentry_sdk/_span_batcher.py
| ||
sentrivana marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @staticmethod | ||
| def _to_transport_format(item: "StreamedSpan") -> "Any": | ||
| # TODO[span-first] | ||
| res: "dict[str, Any]" = { | ||
| "trace_id": item.trace_id, | ||
| "span_id": item.span_id, | ||
| "name": item._name, | ||
| "status": item._status, | ||
| "is_segment": item.is_segment(), | ||
| "start_timestamp": item.start_timestamp.timestamp(), | ||
| } | ||
| if item.timestamp: | ||
| 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() | ||
| @@ -89,11 +110,9 @@ | ||
| return None | ||
| envelopes = [] | ||
| for trace_id, spans in self._span_buffer.items(): | ||
| for spans in self._span_buffer.values(): | ||
| if spans: | ||
| # TODO[span-first] | ||
| # dsc = spans[0].dynamic_sampling_context() | ||
| dsc = None | ||
| dsc = spans[0].dynamic_sampling_context() | ||
| envelope = Envelope( | ||
| headers={ | ||
| @@ -123,6 +142,7 @@ | ||
| envelopes.append(envelope) | ||
| self._span_buffer.clear() | ||
| self._running_size.clear() | ||
| for envelope in envelopes: | ||
| self._capture_func(envelope) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.