Skip to content

Commit

Permalink
fix: use semconv keys for source attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse0Michael committed Oct 16, 2024
1 parent d8404fc commit 870a16d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bridges/otelslog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
)
}

Expand Down
7 changes: 5 additions & 2 deletions bridges/otelslog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
},
Expand All @@ -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)},
},
Expand Down

0 comments on commit 870a16d

Please sign in to comment.