Skip to content

Commit 30f1b9c

Browse files
committed
test: Tests and fixes
1 parent d79f10b commit 30f1b9c

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/sentry/spans/consumers/process_segments/enrichment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ def compute_breakdowns(segment: Span, spans: list[Span], project: Project) -> No
186186
ty = breakdown_config.get("type")
187187

188188
if ty == "spanOperations":
189-
measurements = _compute_span_ops(spans, breakdown_config)
189+
breakdowns = _compute_span_ops(spans, breakdown_config)
190190
else:
191191
continue
192192

193193
measurements = segment.setdefault("measurements", {})
194-
for key, value in measurements.items():
194+
for key, value in breakdowns.items():
195195
measurements[f"{breakdown_name}.{key}"] = value
196196

197197

@@ -202,7 +202,7 @@ def _compute_span_ops(spans: list[Span], config: Any) -> dict[str, _MeasurementV
202202

203203
intervals_by_op = defaultdict(list)
204204
for span in spans:
205-
op = span["op"]
205+
op = span.get("sentry_tags", {}).get("op", "")
206206
if operation_name := next(filter(lambda m: op.startswith(m), matches), None):
207207
intervals_by_op[operation_name].append(_span_interval(span))
208208

tests/sentry/spans/consumers/process_segments/test_enrichment.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from sentry.spans.consumers.process_segments.enrichment import set_exclusive_time
1+
from sentry.spans.consumers.process_segments.enrichment import (
2+
compute_breakdowns,
3+
set_exclusive_time,
4+
)
5+
from sentry.testutils.pytest.fixtures import django_db_all
26
from tests.sentry.spans.consumers.process import build_mock_span
37

48
# Tests ported from Relay
@@ -303,3 +307,78 @@ def test_only_immediate_child_spans_affect_calculation():
303307
"cccccccccccccccc": 400.0,
304308
"dddddddddddddddd": 400.0,
305309
}
310+
311+
312+
@django_db_all
313+
def test_emit_ops_breakdown(default_project):
314+
segment_span = build_mock_span(
315+
project_id=1,
316+
is_segment=True,
317+
start_timestamp_precise=1577836800.0,
318+
end_timestamp_precise=1577858400.01,
319+
span_id="ffffffffffffffff",
320+
)
321+
322+
spans = [
323+
build_mock_span(
324+
project_id=1,
325+
start_timestamp_precise=1577836800.0, # 2020-01-01 00:00:00
326+
end_timestamp_precise=1577840400.0, # 2020-01-01 01:00:00
327+
span_id="fa90fdead5f74052",
328+
parent_span_id=segment_span["span_id"],
329+
span_op="http",
330+
),
331+
build_mock_span(
332+
project_id=1,
333+
start_timestamp_precise=1577844000.0, # 2020-01-01 02:00:00
334+
end_timestamp_precise=1577847600.0, # 2020-01-01 03:00:00
335+
span_id="bbbbbbbbbbbbbbbb",
336+
parent_span_id=segment_span["span_id"],
337+
span_op="db",
338+
),
339+
build_mock_span(
340+
project_id=1,
341+
start_timestamp_precise=1577845800.0, # 2020-01-01 02:30:00
342+
end_timestamp_precise=1577849400.0, # 2020-01-01 03:30:00
343+
span_id="cccccccccccccccc",
344+
parent_span_id=segment_span["span_id"],
345+
span_op="db.postgres",
346+
),
347+
build_mock_span(
348+
project_id=1,
349+
start_timestamp_precise=1577851200.0, # 2020-01-01 04:00:00
350+
end_timestamp_precise=1577853000.0, # 2020-01-01 04:30:00
351+
span_id="dddddddddddddddd",
352+
parent_span_id=segment_span["span_id"],
353+
span_op="db.mongo",
354+
),
355+
build_mock_span(
356+
project_id=1,
357+
start_timestamp_precise=1577854800.0, # 2020-01-01 05:00:00
358+
end_timestamp_precise=1577858400.01, # 2020-01-01 06:00:00.01
359+
span_id="eeeeeeeeeeeeeeee",
360+
parent_span_id=segment_span["span_id"],
361+
span_op="browser",
362+
),
363+
segment_span,
364+
]
365+
366+
breakdowns_config = {
367+
"span_ops": {"type": "spanOperations", "matches": ["http", "db"]},
368+
"span_ops_2": {"type": "spanOperations", "matches": ["http", "db"]},
369+
}
370+
default_project.update_option("sentry:breakdowns", breakdowns_config)
371+
372+
# Compute breakdowns for the segment span
373+
compute_breakdowns(segment_span, spans, default_project)
374+
measurements = segment_span["measurements"]
375+
376+
assert measurements["span_ops.ops.http"]["value"] == 3600000.0
377+
assert measurements["span_ops.ops.db"]["value"] == 7200000.0
378+
assert measurements["span_ops_2.ops.http"]["value"] == 3600000.0
379+
assert measurements["span_ops_2.ops.db"]["value"] == 7200000.0
380+
381+
# NOTE: Relay used to extract a total.time breakdown, which is no longer
382+
# included in span breakdowns.
383+
# assert measurements["span_ops.total.time"]["value"] == 14400000.01
384+
# assert measurements["span_ops_2.total.time"]["value"] == 14400000.01

0 commit comments

Comments
 (0)