|
1 | 1 | defmodule Sentry.Opentelemetry.SpanProcessor do |
2 | 2 | @behaviour :otel_span_processor |
3 | 3 |
|
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} |
68 | 5 |
|
69 | 6 | @impl true |
70 | 7 | def on_start(_ctx, otel_span, _config) do |
|
0 commit comments