Skip to content

Commit 46a4972

Browse files
committed
wip - clean up after narrowing down the scope
1 parent 4d84afc commit 46a4972

File tree

3 files changed

+15
-448
lines changed

3 files changed

+15
-448
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
2222

2323
@impl :otel_span_processor
2424
def on_start(_ctx, otel_span, _config) do
25-
# Track pending children: when a span starts with a parent, register it
26-
# as a pending child. This allows us to wait for all children when
27-
# the parent ends, solving the race condition where parent.on_end
28-
# is called before child.on_end.
29-
parent_span_id = span(otel_span, :parent_span_id)
30-
span_id = span(otel_span, :span_id)
31-
32-
if parent_span_id != nil and parent_span_id != :undefined do
33-
parent_span_id_str = cast_span_id(parent_span_id)
34-
span_id_str = cast_span_id(span_id)
35-
36-
if parent_span_id_str != nil and span_id_str != nil do
37-
SpanStorage.store_pending_child(parent_span_id_str, span_id_str)
38-
end
39-
end
40-
4125
otel_span
4226
end
4327

@@ -50,18 +34,6 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
5034
defp process_span(span_record) do
5135
SpanStorage.store_span(span_record)
5236

53-
# Remove this span from pending children of its parent (if any)
54-
# This must happen after store_span so the span is available when
55-
# the parent builds its transaction
56-
_ =
57-
if span_record.parent_span_id != nil do
58-
SpanStorage.remove_pending_child(span_record.parent_span_id, span_record.span_id)
59-
60-
# Check if our parent was waiting for us (and possibly other siblings)
61-
# If parent has ended and we were the last pending child, build the transaction now
62-
maybe_complete_waiting_parent(span_record.parent_span_id)
63-
end
64-
6537
# Check if this is a root span (no parent) or a transaction root
6638
#
6739
# A span should be a transaction root if:
@@ -90,37 +62,12 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
9062
end
9163

9264
if is_transaction_root do
93-
# Check if we have pending children that haven't ended yet
94-
if SpanStorage.has_pending_children?(span_record.span_id) do
95-
# Store as waiting parent - transaction will be built when last child ends
96-
SpanStorage.store_waiting_parent(span_record)
97-
true
98-
else
99-
# No pending children, build and send transaction immediately
100-
build_and_send_transaction(span_record)
101-
end
65+
build_and_send_transaction(span_record)
10266
else
10367
true
10468
end
10569
end
10670

107-
# Called when a child span ends to check if its parent was waiting for children
108-
defp maybe_complete_waiting_parent(parent_span_id) do
109-
case SpanStorage.get_waiting_parent(parent_span_id) do
110-
nil ->
111-
# Parent hasn't ended yet or wasn't a transaction root - nothing to do
112-
:ok
113-
114-
parent_span_record ->
115-
# Parent was waiting. Check if all children are done now.
116-
unless SpanStorage.has_pending_children?(parent_span_id) do
117-
# All children done! Build and send the transaction.
118-
SpanStorage.remove_waiting_parent(parent_span_id)
119-
build_and_send_transaction(parent_span_record)
120-
end
121-
end
122-
end
123-
12471
defp build_and_send_transaction(span_record) do
12572
child_span_records = SpanStorage.get_child_spans(span_record.span_id)
12673
transaction = build_transaction(span_record, child_span_records)
@@ -144,15 +91,6 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
14491
# Clean up: remove the transaction root span and all its children
14592
:ok = SpanStorage.remove_root_span(span_record.span_id)
14693

147-
# Also clean up any remaining pending children records (shouldn't be any, but be safe)
148-
:ok = SpanStorage.remove_pending_children(span_record.span_id)
149-
150-
if span_record.parent_span_id != nil do
151-
# This span was stored as a child because it has a remote parent (distributed tracing).
152-
# We need to explicitly remove it from the child spans storage.
153-
:ok = SpanStorage.remove_child_span(span_record.parent_span_id, span_record.span_id)
154-
end
155-
15694
result
15795
end
15896

@@ -341,16 +279,5 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
341279
end)
342280
|> Map.new()
343281
end
344-
345-
# Helper to convert span ID from integer to hex string (used in on_start)
346-
defp cast_span_id(nil), do: nil
347-
defp cast_span_id(:undefined), do: nil
348-
349-
defp cast_span_id(span_id) do
350-
case :otel_utils.format_binary_string("~16.16.0b", [span_id]) do
351-
{:ok, result} -> result
352-
{:error, _} -> nil
353-
end
354-
end
355282
end
356283
end

lib/sentry/opentelemetry/span_storage.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
3737
{:noreply, state}
3838
end
3939

40+
@spec span_exists?(String.t(), keyword()) :: boolean()
41+
def span_exists?(span_id, opts \\ []) do
42+
table_name = Keyword.get(opts, :table_name, default_table_name())
43+
44+
case :ets.lookup(table_name, {:root_span, span_id}) do
45+
[{{:root_span, ^span_id}, _span, _stored_at}] -> true
46+
[] ->
47+
case :ets.match_object(table_name, {{:child_span, :_, span_id}, :_, :_}) do
48+
[] -> false
49+
_ -> true
50+
end
51+
end
52+
end
53+
4054
@spec store_span(SpanRecord.t(), keyword()) :: true
4155
def store_span(span_data, opts \\ []) do
4256
table_name = Keyword.get(opts, :table_name, default_table_name())

0 commit comments

Comments
 (0)