@@ -28,11 +28,24 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
2828 attrs =
2929 otel_attrs
3030 |> Keyword . delete ( :attributes )
31- |> Keyword . merge ( origin: origin , attributes: attributes )
31+ |> Keyword . merge (
32+ trace_id: cast_trace_id ( otel_attrs [ :trace_id ] ) ,
33+ origin: origin ,
34+ attributes: attributes
35+ )
3236 |> Map . new ( )
3337
3438 struct ( __MODULE__ , attrs )
3539 end
40+
41+ defp cast_trace_id ( trace_id ) , do: bytes_to_hex ( trace_id , 32 )
42+
43+ defp bytes_to_hex ( bytes , length ) do
44+ case ( :otel_utils . format_binary_string ( "~#{ length } .16.0b" , [ bytes ] ) ) do
45+ { :ok , result } -> result
46+ { :error , _ } -> raise "Failed to convert bytes to hex: #{ inspect ( bytes ) } "
47+ end
48+ end
3649 end
3750
3851 @ impl true
@@ -67,29 +80,26 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
6780 end
6881
6982 defp transaction_from_root_span ( root_span , child_spans ) do
70- trace_id = cast_trace_id ( root_span . trace_id )
71-
72- build_transaction ( trace_id , root_span , child_spans )
83+ build_transaction ( root_span , child_spans )
7384 end
7485
75- defp build_transaction ( trace_id , % SpanRecord { origin: :undefined } = root_span , child_spans ) do
86+ defp build_transaction ( % SpanRecord { origin: :undefined } = root_span , child_spans ) do
7687 Transaction . new ( % {
7788 transaction: root_span . name ,
7889 start_timestamp: cast_timestamp ( root_span . start_time ) ,
7990 timestamp: cast_timestamp ( root_span . end_time ) ,
8091 contexts: % {
8192 trace: % {
82- trace_id: trace_id ,
93+ trace_id: root_span . trace_id ,
8394 span_id: cast_span_id ( root_span . span_id ) ,
8495 op: root_span . name
8596 }
8697 } ,
87- spans: Enum . map ( [ root_span | child_spans ] , & build_span ( & 1 , trace_id ) )
98+ spans: Enum . map ( [ root_span | child_spans ] , & build_span ( & 1 , root_span . trace_id ) )
8899 } )
89100 end
90101
91102 defp build_transaction (
92- trace_id ,
93103 % SpanRecord { attributes: attributes , origin: "opentelemetry_ecto" } = root_span ,
94104 child_spans
95105 ) do
@@ -102,7 +112,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
102112 } ,
103113 contexts: % {
104114 trace: % {
105- trace_id: trace_id ,
115+ trace_id: root_span . trace_id ,
106116 span_id: cast_span_id ( root_span . span_id ) ,
107117 parent_span_id: cast_span_id ( root_span . parent_span_id ) ,
108118 op: "db" ,
@@ -127,17 +137,16 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
127137 "query_time_microseconds" => attributes [ :query_time_microseconds ]
128138 } ,
129139 measurements: % { } ,
130- spans: Enum . map ( child_spans , & build_span ( & 1 , trace_id ) )
140+ spans: Enum . map ( child_spans , & build_span ( & 1 , root_span . trace_id ) )
131141 } )
132142 end
133143
134144 defp build_transaction (
135- trace_id ,
136145 % SpanRecord { attributes: attributes , origin: "opentelemetry_phoenix" } = root_span ,
137146 child_spans
138147 ) do
139148 name = "#{ attributes [ :"phoenix.plug" ] } ##{ attributes [ :"phoenix.action" ] } "
140- trace = build_trace_context ( trace_id , root_span )
149+ trace = build_trace_context ( root_span )
141150
142151 Transaction . new ( % {
143152 transaction: name ,
@@ -175,12 +184,11 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
175184 }
176185 } ,
177186 measurements: % { } ,
178- spans: Enum . map ( child_spans , & build_span ( & 1 , trace_id ) )
187+ spans: Enum . map ( child_spans , & build_span ( & 1 , root_span . trace_id ) )
179188 } )
180189 end
181190
182191 defp build_transaction (
183- trace_id ,
184192 % SpanRecord { attributes: attributes , origin: "opentelemetry_bandit" } = root_span ,
185193 child_spans
186194 ) do
@@ -194,7 +202,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
194202 } ,
195203 contexts: % {
196204 trace: % {
197- trace_id: trace_id ,
205+ trace_id: root_span . trace_id ,
198206 span_id: cast_span_id ( root_span . span_id ) ,
199207 parent_span_id: cast_span_id ( root_span . parent_span_id )
200208 }
@@ -216,16 +224,13 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
216224 }
217225 } ,
218226 measurements: % { } ,
219- spans: Enum . map ( child_spans , & build_span ( & 1 , trace_id ) )
227+ spans: Enum . map ( child_spans , & build_span ( & 1 , root_span . trace_id ) )
220228 }
221229 end
222230
223- defp build_trace_context (
224- trace_id ,
225- % SpanRecord { origin: origin , attributes: attributes } = root_span
226- ) do
231+ defp build_trace_context ( % SpanRecord { origin: origin , attributes: attributes } = root_span ) do
227232 % {
228- trace_id: trace_id ,
233+ trace_id: root_span . trace_id ,
229234 span_id: cast_span_id ( root_span . span_id ) ,
230235 parent_span_id: nil ,
231236 op: "http.server" ,
@@ -315,8 +320,6 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
315320 defp cast_span_id ( :undefined ) , do: nil
316321 defp cast_span_id ( span_id ) , do: bytes_to_hex ( span_id , 16 )
317322
318- defp cast_trace_id ( trace_id ) , do: bytes_to_hex ( trace_id , 32 )
319-
320323 defp cast_timestamp ( :undefined ) , do: nil
321324 defp cast_timestamp ( nil ) , do: nil
322325
0 commit comments