Skip to content

Commit 67c963d

Browse files
authored
feat(summary): Fixed the incorrect emission of span metric summaries (#2566)
1 parent 465f44a commit 67c963d

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

sentry_sdk/metrics.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,22 @@ def add(
375375

376376
def to_json(self):
377377
# type: (...) -> Dict[str, Any]
378-
rv = {}
378+
rv = {} # type: Any
379379
for (export_key, tags), (
380380
v_min,
381381
v_max,
382382
v_count,
383383
v_sum,
384384
) in self._measurements.items():
385-
rv[export_key] = {
386-
"tags": _tags_to_dict(tags),
387-
"min": v_min,
388-
"max": v_max,
389-
"count": v_count,
390-
"sum": v_sum,
391-
}
385+
rv.setdefault(export_key, []).append(
386+
{
387+
"tags": _tags_to_dict(tags),
388+
"min": v_min,
389+
"max": v_max,
390+
"count": v_count,
391+
"sum": v_sum,
392+
}
393+
)
392394
return rv
393395

394396

tests/test_metrics.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -597,33 +597,37 @@ def test_metric_summaries(sentry_init, capture_envelopes):
597597
t = transaction.items[0].get_transaction_event()
598598

599599
assert t["_metrics_summary"] == {
600-
"c:root-counter@none": {
601-
"count": 1,
602-
"min": 1.0,
603-
"max": 1.0,
604-
"sum": 1.0,
600+
"c:root-counter@none": [
601+
{
602+
"count": 1,
603+
"min": 1.0,
604+
"max": 1.0,
605+
"sum": 1.0,
606+
"tags": {
607+
"transaction": "/foo",
608+
"release": "fun-release@1.0.0",
609+
"environment": "not-fun-env",
610+
},
611+
}
612+
]
613+
}
614+
615+
assert t["spans"][0]["_metrics_summary"]["d:my-dist@none"] == [
616+
{
617+
"count": 10,
618+
"min": 0.0,
619+
"max": 9.0,
620+
"sum": 45.0,
605621
"tags": {
606-
"transaction": "/foo",
607-
"release": "fun-release@1.0.0",
608622
"environment": "not-fun-env",
623+
"release": "fun-release@1.0.0",
624+
"transaction": "/foo",
609625
},
610626
}
611-
}
612-
613-
assert t["spans"][0]["_metrics_summary"]["d:my-dist@none"] == {
614-
"count": 10,
615-
"min": 0.0,
616-
"max": 9.0,
617-
"sum": 45.0,
618-
"tags": {
619-
"environment": "not-fun-env",
620-
"release": "fun-release@1.0.0",
621-
"transaction": "/foo",
622-
},
623-
}
627+
]
624628

625629
assert t["spans"][0]["tags"] == {"a": "b"}
626-
timer = t["spans"][0]["_metrics_summary"]["d:my-timer-metric@second"]
630+
(timer,) = t["spans"][0]["_metrics_summary"]["d:my-timer-metric@second"]
627631
assert timer["count"] == 1
628632
assert timer["max"] == timer["min"] == timer["sum"]
629633
assert timer["sum"] > 0
@@ -697,6 +701,7 @@ def should_summarize_metric(key, tags):
697701
op="stuff", name="/foo", source=TRANSACTION_SOURCE_ROUTE
698702
) as transaction:
699703
metrics.timing("foo", value=1.0, tags={"a": "b"}, timestamp=ts)
704+
metrics.timing("foo", value=1.0, tags={"b": "c"}, timestamp=ts)
700705
metrics.timing("bar", value=1.0, tags={"a": "b"}, timestamp=ts)
701706

702707
Hub.current.flush()
@@ -707,25 +712,40 @@ def should_summarize_metric(key, tags):
707712
assert envelope.items[0].headers["type"] == "statsd"
708713
m = parse_metrics(envelope.items[0].payload.get_bytes())
709714

710-
assert len(m) == 2
715+
assert len(m) == 3
711716
assert m[0][1] == "bar@second"
712717
assert m[1][1] == "foo@second"
718+
assert m[2][1] == "foo@second"
713719

714720
# Measurement Attachment
715721
t = transaction.items[0].get_transaction_event()["_metrics_summary"]
716722
assert t == {
717-
"d:foo@second": {
718-
"tags": {
719-
"a": "b",
720-
"environment": "not-fun-env",
721-
"release": "fun-release@1.0.0",
722-
"transaction": "/foo",
723+
"d:foo@second": [
724+
{
725+
"tags": {
726+
"a": "b",
727+
"environment": "not-fun-env",
728+
"release": "fun-release@1.0.0",
729+
"transaction": "/foo",
730+
},
731+
"min": 1.0,
732+
"max": 1.0,
733+
"count": 1,
734+
"sum": 1.0,
723735
},
724-
"min": 1.0,
725-
"max": 1.0,
726-
"count": 1,
727-
"sum": 1.0,
728-
}
736+
{
737+
"tags": {
738+
"b": "c",
739+
"environment": "not-fun-env",
740+
"release": "fun-release@1.0.0",
741+
"transaction": "/foo",
742+
},
743+
"min": 1.0,
744+
"max": 1.0,
745+
"count": 1,
746+
"sum": 1.0,
747+
},
748+
]
729749
}
730750

731751

0 commit comments

Comments
 (0)