From a4ab07477a533ab4fd3fb498668d3aae9eb5f6db Mon Sep 17 00:00:00 2001 From: Jake Morrison Date: Fri, 22 Dec 2023 21:11:23 -0600 Subject: [PATCH] Add logger filter --- src/opentelemetry_xray_logger_filter.erl | 33 +++++++++++++++++++ ...opentelemetry_xray_logger_filter_SUITE.erl | 28 ++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/opentelemetry_xray_logger_filter.erl create mode 100644 test/opentelemetry_xray_logger_filter_SUITE.erl diff --git a/src/opentelemetry_xray_logger_filter.erl b/src/opentelemetry_xray_logger_filter.erl new file mode 100644 index 0000000..d4897b2 --- /dev/null +++ b/src/opentelemetry_xray_logger_filter.erl @@ -0,0 +1,33 @@ +%% @doc +%% +%% Erlang logger filter to format trace id in X-Ray format. +%% +%% This module implements a logger filter which formats the trace_id in X-Ray +%% format so log messages can be correlated with traces. +%% +%% See: +%% * https://www.erlang.org/doc/man/logger#type-filter +%% * https://github.com/aws/aws-xray-sdk-python/issues/188 +%% @end +-module(opentelemetry_xray_logger_filter). + +-include_lib("kernel/include/logger.hrl"). + +-export([trace_id/2]). + +-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) 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); +trace_id(#{meta := #{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), + 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 new file mode 100644 index 0000000..53dd246 --- /dev/null +++ b/test/opentelemetry_xray_logger_filter_SUITE.erl @@ -0,0 +1,28 @@ +-module(opentelemetry_xray_logger_filter_SUITE). + +-compile(export_all). + +-include_lib("common_test/include/ct.hrl"). +-include_lib("kernel/include/logger.hrl"). +-include_lib("stdlib/include/assert.hrl"). + +all() -> [encode]. + +encode() -> [{docs, "encode"}]. + +encode(_) -> + TraceId = binary_to_integer(<<"5759e988bd862e3fe1be46a994272793">>, 16), + SpanId = binary_to_integer(<<"53995c3f42cd8ad8">>, 16), + ?assertMatch( + #{meta := #{trace_id := <<"1-5759e988-bd862e3fe1be46a994272793">>}}, + opentelemetry_xray_logger_filter:trace_id(#{meta => #{trace_id => TraceId}}, undefined) + ), + ?assertMatch( + #{meta := #{trace_id := <<"1-5759e988-bd862e3fe1be46a994272793@53995c3f42cd8ad8">>}}, + opentelemetry_xray_logger_filter:trace_id(#{meta => #{trace_id => TraceId, span_id => SpanId}}, undefined) + ), + ?assertMatch( + #{meta := #{}}, + opentelemetry_xray_logger_filter:trace_id(#{meta => #{}}, undefined) + ), + ok.