Skip to content

Commit b2bde73

Browse files
committed
fix(spans): moving conversion to AnnotatedValue closer to serializer
Extend tests to check for correct `_meta` content. Cleanup.
1 parent 497d4a7 commit b2bde73

File tree

5 files changed

+19
-52
lines changed

5 files changed

+19
-52
lines changed

sentry_sdk/_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class SDKInfo(TypedDict):
7878
"check_in_id": str,
7979
"contexts": dict[str, dict[str, object]],
8080
"dist": str,
81+
"dropped_spans": int,
8182
"duration": Optional[float],
8283
"environment": str,
8384
"errors": list[dict[str, Any]], # TODO: We can expand on this type

sentry_sdk/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from sentry_sdk._compat import PY37, check_uwsgi_thread_support
1212
from sentry_sdk.utils import (
13+
AnnotatedValue,
1314
ContextVar,
1415
capture_internal_exceptions,
1516
current_stacktrace,
@@ -483,16 +484,14 @@ def _prepare_event(
483484
):
484485
# type: (...) -> Optional[Event]
485486

487+
spans_delta = 0 # type: int
488+
486489
if event.get("timestamp") is None:
487490
event["timestamp"] = datetime.now(timezone.utc)
488491

489492
if scope is not None:
490493
is_transaction = event.get("type") == "transaction"
491-
spans = event.get("spans", [])
492-
from sentry_sdk.utils import AnnotatedValue
493-
if isinstance(spans, AnnotatedValue):
494-
spans = spans.value
495-
spans_before = len(spans)
494+
spans_before = len(event.get("spans", []))
496495
event_ = scope.apply_to_event(event, hint, self.options)
497496

498497
# one of the event/error processors returned None
@@ -511,11 +510,7 @@ def _prepare_event(
511510
return None
512511

513512
event = event_
514-
515-
spans = event.get("spans", [])
516-
if isinstance(spans, AnnotatedValue):
517-
spans = spans.value
518-
spans_delta = spans_before - len(spans)
513+
spans_delta = spans_before - len(event.get("spans", []))
519514
if is_transaction and spans_delta > 0 and self.transport is not None:
520515
self.transport.record_lost_event(
521516
"event_processor", data_category="span", quantity=spans_delta
@@ -568,6 +563,11 @@ def _prepare_event(
568563
if event_scrubber:
569564
event_scrubber.scrub_event(event)
570565

566+
567+
dropped_spans = event.pop("dropped_spans", 0) + spans_delta # type: int
568+
if dropped_spans > 0 or spans_delta > 0:
569+
event["spans"] = AnnotatedValue(event.get("spans", []), {"len": spans_before + dropped_spans})
570+
571571
# Postprocess the event here so that annotated types do
572572
# generally not surface in before_send
573573
if event is not None:

sentry_sdk/serializer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ def _serialize_node_impl(
382382
serialized_event = _serialize_node(event, **kwargs)
383383
if not is_vars and meta_stack and isinstance(serialized_event, dict):
384384
serialized_event["_meta"] = meta_stack[0]
385-
print(serialized_event["_meta"])
386385

387386
return serialized_event
388387
finally:

sentry_sdk/tracing.py

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
is_valid_sample_rate,
1212
logger,
1313
nanosecond_time,
14-
AnnotatedValue
1514
)
1615

1716
from typing import TYPE_CHECKING
@@ -204,7 +203,7 @@ def __init__(self, maxlen):
204203
# should be changed to match a consistent interpretation of what maxlen
205204
# limits: either transaction+spans or only child spans.
206205
self.maxlen = maxlen - 1
207-
self.spans = [] # type: Union[List[Span], AnnotatedValue]
206+
self.spans = [] # type: List[Span]
208207
self.dropped_spans = 0 # type: int
209208

210209
def add(self, span):
@@ -215,42 +214,6 @@ def add(self, span):
215214
else:
216215
self.spans.append(span)
217216

218-
219-
220-
221-
222-
223-
224-
# __slots__ = ("maxlen", "_spans")
225-
226-
# def __init__(self, maxlen):
227-
# # type: (int) -> None
228-
# # FIXME: this is `maxlen - 1` only to preserve historical behavior
229-
# # enforced by tests.
230-
# # Either this should be changed to `maxlen` or the JS SDK implementation
231-
# # should be changed to match a consistent interpretation of what maxlen
232-
# # limits: either transaction+spans or only child spans.
233-
# self.maxlen = maxlen - 1
234-
# self._spans = [] # type: Union[List[Span], AnnotatedValue]
235-
236-
# def add(self, span):
237-
# # type: (Span) -> None
238-
# if isinstance(self._spans, AnnotatedValue):
239-
# self._spans.metadata["len"] += 1
240-
# else:
241-
# if len(self._spans) > self.maxlen:
242-
# span._span_recorder = None
243-
# self._spans = AnnotatedValue(self._spans, {"len": self.maxlen}) # TODO!
244-
# else:
245-
# self._spans.append(span)
246-
247-
# @property
248-
# def spans(self):
249-
# # type: () -> List[Span]
250-
# if isinstance(self._spans, AnnotatedValue):
251-
# return self._spans.value
252-
# return self._spans
253-
254217

255218
class Span:
256219
"""A span holds timing information of a block of code.
@@ -1011,9 +974,8 @@ def finish(
1011974
if span.timestamp is not None
1012975
]
1013976

1014-
len_diff = len(self._span_recorder.spans) - len(finished_spans)
1015-
if len_diff or self._span_recorder.dropped_spans:
1016-
finished_spans = AnnotatedValue(finished_spans, {"len": len_diff + self._span_recorder.dropped_spans})
977+
len_diff = len(self._span_recorder.spans) - len(finished_spans)
978+
dropped_spans = len_diff + self._span_recorder.dropped_spans
1017979

1018980
# we do this to break the circular reference of transaction -> span
1019981
# recorder -> span -> containing transaction (which is where we started)
@@ -1039,6 +1001,9 @@ def finish(
10391001
"spans": finished_spans,
10401002
} # type: Event
10411003

1004+
if dropped_spans > 0:
1005+
event["dropped_spans"] = dropped_spans
1006+
10421007
if self._profile is not None and self._profile.valid():
10431008
event["profile"] = self._profile
10441009
self._profile = None

tests/tracing/test_misc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_span_trimming(sentry_init, capture_events):
3131
assert span2["op"] == "foo1"
3232
assert span3["op"] == "foo2"
3333

34+
assert event["_meta"]["spans"][""]["len"] == 10
35+
3436

3537
def test_transaction_naming(sentry_init, capture_events):
3638
sentry_init(traces_sample_rate=1.0)

0 commit comments

Comments
 (0)