Skip to content

Commit 0359cb4

Browse files
committed
Remove child spans from SpanStorage automatically
1 parent 317703b commit 0359cb4

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ defmodule Sentry.OpenTelemetry.SpanProcessor do
4242
end
4343

4444
SpanStorage.remove_span(span_record.span_id)
45-
SpanStorage.remove_child_spans(span_record.span_id)
4645

4746
result
4847
else

lib/sentry/opentelemetry/span_storage.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ defmodule Sentry.OpenTelemetry.SpanStorage do
6868
end
6969

7070
def remove_span(span_id) do
71-
:ets.delete(@table, {:root_span, span_id})
72-
:ok
71+
case get_root_span(span_id) do
72+
nil -> :ok
73+
_root_span ->
74+
:ets.delete(@table, {:root_span, span_id})
75+
remove_child_spans(span_id)
76+
end
7377
end
7478

7579
def remove_child_spans(parent_span_id) do

test/sentry/opentelemetry/span_storage_test.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ defmodule Sentry.OpenTelemetry.SpanStorageTest do
6868
SpanStorage.remove_span("abc123")
6969
assert nil == SpanStorage.get_root_span("abc123")
7070
end
71+
72+
test "removes root span and all its children" do
73+
root_span = %SpanRecord{
74+
span_id: "root123",
75+
parent_span_id: nil,
76+
trace_id: "trace123",
77+
name: "root_span"
78+
}
79+
80+
child_span1 = %SpanRecord{
81+
span_id: "child1",
82+
parent_span_id: "root123",
83+
trace_id: "trace123",
84+
name: "child_span_1"
85+
}
86+
87+
child_span2 = %SpanRecord{
88+
span_id: "child2",
89+
parent_span_id: "root123",
90+
trace_id: "trace123",
91+
name: "child_span_2"
92+
}
93+
94+
SpanStorage.store_span(root_span)
95+
SpanStorage.store_span(child_span1)
96+
SpanStorage.store_span(child_span2)
97+
98+
# Verify initial state
99+
assert root_span == SpanStorage.get_root_span("root123")
100+
assert length(SpanStorage.get_child_spans("root123")) == 2
101+
102+
# Remove root span should remove everything
103+
SpanStorage.remove_span("root123")
104+
105+
# Verify everything is removed
106+
assert nil == SpanStorage.get_root_span("root123")
107+
assert [] == SpanStorage.get_child_spans("root123")
108+
end
71109
end
72110

73111
describe "child spans" do

0 commit comments

Comments
 (0)