Skip to content

Commit 3f8d960

Browse files
committed
Extract setting root span context
1 parent aa1576b commit 3f8d960

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
4141
start_timestamp: root_span.start_time,
4242
timestamp: root_span.end_time,
4343
contexts: %{
44-
trace: %{
45-
trace_id: root_span.trace_id,
46-
span_id: root_span.span_id,
47-
op: root_span.name
48-
}
44+
trace: build_trace_context(root_span)
4945
},
5046
spans: Enum.map([root_span | child_spans], &build_span(&1))
5147
})
@@ -63,13 +59,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
6359
source: "db"
6460
},
6561
contexts: %{
66-
trace: %{
67-
trace_id: root_span.trace_id,
68-
span_id: root_span.span_id,
69-
parent_span_id: root_span.parent_span_id,
70-
op: "db",
71-
origin: root_span.origin
72-
}
62+
trace: build_trace_context(root_span)
7363
},
7464
data: %{
7565
"db.system" => attributes[:"db.system"],
@@ -92,18 +82,15 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
9282
%SpanRecord{attributes: attributes, origin: "opentelemetry_phoenix"} = root_span,
9383
child_spans
9484
) do
95-
name = "#{attributes[:"phoenix.plug"]}##{attributes[:"phoenix.action"]}"
96-
trace = build_trace_context(root_span)
97-
9885
Transaction.new(%{
99-
transaction: name,
86+
transaction: "#{attributes[:"phoenix.plug"]}##{attributes[:"phoenix.action"]}",
10087
start_timestamp: root_span.start_time,
10188
timestamp: root_span.end_time,
10289
transaction_info: %{
10390
source: "view"
10491
},
10592
contexts: %{
106-
trace: trace
93+
trace: build_trace_context(root_span)
10794
},
10895
request: %{
10996
url: attributes[:"http.target"],
@@ -142,11 +129,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
142129
source: "url"
143130
},
144131
contexts: %{
145-
trace: %{
146-
trace_id: root_span.trace_id,
147-
span_id: root_span.span_id,
148-
parent_span_id: root_span.parent_span_id
149-
}
132+
trace: build_trace_context(root_span)
150133
},
151134
request: %{
152135
url: attributes[:"http.url"],
@@ -164,19 +147,43 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
164147
}
165148
end
166149

167-
defp build_trace_context(%SpanRecord{origin: origin, attributes: attributes} = root_span) do
150+
defp build_trace_context(
151+
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = root_span
152+
) do
168153
%{
169154
trace_id: root_span.trace_id,
170155
span_id: root_span.span_id,
171156
parent_span_id: nil,
172157
op: "http.server",
173-
origin: origin,
158+
origin: root_span.origin,
174159
data: %{
175160
"http.response.status_code" => attributes[:"http.status_code"]
176161
}
177162
}
178163
end
179164

165+
defp build_trace_context(%SpanRecord{origin: "opentelemetry_ecto"} = root_span) do
166+
%{
167+
trace_id: root_span.trace_id,
168+
span_id: root_span.span_id,
169+
parent_span_id: root_span.parent_span_id,
170+
op: "db",
171+
origin: root_span.origin,
172+
data: root_span.attributes
173+
}
174+
end
175+
176+
defp build_trace_context(%SpanRecord{attributes: attributes} = root_span) do
177+
%{
178+
trace_id: root_span.trace_id,
179+
span_id: root_span.span_id,
180+
parent_span_id: nil,
181+
op: root_span.name,
182+
origin: root_span.origin,
183+
data: attributes
184+
}
185+
end
186+
180187
defp build_span(
181188
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = span_record
182189
) do

0 commit comments

Comments
 (0)