Skip to content

Commit

Permalink
Handle binary otel_trace_id
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Dec 25, 2023
1 parent d73da48 commit 3d20b40
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
34 changes: 28 additions & 6 deletions src/opentelemetry_xray_logger_filter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,41 @@

-spec trace_id(LogEvent, Extra) ->
logger:filter_return() when LogEvent :: logger:log_event(), Extra :: term().
trace_id(#{meta := #{trace_id := TraceId, span_id := SpanId} = Meta} = LogEvent, _Extra)
trace_id(#{meta := #{otel_trace_id := TraceId, otel_span_id := SpanId} = Meta} = LogEvent, _Extra)
when is_integer(TraceId), is_integer(SpanId) ->
EncodedTraceId = opentelemetry_xray_propagator:encode_trace_id(TraceId),
EncodedSpanId = opentelemetry_xray_propagator:encode_span_id(SpanId),
NewId = otel_utils:assert_to_binary(["1-", EncodedTraceId, "@", EncodedSpanId]),
NewMeta0 = maps:update(trace_id, NewId, Meta),
NewMeta1 = maps:remove(span_id, NewMeta0),
maps:update(meta, NewMeta1, LogEvent);
NewMeta = maps:put(xray_trace_id, NewId, Meta),
maps:update(meta, NewMeta, LogEvent);

trace_id(#{meta := #{trace_id := TraceId} = Meta} = LogEvent, _Extra) when is_integer(TraceId) ->
trace_id(#{meta := #{otel_trace_id := TraceId} = Meta} = LogEvent, _Extra) when is_integer(TraceId) ->
EncodedTraceId = opentelemetry_xray_propagator:encode_trace_id(TraceId),
NewId = otel_utils:assert_to_binary(["1-", EncodedTraceId]),
NewMeta = maps:update(trace_id, NewId, Meta),
NewMeta = maps:put(xray_trace_id, NewId, Meta),
maps:update(meta, NewMeta, LogEvent);

trace_id(
#{
meta
:=
#{otel_trace_id := <<Time:8/binary, TraceId/binary>> = HexTraceId, otel_span_id := HexSpanId} =
Meta
} = LogEvent,
_Extra
)
when is_binary(HexTraceId), is_binary(HexSpanId) ->
NewId = otel_utils:assert_to_binary(["1-", Time, "-", TraceId, "@", HexSpanId]),
NewMeta = maps:put(xray_trace_id, NewId, Meta),
maps:update(meta, NewMeta, LogEvent);

trace_id(
#{meta := #{otel_trace_id := <<Time:8/binary, TraceId/binary>> = HexTraceId} = Meta} = LogEvent,
_Extra
)
when is_binary(HexTraceId) ->
NewId = otel_utils:assert_to_binary(["1-", Time, "-", TraceId]),
NewMeta = maps:put(xray_trace_id, NewId, Meta),
maps:update(meta, NewMeta, LogEvent);

trace_id(LogEvent, _Extra) -> LogEvent.
36 changes: 32 additions & 4 deletions test/opentelemetry_xray_logger_filter_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,43 @@ encode() -> [{docs, "encode"}].

encode(_) ->
TraceId = binary_to_integer(<<"5759e988bd862e3fe1be46a994272793">>, 16),
HexTraceId = <<"5759e988bd862e3fe1be46a994272793">>,
SpanId = binary_to_integer(<<"53995c3f42cd8ad8">>, 16),
HexSpanId = <<"53995c3f42cd8ad8">>,
?assertMatch(
#{meta := #{trace_id := <<"1-5759e988-bd862e3fe1be46a994272793">>}},
opentelemetry_xray_logger_filter:trace_id(#{meta => #{trace_id => TraceId}}, undefined)
#{meta := #{xray_trace_id := <<"1-5759e988-bd862e3fe1be46a994272793">>}},
opentelemetry_xray_logger_filter:trace_id(#{meta => #{otel_trace_id => TraceId}}, undefined)
),
?assertMatch(
#{meta := #{trace_id := <<"1-5759e988-bd862e3fe1be46a994272793@53995c3f42cd8ad8">>}},
#{meta := #{xray_trace_id := <<"1-5759e988-bd862e3fe1be46a994272793@53995c3f42cd8ad8">>}},
opentelemetry_xray_logger_filter:trace_id(
#{meta => #{trace_id => TraceId, span_id => SpanId}},
#{meta => #{otel_trace_id => TraceId, otel_span_id => SpanId}},
undefined
)
),
?assertMatch(
#{
meta
:=
#{
xray_trace_id := <<"1-5759e988-bd862e3fe1be46a994272793">>,
otel_trace_id := <<"5759e988bd862e3fe1be46a994272793">>
}
},
opentelemetry_xray_logger_filter:trace_id(#{meta => #{otel_trace_id => HexTraceId}}, undefined)
),
?assertMatch(
#{
meta
:=
#{
xray_trace_id := <<"1-5759e988-bd862e3fe1be46a994272793@53995c3f42cd8ad8">>,
otel_trace_id := <<"5759e988bd862e3fe1be46a994272793">>,
otel_span_id := <<"53995c3f42cd8ad8">>
}
},
opentelemetry_xray_logger_filter:trace_id(
#{meta => #{otel_trace_id => HexTraceId, otel_span_id => HexSpanId}},
undefined
)
),
Expand Down

0 comments on commit 3d20b40

Please sign in to comment.