Skip to content

Commit 3c98d4f

Browse files
committed
add genserver back
1 parent e32e392 commit 3c98d4f

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

lib/sentry/application.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ defmodule Sentry.Application do
1010
config = Config.validate!()
1111
:ok = Config.persist(config)
1212

13-
# Setup ETS tables for span storage
14-
Sentry.Opentelemetry.SpanStorage.setup()
15-
1613
http_client = Keyword.fetch!(config, :client)
1714

1815
maybe_http_client_spec =
@@ -30,6 +27,7 @@ defmodule Sentry.Application do
3027
Sentry.Sources,
3128
Sentry.Dedupe,
3229
Sentry.ClientReport.Sender,
30+
Sentry.Opentelemetry.SpanStorage,
3331
{Sentry.Integrations.CheckInIDMappings,
3432
[
3533
max_expected_check_in_time:
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Sentry.Opentelemetry.SpanStorage do
22
@moduledoc false
3+
use GenServer
34

45
@table :span_storage
56

@@ -9,7 +10,6 @@ defmodule Sentry.Opentelemetry.SpanStorage do
910
GenServer.start_link(__MODULE__, nil, name: name)
1011
end
1112

12-
# start ets table on initialization
1313
@impl true
1414
def init(nil) do
1515
_table =
@@ -21,7 +21,10 @@ defmodule Sentry.Opentelemetry.SpanStorage do
2121
end
2222

2323
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})
24+
case :ets.lookup(@table, {:root_span, span_data.span_id}) do
25+
[] -> :ets.insert(@table, {{:root_span, span_data.span_id}, span_data})
26+
_ -> :ok
27+
end
2528
end
2629

2730
def store_span(span_data) do
@@ -31,43 +34,37 @@ defmodule Sentry.Opentelemetry.SpanStorage do
3134
def get_root_span(span_id) do
3235
case :ets.lookup(@table, {:root_span, span_id}) do
3336
[{{:root_span, ^span_id}, span}] -> span
34-
# how do we handle if span cannot be found?
3537
[] -> nil
3638
end
3739
end
3840

3941
def get_child_spans(parent_span_id) do
40-
child_spans = :ets.lookup(@table, parent_span_id)
41-
42-
if child_spans == [] do
43-
nil
44-
else
45-
Enum.map(child_spans, fn {parent_span_id, span} -> span end)
46-
end
47-
end
48-
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})
53-
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})
57-
end
42+
:ets.lookup(@table, parent_span_id)
43+
|> Enum.map(fn {_parent_id, span} -> span end)
5844
end
5945

6046
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})
47+
if span_data.parent_span_id == nil do
48+
case :ets.lookup(@table, {:root_span, span_data.parent_span_id}) do
49+
[] ->
50+
:ets.insert(@table, {{:root_span, span_data.parent_span_id}, span_data})
51+
52+
_ ->
53+
:ets.delete(@table, {:root_span, span_data.parent_span_id})
54+
:ets.insert(@table, {{:root_span, span_data.parent_span_id}, span_data})
6955
end
70-
end)
56+
else
57+
existing_spans = :ets.lookup(@table, span_data.parent_span_id)
58+
59+
Enum.each(existing_spans, fn {parent_id, span} ->
60+
if span.span_id == span_data.span_id do
61+
:ets.delete_object(@table, {parent_id, span})
62+
:ets.insert(@table, {span_data.parent_span_id, span_data})
63+
end
64+
end)
65+
end
66+
67+
:ok
7168
end
7269

7370
def remove_span(span_id) do
@@ -79,4 +76,6 @@ defmodule Sentry.Opentelemetry.SpanStorage do
7976
:ets.delete(@table, parent_span_id)
8077
:ok
8178
end
79+
80+
# need to ad in sweep for any leftover spans. Should we initiate a sweep on_end or add a ttl or both?
8281
end

test/sentry/opentelemetry/span_processor_test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
66
alias Sentry.Opentelemetry.SpanStorage
77

88
setup do
9-
# Create tables
10-
SpanStorage.setup()
11-
129
on_exit(fn ->
1310
# Only try to clean up tables if they exist
14-
if :ets.whereis(:sentry_root_spans) != :undefined do
15-
:ets.delete_all_objects(:sentry_root_spans)
16-
end
17-
18-
if :ets.whereis(:sentry_child_spans) != :undefined do
19-
:ets.delete_all_objects(:sentry_child_spans)
11+
if :ets.whereis(:span_storage) != :undefined do
12+
:ets.delete_all_objects(:span_storage)
2013
end
2114
end)
2215

@@ -71,7 +64,6 @@ defmodule Sentry.Opentelemetry.SpanProcessorTest do
7164
TestEndpoint.instrumented_function()
7265

7366
assert [%Sentry.Transaction{} = transaction] = Sentry.Test.pop_sentry_transactions()
74-
7567
assert_valid_iso8601(transaction.timestamp)
7668
assert_valid_iso8601(transaction.start_timestamp)
7769
assert transaction.timestamp > transaction.start_timestamp

0 commit comments

Comments
 (0)