From 3d20b40215b4a1227f12e8973f5911a4c1616155 Mon Sep 17 00:00:00 2001 From: Jake Morrison Date: Mon, 25 Dec 2023 16:48:40 -0600 Subject: [PATCH] Handle binary otel_trace_id --- src/opentelemetry_xray_logger_filter.erl | 34 ++++++++++++++---- ...opentelemetry_xray_logger_filter_SUITE.erl | 36 ++++++++++++++++--- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/opentelemetry_xray_logger_filter.erl b/src/opentelemetry_xray_logger_filter.erl index 4135ccc..729ee74 100644 --- a/src/opentelemetry_xray_logger_filter.erl +++ b/src/opentelemetry_xray_logger_filter.erl @@ -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 := <> = 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 := <> = 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. diff --git a/test/opentelemetry_xray_logger_filter_SUITE.erl b/test/opentelemetry_xray_logger_filter_SUITE.erl index 0a6b17c..fb6434b 100644 --- a/test/opentelemetry_xray_logger_filter_SUITE.erl +++ b/test/opentelemetry_xray_logger_filter_SUITE.erl @@ -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 ) ),