|
1 | 1 | defmodule Sentry.Opentelemetry.SpanStorage do |
2 | 2 | @moduledoc false |
3 | 3 |
|
4 | | - @root_spans_table :sentry_root_spans |
5 | | - @child_spans_table :sentry_child_spans |
| 4 | + @table :span_storage |
6 | 5 |
|
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 | | - |
12 | | - _ -> |
13 | | - :ok |
14 | | - end |
| 6 | + @spec start_link(keyword()) :: GenServer.on_start() |
| 7 | + def start_link(opts \\ []) do |
| 8 | + name = Keyword.get(opts, :name, __MODULE__) |
| 9 | + GenServer.start_link(__MODULE__, nil, name: name) |
| 10 | + end |
15 | 11 |
|
16 | | - case :ets.whereis(@child_spans_table) do |
17 | | - :undefined -> |
18 | | - :ets.new(@child_spans_table, [:bag, :public, :named_table]) |
| 12 | + # start ets table on initialization |
| 13 | + @impl true |
| 14 | + def init(nil) do |
| 15 | + _table = |
| 16 | + if :ets.whereis(@table) == :undefined do |
| 17 | + :ets.new(@table, [:named_table, :public, :bag]) |
| 18 | + end |
19 | 19 |
|
20 | | - _ -> |
21 | | - :ok |
22 | | - end |
| 20 | + {:ok, :no_state} |
| 21 | + end |
23 | 22 |
|
24 | | - :ok |
| 23 | + def store_span(span_data) when span_data.parent_span_id == nil do |
| 24 | + _ = :ets.insert(@table, {{:root_span, span_data.span_id}, span_data}) |
25 | 25 | end |
26 | 26 |
|
27 | 27 | def store_span(span_data) do |
28 | | - if span_data.parent_span_id == nil do |
29 | | - :ets.insert(@root_spans_table, {span_data.span_id, span_data}) |
30 | | - else |
31 | | - :ets.insert(@child_spans_table, {span_data.parent_span_id, span_data}) |
32 | | - end |
33 | | - |
34 | | - :ok |
| 28 | + _ = :ets.insert(@table, {span_data.parent_span_id, span_data}) |
35 | 29 | end |
36 | 30 |
|
37 | 31 | def get_root_span(span_id) do |
38 | | - case :ets.lookup(@root_spans_table, span_id) do |
39 | | - [{^span_id, span}] -> span |
| 32 | + case :ets.lookup(@table, {:root_span, span_id}) do |
| 33 | + [{{:root_span, ^span_id}, span}] -> span |
| 34 | + # how do we handle if span cannot be found? |
40 | 35 | [] -> nil |
41 | 36 | end |
42 | 37 | end |
43 | 38 |
|
44 | 39 | def get_child_spans(parent_span_id) do |
45 | | - :ets.lookup(@child_spans_table, parent_span_id) |
46 | | - |> Enum.map(fn {_parent_id, span} -> span end) |
47 | | - end |
| 40 | + child_spans = :ets.lookup(@table, parent_span_id) |
48 | 41 |
|
49 | | - def update_span(span_data) do |
50 | | - if span_data.parent_span_id == nil do |
51 | | - :ets.insert(@root_spans_table, {span_data.span_id, span_data}) |
| 42 | + if child_spans == [] do |
| 43 | + nil |
52 | 44 | else |
53 | | - existing_spans = :ets.lookup(@child_spans_table, span_data.parent_span_id) |
54 | | - |
55 | | - :ets.delete(@child_spans_table, span_data.parent_span_id) |
| 45 | + Enum.map(child_spans, fn {parent_span_id, span} -> span end) |
| 46 | + end |
| 47 | + end |
56 | 48 |
|
57 | | - Enum.each(existing_spans, fn {parent_id, span} -> |
58 | | - if span.span_id != span_data.span_id do |
59 | | - :ets.insert(@child_spans_table, {parent_id, span}) |
60 | | - end |
61 | | - end) |
| 49 | + def update_span(span_data) when span_data.parent_span_id == nil do |
| 50 | + case :ets.lookup(@table, {:root_span, span_data.span_id}) do |
| 51 | + [] -> |
| 52 | + :ets.insert(@table, {{:root_span, span_data.span_id}, span_data}) |
62 | 53 |
|
63 | | - :ets.insert(@child_spans_table, {span_data.parent_span_id, span_data}) |
| 54 | + [{{:root_span, span_data.span_id}, span}] -> |
| 55 | + :ets.delete(@table, {:root_span, span_data.span_id}) |
| 56 | + :ets.insert(@table, {{:root_span, span_data.span_id}, span_data}) |
64 | 57 | end |
| 58 | + end |
65 | 59 |
|
66 | | - :ok |
| 60 | + def update_span(span_data) do |
| 61 | + existing_spans = :ets.lookup(@table, span_data.parent_span_id) |
| 62 | + |
| 63 | + :ets.delete(@table, span_data.parent_span_id) |
| 64 | + |
| 65 | + Enum.each(existing_spans, fn {parent_span_id, span} -> |
| 66 | + if span.span_id == span_data.span_id do |
| 67 | + :ets.delete_object(@table, span) |
| 68 | + :ets.insert(@table, {parent_span_id, span_data}) |
| 69 | + end |
| 70 | + end) |
67 | 71 | end |
68 | 72 |
|
69 | 73 | def remove_span(span_id) do |
70 | | - :ets.delete(@root_spans_table, span_id) |
| 74 | + :ets.delete(@table, {:root_span, span_id}) |
71 | 75 | :ok |
72 | 76 | end |
73 | 77 |
|
74 | 78 | def remove_child_spans(parent_span_id) do |
75 | | - :ets.delete(@child_spans_table, parent_span_id) |
| 79 | + :ets.delete(@table, parent_span_id) |
76 | 80 | :ok |
77 | 81 | end |
78 | 82 | end |
0 commit comments