Skip to content

Commit 4d0764c

Browse files
committed
Handle casting timestamps in the SpanRecord struct
1 parent a603d9c commit 4d0764c

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
3333
span_id: cast_span_id(otel_attrs[:span_id]),
3434
parent_span_id: cast_span_id(otel_attrs[:parent_span_id]),
3535
origin: origin,
36+
start_time: cast_timestamp(otel_attrs[:start_time]),
37+
end_time: cast_timestamp(otel_attrs[:end_time]),
3638
attributes: attributes
3739
)
3840
|> Map.new()
@@ -46,6 +48,16 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
4648

4749
defp cast_trace_id(trace_id), do: bytes_to_hex(trace_id, 32)
4850

51+
defp cast_timestamp(:undefined), do: nil
52+
defp cast_timestamp(nil), do: nil
53+
54+
defp cast_timestamp(timestamp) do
55+
nano_timestamp = :opentelemetry.timestamp_to_nano(timestamp)
56+
{:ok, datetime} = DateTime.from_unix(div(nano_timestamp, 1_000_000), :millisecond)
57+
58+
DateTime.to_iso8601(datetime)
59+
end
60+
4961
defp bytes_to_hex(bytes, length) do
5062
case(:otel_utils.format_binary_string("~#{length}.16.0b", [bytes])) do
5163
{:ok, result} -> result
@@ -92,8 +104,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
92104
defp build_transaction(%SpanRecord{origin: :undefined} = root_span, child_spans) do
93105
Transaction.new(%{
94106
transaction: root_span.name,
95-
start_timestamp: cast_timestamp(root_span.start_time),
96-
timestamp: cast_timestamp(root_span.end_time),
107+
start_timestamp: root_span.start_time,
108+
timestamp: root_span.end_time,
97109
contexts: %{
98110
trace: %{
99111
trace_id: root_span.trace_id,
@@ -111,8 +123,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
111123
) do
112124
Transaction.new(%{
113125
transaction: root_span.name,
114-
start_timestamp: cast_timestamp(root_span.start_time),
115-
timestamp: cast_timestamp(root_span.end_time),
126+
start_timestamp: root_span.start_time,
127+
timestamp: root_span.end_time,
116128
transaction_info: %{
117129
source: "db"
118130
},
@@ -156,8 +168,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
156168

157169
Transaction.new(%{
158170
transaction: name,
159-
start_timestamp: cast_timestamp(root_span.start_time),
160-
timestamp: cast_timestamp(root_span.end_time),
171+
start_timestamp: root_span.start_time,
172+
timestamp: root_span.end_time,
161173
transaction_info: %{
162174
source: "view"
163175
},
@@ -200,8 +212,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
200212
) do
201213
%Sentry.Transaction{
202214
event_id: Sentry.UUID.uuid4_hex(),
203-
start_timestamp: cast_timestamp(root_span.start_time),
204-
timestamp: cast_timestamp(root_span.end_time),
215+
start_timestamp: root_span.start_time,
216+
timestamp: root_span.end_time,
205217
transaction: attributes[:"http.target"],
206218
transaction_info: %{
207219
source: "url"
@@ -254,8 +266,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
254266

255267
%Span{
256268
op: op,
257-
start_timestamp: cast_timestamp(span_record.start_time),
258-
timestamp: cast_timestamp(span_record.end_time),
269+
start_timestamp: span_record.start_time,
270+
timestamp: span_record.end_time,
259271
trace_id: span_record.trace_id,
260272
span_id: span_record.span_id,
261273
parent_span_id: span_record.parent_span_id,
@@ -268,8 +280,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
268280
%Span{
269281
trace_id: span_record.trace_id,
270282
op: span_record.name,
271-
start_timestamp: cast_timestamp(span_record.start_time),
272-
timestamp: cast_timestamp(span_record.end_time),
283+
start_timestamp: span_record.start_time,
284+
timestamp: span_record.end_time,
273285
span_id: span_record.span_id,
274286
parent_span_id: span_record.parent_span_id
275287
}
@@ -279,8 +291,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
279291
%Span{
280292
trace_id: span_record.trace_id,
281293
op: span_record.name,
282-
start_timestamp: cast_timestamp(span_record.start_time),
283-
timestamp: cast_timestamp(span_record.end_time),
294+
start_timestamp: span_record.start_time,
295+
timestamp: span_record.end_time,
284296
span_id: span_record.span_id,
285297
parent_span_id: span_record.parent_span_id,
286298
description: span_record.name,
@@ -292,8 +304,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
292304
%Span{
293305
trace_id: span_record.trace_id,
294306
op: span_record.name,
295-
start_timestamp: cast_timestamp(span_record.start_time),
296-
timestamp: cast_timestamp(span_record.end_time),
307+
start_timestamp: span_record.start_time,
308+
timestamp: span_record.end_time,
297309
span_id: span_record.span_id,
298310
parent_span_id: span_record.parent_span_id,
299311
origin: span_record.origin,
@@ -308,20 +320,10 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
308320
%Span{
309321
trace_id: span_record.trace_id,
310322
op: span_record.name,
311-
start_timestamp: cast_timestamp(span_record.start_time),
312-
timestamp: cast_timestamp(span_record.end_time),
323+
start_timestamp: span_record.start_time,
324+
timestamp: span_record.end_time,
313325
span_id: span_record.span_id,
314326
parent_span_id: span_record.parent_span_id
315327
}
316328
end
317-
318-
defp cast_timestamp(:undefined), do: nil
319-
defp cast_timestamp(nil), do: nil
320-
321-
defp cast_timestamp(timestamp) do
322-
nano_timestamp = :opentelemetry.timestamp_to_nano(timestamp)
323-
{:ok, datetime} = DateTime.from_unix(div(nano_timestamp, 1_000_000), :millisecond)
324-
325-
DateTime.to_iso8601(datetime)
326-
end
327329
end

0 commit comments

Comments
 (0)