Skip to content

Commit 09ac669

Browse files
committed
Move SpanStorage to its own file
1 parent d45aca4 commit 09ac669

File tree

3 files changed

+67
-68
lines changed

3 files changed

+67
-68
lines changed

lib/sentry/application.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Sentry.Application do
2626
{Registry, keys: :unique, name: Sentry.Transport.SenderRegistry},
2727
Sentry.Sources,
2828
Sentry.Dedupe,
29-
Sentry.Telemetry.SpanProcessor.SpanStorage,
29+
Sentry.Telemetry.SpanStorage,
3030
{Sentry.Integrations.CheckInIDMappings,
3131
[
3232
max_expected_check_in_time:

lib/sentry/telemetry/span_processor.ex

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,7 @@ defmodule Sentry.Telemetry.SpanProcessor do
66
@fields Record.extract(:span, from: "deps/opentelemetry/include/otel_span.hrl")
77
Record.defrecordp(:span, @fields)
88

9-
alias Sentry.{Span, Transaction}
10-
11-
defmodule SpanStorage do
12-
use GenServer
13-
14-
def start_link(_opts) do
15-
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
16-
end
17-
18-
def init(_) do
19-
{:ok, %{root_spans: %{}, child_spans: %{}}}
20-
end
21-
22-
def store_span(span_data) do
23-
GenServer.call(__MODULE__, {:store_span, span_data})
24-
end
25-
26-
def get_root_span(span_id) do
27-
GenServer.call(__MODULE__, {:get_root_span, span_id})
28-
end
29-
30-
def get_child_spans(parent_span_id) do
31-
GenServer.call(__MODULE__, {:get_child_spans, parent_span_id})
32-
end
33-
34-
def update_span(span_data) do
35-
GenServer.call(__MODULE__, {:update_span, span_data})
36-
end
37-
38-
def handle_call({:store_span, span_data}, _from, state) do
39-
if span_data[:parent_span_id] == :undefined do
40-
new_state = put_in(state, [:root_spans, span_data[:span_id]], span_data)
41-
{:reply, :ok, new_state}
42-
else
43-
new_state =
44-
update_in(state, [:child_spans, span_data[:parent_span_id]], fn spans ->
45-
(spans || []) ++ [span_data]
46-
end)
47-
48-
{:reply, :ok, new_state}
49-
end
50-
end
51-
52-
def handle_call({:get_root_span, span_id}, _from, state) do
53-
{:reply, state.root_spans[span_id], state}
54-
end
55-
56-
def handle_call({:get_child_spans, parent_span_id}, _from, state) do
57-
{:reply, state.child_spans[parent_span_id] || [], state}
58-
end
59-
60-
def handle_call({:update_span, span_data}, _from, state) do
61-
if span_data[:parent_span_id] == :undefined do
62-
new_state = put_in(state, [:root_spans, span_data[:span_id]], span_data)
63-
{:reply, :ok, new_state}
64-
else
65-
new_state =
66-
update_in(state, [:child_spans, span_data[:parent_span_id]], fn spans ->
67-
Enum.map(spans || [], fn span ->
68-
if span[:span_id] == span_data[:span_id], do: span_data, else: span
69-
end)
70-
end)
71-
72-
{:reply, :ok, new_state}
73-
end
74-
end
75-
end
9+
alias Sentry.{Span, Transaction, Telemetry.SpanStorage}
7610

7711
@impl true
7812
def on_start(_ctx, otel_span, _config) do
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
defmodule Sentry.Telemetry.SpanStorage do
2+
use GenServer
3+
4+
def start_link(_opts) do
5+
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
6+
end
7+
8+
def init(_) do
9+
{:ok, %{root_spans: %{}, child_spans: %{}}}
10+
end
11+
12+
def store_span(span_data) do
13+
GenServer.call(__MODULE__, {:store_span, span_data})
14+
end
15+
16+
def get_root_span(span_id) do
17+
GenServer.call(__MODULE__, {:get_root_span, span_id})
18+
end
19+
20+
def get_child_spans(parent_span_id) do
21+
GenServer.call(__MODULE__, {:get_child_spans, parent_span_id})
22+
end
23+
24+
def update_span(span_data) do
25+
GenServer.call(__MODULE__, {:update_span, span_data})
26+
end
27+
28+
def handle_call({:store_span, span_data}, _from, state) do
29+
if span_data[:parent_span_id] == :undefined do
30+
new_state = put_in(state, [:root_spans, span_data[:span_id]], span_data)
31+
{:reply, :ok, new_state}
32+
else
33+
new_state =
34+
update_in(state, [:child_spans, span_data[:parent_span_id]], fn spans ->
35+
(spans || []) ++ [span_data]
36+
end)
37+
38+
{:reply, :ok, new_state}
39+
end
40+
end
41+
42+
def handle_call({:get_root_span, span_id}, _from, state) do
43+
{:reply, state.root_spans[span_id], state}
44+
end
45+
46+
def handle_call({:get_child_spans, parent_span_id}, _from, state) do
47+
{:reply, state.child_spans[parent_span_id] || [], state}
48+
end
49+
50+
def handle_call({:update_span, span_data}, _from, state) do
51+
if span_data[:parent_span_id] == :undefined do
52+
new_state = put_in(state, [:root_spans, span_data[:span_id]], span_data)
53+
{:reply, :ok, new_state}
54+
else
55+
new_state =
56+
update_in(state, [:child_spans, span_data[:parent_span_id]], fn spans ->
57+
Enum.map(spans || [], fn span ->
58+
if span[:span_id] == span_data[:span_id], do: span_data, else: span
59+
end)
60+
end)
61+
62+
{:reply, :ok, new_state}
63+
end
64+
end
65+
end

0 commit comments

Comments
 (0)