Skip to content

Commit 854457c

Browse files
committed
Remove span records after sending a transaction
1 parent 8306a89 commit 854457c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
2525
transaction = build_transaction(root_span, child_spans)
2626

2727
Sentry.send_transaction(transaction)
28+
29+
SpanStorage.remove_span(span_record.span_id)
30+
SpanStorage.remove_child_spans(span_record.span_id)
2831
end
2932

3033
:ok

lib/sentry/opentelemetry/span_storage.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ defmodule Sentry.Opentelemetry.SpanStorage do
2525
GenServer.call(__MODULE__, {:update_span, span_data})
2626
end
2727

28+
def remove_span(span_id) do
29+
GenServer.call(__MODULE__, {:remove_span, span_id})
30+
end
31+
32+
def remove_child_spans(parent_span_id) do
33+
GenServer.call(__MODULE__, {:remove_child_spans, parent_span_id})
34+
end
35+
2836
def handle_call({:store_span, span_data}, _from, state) do
2937
if span_data.parent_span_id == nil do
3038
new_state = put_in(state, [:root_spans, span_data.span_id], span_data)
@@ -62,4 +70,18 @@ defmodule Sentry.Opentelemetry.SpanStorage do
6270
{:reply, :ok, new_state}
6371
end
6472
end
73+
74+
def handle_call({:remove_span, span_id}, _from, state) do
75+
new_state = %{
76+
state |
77+
root_spans: Map.delete(state.root_spans, span_id),
78+
child_spans: Map.delete(state.child_spans, span_id)
79+
}
80+
{:reply, :ok, new_state}
81+
end
82+
83+
def handle_call({:remove_child_spans, parent_span_id}, _from, state) do
84+
new_state = %{state | child_spans: Map.delete(state.child_spans, parent_span_id)}
85+
{:reply, :ok, new_state}
86+
end
6587
end

test/sentry/opentelemetry/span_processor_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
8080
assert_valid_trace_id(child_span_two.trace_id)
8181
end
8282

83+
test "removes span records from storage after sending a transaction" do
84+
put_test_config(environment_name: "test")
85+
86+
Sentry.Test.start_collecting_sentry_reports()
87+
88+
TestEndpoint.instrumented_function()
89+
90+
assert [%Sentry.Transaction{} = transaction] = Sentry.Test.pop_sentry_transactions()
91+
92+
assert nil ==
93+
Sentry.Opentelemetry.SpanStorage.get_root_span(transaction.contexts.trace.span_id)
94+
95+
assert [] ==
96+
Sentry.Opentelemetry.SpanStorage.get_child_spans(transaction.contexts.trace.span_id)
97+
end
98+
8399
defp assert_valid_iso8601(timestamp) do
84100
case DateTime.from_iso8601(timestamp) do
85101
{:ok, datetime, _offset} ->

0 commit comments

Comments
 (0)