Skip to content

Commit ec0920d

Browse files
committed
Extract SpanRecord into its own file
1 parent eac4342 commit ec0920d

File tree

2 files changed

+63
-64
lines changed

2 files changed

+63
-64
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,7 @@
11
defmodule Sentry.Opentelemetry.SpanProcessor do
22
@behaviour :otel_span_processor
33

4-
alias Sentry.{Span, Transaction, Opentelemetry.SpanStorage}
5-
6-
defmodule SpanRecord do
7-
require Record
8-
9-
@fields Record.extract(:span, from: "deps/opentelemetry/include/otel_span.hrl")
10-
Record.defrecordp(:span, @fields)
11-
12-
defstruct @fields ++ [:origin]
13-
14-
def new(otel_span) do
15-
otel_attrs = span(otel_span)
16-
17-
{:attributes, _, _, _, attributes} = otel_attrs[:attributes]
18-
19-
origin =
20-
case otel_attrs[:instrumentation_scope] do
21-
{:instrumentation_scope, origin, _version, _} ->
22-
origin
23-
24-
_ ->
25-
:undefined
26-
end
27-
28-
attrs =
29-
otel_attrs
30-
|> Keyword.delete(:attributes)
31-
|> Keyword.merge(
32-
trace_id: cast_trace_id(otel_attrs[:trace_id]),
33-
span_id: cast_span_id(otel_attrs[:span_id]),
34-
parent_span_id: cast_span_id(otel_attrs[:parent_span_id]),
35-
origin: origin,
36-
start_time: cast_timestamp(otel_attrs[:start_time]),
37-
end_time: cast_timestamp(otel_attrs[:end_time]),
38-
attributes: attributes
39-
)
40-
|> Map.new()
41-
42-
struct(__MODULE__, attrs)
43-
end
44-
45-
defp cast_span_id(nil), do: nil
46-
defp cast_span_id(:undefined), do: nil
47-
defp cast_span_id(span_id), do: bytes_to_hex(span_id, 16)
48-
49-
defp cast_trace_id(trace_id), do: bytes_to_hex(trace_id, 32)
50-
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-
61-
defp bytes_to_hex(bytes, length) do
62-
case(:otel_utils.format_binary_string("~#{length}.16.0b", [bytes])) do
63-
{:ok, result} -> result
64-
{:error, _} -> raise "Failed to convert bytes to hex: #{inspect(bytes)}"
65-
end
66-
end
67-
end
4+
alias Sentry.{Span, Transaction, Opentelemetry.SpanStorage, Opentelemetry.SpanRecord}
685

696
@impl true
707
def on_start(_ctx, otel_span, _config) do
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
defmodule Sentry.Opentelemetry.SpanRecord do
2+
require Record
3+
4+
@fields Record.extract(:span, from: "deps/opentelemetry/include/otel_span.hrl")
5+
Record.defrecordp(:span, @fields)
6+
7+
defstruct @fields ++ [:origin]
8+
9+
def new(otel_span) do
10+
otel_attrs = span(otel_span)
11+
12+
{:attributes, _, _, _, attributes} = otel_attrs[:attributes]
13+
14+
origin =
15+
case otel_attrs[:instrumentation_scope] do
16+
{:instrumentation_scope, origin, _version, _} ->
17+
origin
18+
19+
_ ->
20+
:undefined
21+
end
22+
23+
attrs =
24+
otel_attrs
25+
|> Keyword.delete(:attributes)
26+
|> Keyword.merge(
27+
trace_id: cast_trace_id(otel_attrs[:trace_id]),
28+
span_id: cast_span_id(otel_attrs[:span_id]),
29+
parent_span_id: cast_span_id(otel_attrs[:parent_span_id]),
30+
origin: origin,
31+
start_time: cast_timestamp(otel_attrs[:start_time]),
32+
end_time: cast_timestamp(otel_attrs[:end_time]),
33+
attributes: attributes
34+
)
35+
|> Map.new()
36+
37+
struct(__MODULE__, attrs)
38+
end
39+
40+
defp cast_span_id(nil), do: nil
41+
defp cast_span_id(:undefined), do: nil
42+
defp cast_span_id(span_id), do: bytes_to_hex(span_id, 16)
43+
44+
defp cast_trace_id(trace_id), do: bytes_to_hex(trace_id, 32)
45+
46+
defp cast_timestamp(:undefined), do: nil
47+
defp cast_timestamp(nil), do: nil
48+
49+
defp cast_timestamp(timestamp) do
50+
nano_timestamp = :opentelemetry.timestamp_to_nano(timestamp)
51+
{:ok, datetime} = DateTime.from_unix(div(nano_timestamp, 1_000_000), :millisecond)
52+
53+
DateTime.to_iso8601(datetime)
54+
end
55+
56+
defp bytes_to_hex(bytes, length) do
57+
case(:otel_utils.format_binary_string("~#{length}.16.0b", [bytes])) do
58+
{:ok, result} -> result
59+
{:error, _} -> raise "Failed to convert bytes to hex: #{inspect(bytes)}"
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)