|
1 | 1 | defmodule Sentry.Opentelemetry.SpanStorage do |
2 | | - use GenServer |
| 2 | + @moduledoc false |
3 | 3 |
|
4 | | - def start_link(_opts) do |
5 | | - GenServer.start_link(__MODULE__, nil, name: __MODULE__) |
6 | | - end |
| 4 | + @root_spans_table :sentry_root_spans |
| 5 | + @child_spans_table :sentry_child_spans |
7 | 6 |
|
8 | | - def init(_) do |
9 | | - {:ok, %{root_spans: %{}, child_spans: %{}}} |
10 | | - end |
| 7 | + def setup do |
| 8 | + case :ets.whereis(@root_spans_table) do |
| 9 | + :undefined -> |
| 10 | + :ets.new(@root_spans_table, [:set, :public, :named_table]) |
11 | 11 |
|
12 | | - def store_span(span_data) do |
13 | | - GenServer.call(__MODULE__, {:store_span, span_data}) |
14 | | - end |
| 12 | + _ -> |
| 13 | + :ok |
| 14 | + end |
15 | 15 |
|
16 | | - def get_root_span(span_id) do |
17 | | - GenServer.call(__MODULE__, {:get_root_span, span_id}) |
18 | | - end |
| 16 | + case :ets.whereis(@child_spans_table) do |
| 17 | + :undefined -> |
| 18 | + :ets.new(@child_spans_table, [:bag, :public, :named_table]) |
19 | 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 |
| 20 | + _ -> |
| 21 | + :ok |
| 22 | + end |
27 | 23 |
|
28 | | - def remove_span(span_id) do |
29 | | - GenServer.call(__MODULE__, {:remove_span, span_id}) |
| 24 | + :ok |
30 | 25 | end |
31 | 26 |
|
32 | | - def remove_child_spans(parent_span_id) do |
33 | | - GenServer.call(__MODULE__, {:remove_child_spans, parent_span_id}) |
34 | | - end |
| 27 | + def store_span(span_data) do |
| 28 | + ensure_tables_exist() |
35 | 29 |
|
36 | | - def handle_call({:store_span, span_data}, _from, state) do |
37 | 30 | if span_data.parent_span_id == nil do |
38 | | - new_state = put_in(state, [:root_spans, span_data.span_id], span_data) |
39 | | - {:reply, :ok, new_state} |
| 31 | + :ets.insert(@root_spans_table, {span_data.span_id, span_data}) |
40 | 32 | else |
41 | | - new_state = |
42 | | - update_in(state, [:child_spans, span_data.parent_span_id], fn spans -> |
43 | | - (spans || []) ++ [span_data] |
44 | | - end) |
45 | | - |
46 | | - {:reply, :ok, new_state} |
| 33 | + :ets.insert(@child_spans_table, {span_data.parent_span_id, span_data}) |
47 | 34 | end |
| 35 | + |
| 36 | + :ok |
48 | 37 | end |
49 | 38 |
|
50 | | - def handle_call({:get_root_span, span_id}, _from, state) do |
51 | | - {:reply, state.root_spans[span_id], state} |
| 39 | + def get_root_span(span_id) do |
| 40 | + ensure_tables_exist() |
| 41 | + |
| 42 | + case :ets.lookup(@root_spans_table, span_id) do |
| 43 | + [{^span_id, span}] -> span |
| 44 | + [] -> nil |
| 45 | + end |
52 | 46 | end |
53 | 47 |
|
54 | | - def handle_call({:get_child_spans, parent_span_id}, _from, state) do |
55 | | - {:reply, state.child_spans[parent_span_id] || [], state} |
| 48 | + def get_child_spans(parent_span_id) do |
| 49 | + ensure_tables_exist() |
| 50 | + |
| 51 | + :ets.lookup(@child_spans_table, parent_span_id) |
| 52 | + |> Enum.map(fn {_parent_id, span} -> span end) |
56 | 53 | end |
57 | 54 |
|
58 | | - def handle_call({:update_span, span_data}, _from, state) do |
| 55 | + def update_span(span_data) do |
| 56 | + ensure_tables_exist() |
| 57 | + |
59 | 58 | if span_data.parent_span_id == nil do |
60 | | - new_state = put_in(state, [:root_spans, span_data.span_id], span_data) |
61 | | - {:reply, :ok, new_state} |
| 59 | + :ets.insert(@root_spans_table, {span_data.span_id, span_data}) |
62 | 60 | else |
63 | | - new_state = |
64 | | - update_in(state, [:child_spans, span_data.parent_span_id], fn spans -> |
65 | | - Enum.map(spans || [], fn span -> |
66 | | - if span.span_id == span_data.span_id, do: span_data, else: span |
67 | | - end) |
68 | | - end) |
69 | | - |
70 | | - {:reply, :ok, new_state} |
| 61 | + existing_spans = :ets.lookup(@child_spans_table, span_data.parent_span_id) |
| 62 | + |
| 63 | + :ets.delete(@child_spans_table, span_data.parent_span_id) |
| 64 | + |
| 65 | + Enum.each(existing_spans, fn {parent_id, span} -> |
| 66 | + if span.span_id != span_data.span_id do |
| 67 | + :ets.insert(@child_spans_table, {parent_id, span}) |
| 68 | + end |
| 69 | + end) |
| 70 | + |
| 71 | + :ets.insert(@child_spans_table, {span_data.parent_span_id, span_data}) |
71 | 72 | end |
| 73 | + |
| 74 | + :ok |
72 | 75 | end |
73 | 76 |
|
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 | | - } |
| 77 | + def remove_span(span_id) do |
| 78 | + ensure_tables_exist() |
| 79 | + :ets.delete(@root_spans_table, span_id) |
| 80 | + :ok |
| 81 | + end |
80 | 82 |
|
81 | | - {:reply, :ok, new_state} |
| 83 | + def remove_child_spans(parent_span_id) do |
| 84 | + ensure_tables_exist() |
| 85 | + :ets.delete(@child_spans_table, parent_span_id) |
| 86 | + :ok |
82 | 87 | end |
83 | 88 |
|
84 | | - def handle_call({:remove_child_spans, parent_span_id}, _from, state) do |
85 | | - new_state = %{state | child_spans: Map.delete(state.child_spans, parent_span_id)} |
86 | | - {:reply, :ok, new_state} |
| 89 | + defp ensure_tables_exist do |
| 90 | + setup() |
87 | 91 | end |
88 | 92 | end |
0 commit comments