From 40f2258d94f9fc1887bb735e3f483033bd43b864 Mon Sep 17 00:00:00 2001 From: Thomas Cyron Date: Tue, 9 Jul 2024 13:56:47 +0200 Subject: [PATCH] Handle slog.Any as string otel attributes Similar to how slog default text/JSON handlers deal with Any values: https://cs.opensource.google/go/go/+/master:src/log/slog/text_handler.go;drc=f09db2bb9331f4b31afb867173b1773e6494af27;l=119 --- slog_otel.go | 2 +- slog_otel_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/slog_otel.go b/slog_otel.go index daff514..faeecbd 100644 --- a/slog_otel.go +++ b/slog_otel.go @@ -212,7 +212,7 @@ func (h OtelHandler) slogAttrToOtelAttr(attr slog.Attr, groupKeys ...string) att case []bool: return attribute.BoolSlice(key, v) default: - return attribute.KeyValue{} + return attribute.String(key, fmt.Sprintf("%+v", v)) } default: return attribute.KeyValue{} diff --git a/slog_otel_test.go b/slog_otel_test.go index d585331..50f24b5 100644 --- a/slog_otel_test.go +++ b/slog_otel_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "log/slog" "strings" "testing" @@ -127,6 +128,9 @@ func TestOtelHandler(t *testing.T) { }, { Key: "group_2.key_2", Value: attribute.StringValue("value_2"), + }, { + Key: "err", + Value: attribute.StringValue("boom"), }} func() { @@ -151,6 +155,7 @@ func TestOtelHandler(t *testing.T) { "float64_slice", float64Slice, "bool_slice", boolSlice, "a_key", "a_value", + "err", errors.New("boom"), group1, group2, )