From 870a16db6c6625646c93da6de8329a93b03cd7fa Mon Sep 17 00:00:00 2001 From: Jesse Michael Date: Wed, 16 Oct 2024 07:18:37 -0700 Subject: [PATCH] fix: use semconv keys for source attributes --- bridges/otelslog/go.mod | 2 +- bridges/otelslog/handler.go | 9 +++++---- bridges/otelslog/handler_test.go | 7 +++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bridges/otelslog/go.mod b/bridges/otelslog/go.mod index 3166cc845b3..5041613bf90 100644 --- a/bridges/otelslog/go.mod +++ b/bridges/otelslog/go.mod @@ -4,6 +4,7 @@ go 1.22 require ( github.com/stretchr/testify v1.9.0 + go.opentelemetry.io/otel v1.31.0 go.opentelemetry.io/otel/log v0.7.0 ) @@ -12,7 +13,6 @@ require ( github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - go.opentelemetry.io/otel v1.31.0 // indirect go.opentelemetry.io/otel/metric v1.31.0 // indirect go.opentelemetry.io/otel/trace v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/bridges/otelslog/handler.go b/bridges/otelslog/handler.go index b2d8cf791c1..6d40533a0b5 100644 --- a/bridges/otelslog/handler.go +++ b/bridges/otelslog/handler.go @@ -53,6 +53,7 @@ import ( "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/global" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" ) // NewLogger returns a new [slog.Logger] backed by a new [Handler]. See @@ -191,10 +192,10 @@ func (h *Handler) convertRecord(r slog.Record) log.Record { if h.source { fs := runtime.CallersFrames([]uintptr{r.PC}) f, _ := fs.Next() - record.AddAttributes(log.Map("source", - log.String("function", f.Function), - log.String("file", f.File), - log.Int("line", f.Line)), + record.AddAttributes( + log.String(string(semconv.CodeFilepathKey), f.File), + log.String(string(semconv.CodeFunctionKey), f.Function), + log.Int(string(semconv.CodeLineNumberKey), f.Line), ) } diff --git a/bridges/otelslog/handler_test.go b/bridges/otelslog/handler_test.go index da64d8b10f3..9ca30603cdb 100644 --- a/bridges/otelslog/handler_test.go +++ b/bridges/otelslog/handler_test.go @@ -23,6 +23,7 @@ import ( "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/embedded" "go.opentelemetry.io/otel/log/global" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" ) var now = time.Now() @@ -402,7 +403,7 @@ func TestSLogHandler(t *testing.T) { }, { name: "WithSource", - explanation: withSource("a Handler using the WithSource Option should include a source attribute containing the source location of where the file was emitted"), + explanation: withSource("a Handler using the WithSource Option should include file attributes from where the log was emitted"), f: func(l *slog.Logger) { l.Info("msg") }, @@ -411,7 +412,9 @@ func TestSLogHandler(t *testing.T) { r.PC = pc }, checks: [][]check{{ - hasAttr("source", map[string]any{"function": funcName, "file": file, "line": int64(line)}), + hasAttr(string(semconv.CodeFilepathKey), file), + hasAttr(string(semconv.CodeFunctionKey), funcName), + hasAttr(string(semconv.CodeLineNumberKey), int64(line)), }}, options: []Option{WithSource(true)}, },