Skip to content

Commit ad782bf

Browse files
committed
Treat any span with remote parent as a transaction
1 parent 8731e9d commit ad782bf

File tree

1 file changed

+16
-49
lines changed

1 file changed

+16
-49
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,35 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
2222

2323
@impl :otel_span_processor
2424
def on_start(_ctx, otel_span, _config) do
25+
span_record = SpanRecord.new(otel_span)
26+
SpanStorage.store_span(span_record)
2527
otel_span
2628
end
2729

2830
@impl :otel_span_processor
2931
def on_end(otel_span, _config) do
3032
span_record = SpanRecord.new(otel_span)
31-
process_span(span_record)
32-
end
33-
34-
defp process_span(span_record) do
35-
SpanStorage.store_span(span_record)
36-
37-
# Check if this is a root span (no parent) or a transaction root
38-
#
39-
# A span should be a transaction root if:
40-
# 1. It has no parent (true root span)
41-
# 2. OR it's a server span with only a REMOTE parent (distributed tracing)
42-
#
43-
# A span should NOT be a transaction root if:
44-
# - It has a LOCAL parent (parent span exists in our SpanStorage)
45-
is_transaction_root =
46-
cond do
47-
# No parent = definitely a root
48-
span_record.parent_span_id == nil ->
49-
true
50-
51-
# Has a parent - check if it's local or remote
52-
true ->
53-
has_local_parent = has_local_parent_span?(span_record.parent_span_id)
54-
55-
if has_local_parent do
56-
# Parent exists locally - this is a child span, not a transaction root
57-
false
58-
else
59-
# Parent is remote (distributed tracing) - treat server spans as transaction roots
60-
is_server_span?(span_record)
61-
end
62-
end
33+
SpanStorage.update_span(span_record)
6334

64-
if is_transaction_root do
35+
if is_transaction_root?(span_record) do
6536
build_and_send_transaction(span_record)
6637
else
6738
true
6839
end
6940
end
7041

42+
# Check if this is a root span (no parent) or a transaction root
43+
#
44+
# A span should be a transaction root if:
45+
#
46+
# 1. It has no parent (true root span)
47+
# 2. OR it's a span with a remote parent span
48+
#
49+
defp is_transaction_root?(span_record) do
50+
span_record.parent_span_id == nil or
51+
not SpanStorage.span_exists?(span_record.parent_span_id)
52+
end
53+
7154
defp build_and_send_transaction(span_record) do
7255
child_span_records = SpanStorage.get_child_spans(span_record.span_id)
7356
transaction = build_transaction(span_record, child_span_records)
@@ -99,22 +82,6 @@ if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do
9982
:ok
10083
end
10184

102-
# Checks if a parent span exists in our local SpanStorage
103-
# This helps distinguish between:
104-
# - Local parents: span exists in storage (same service)
105-
# - Remote parents: span doesn't exist in storage (distributed tracing from another service)
106-
defp has_local_parent_span?(parent_span_id) do
107-
SpanStorage.span_exists?(parent_span_id)
108-
end
109-
110-
# Helper function to detect if a span is a server span that should be
111-
# treated as a transaction root for distributed tracing.
112-
defp is_server_span?(%{kind: :server, attributes: attributes}) do
113-
Map.has_key?(attributes, to_string(HTTPAttributes.http_request_method()))
114-
end
115-
116-
defp is_server_span?(_), do: false
117-
11885
defp build_transaction(root_span_record, child_span_records) do
11986
root_span = build_span(root_span_record)
12087
child_spans = Enum.map(child_span_records, &build_span(&1))

0 commit comments

Comments
 (0)