From 5af95a43641845fa6a94ab49a62103313aa78840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Tue, 10 Dec 2024 10:14:34 +0100 Subject: [PATCH] splitFuncName --- bridges/otelslog/handler.go | 17 +++++++++++++- bridges/otelslog/handler_test.go | 39 +++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/bridges/otelslog/handler.go b/bridges/otelslog/handler.go index 6d40533a0b5..26e9994715f 100644 --- a/bridges/otelslog/handler.go +++ b/bridges/otelslog/handler.go @@ -50,6 +50,7 @@ import ( "log/slog" "runtime" "slices" + "strings" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/global" @@ -192,9 +193,11 @@ func (h *Handler) convertRecord(r slog.Record) log.Record { if h.source { fs := runtime.CallersFrames([]uintptr{r.PC}) f, _ := fs.Next() + funcName, namespace := splitFuncName(f.Function) record.AddAttributes( log.String(string(semconv.CodeFilepathKey), f.File), - log.String(string(semconv.CodeFunctionKey), f.Function), + log.String(string(semconv.CodeFunctionKey), funcName), + log.String(string(semconv.CodeNamespaceKey), namespace), log.Int(string(semconv.CodeLineNumberKey), f.Line), ) } @@ -476,3 +479,15 @@ func convert(v slog.Value) log.Value { return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", v.Kind(), v.Any())) } } + +// splitFuncName splits package path-qualified function name into +// function name and package full name (namespace). E.g. it splits +// "github.com/my/repo/pkg.foo" into +// "foo" and "github.com/my/repo/pkg". +func splitFuncName(f string) (string, string) { + i := strings.LastIndexByte(f, '.') + if i < 0 { + return "", "" + } + return f[i+1:], f[:i] +} diff --git a/bridges/otelslog/handler_test.go b/bridges/otelslog/handler_test.go index fae4004cc37..41c3679f206 100644 --- a/bridges/otelslog/handler_test.go +++ b/bridges/otelslog/handler_test.go @@ -230,7 +230,7 @@ func (h *wrapper) Handle(ctx context.Context, r slog.Record) error { func TestSLogHandler(t *testing.T) { // Capture the PC of this line pc, file, line, _ := runtime.Caller(0) - funcName := runtime.FuncForPC(pc).Name() + funcName, namespace := splitFuncName(runtime.FuncForPC(pc).Name()) cases := []testCase{ { @@ -414,6 +414,7 @@ func TestSLogHandler(t *testing.T) { checks: [][]check{{ hasAttr(string(semconv.CodeFilepathKey), file), hasAttr(string(semconv.CodeFunctionKey), funcName), + hasAttr(string(semconv.CodeNamespaceKey), namespace), hasAttr(string(semconv.CodeLineNumberKey), int64(line)), }}, options: []Option{WithSource(true)}, @@ -510,6 +511,42 @@ func TestHandlerEnabled(t *testing.T) { assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed") } +func TestSplitFuncName(t *testing.T) { + testCases := []struct { + fullFuncName string + wantFuncName string + wantNamespace string + }{ + { + fullFuncName: "github.com/my/repo/pkg.foo", + wantFuncName: "foo", + wantNamespace: "github.com/my/repo/pkg", + }, + { + fullFuncName: "net/http.Get", + wantFuncName: "Get", + wantNamespace: "net/http", + }, + { + fullFuncName: "invalid", + wantFuncName: "", + wantNamespace: "", + }, + { + fullFuncName: ".", + wantFuncName: "", + wantNamespace: "", + }, + } + for _, tc := range testCases { + t.Run(tc.fullFuncName, func(t *testing.T) { + gotFuncName, gotNamespace := splitFuncName(tc.fullFuncName) + assert.Equal(t, tc.wantFuncName, gotFuncName) + assert.Equal(t, tc.wantNamespace, gotNamespace) + }) + } +} + func BenchmarkHandler(b *testing.B) { var ( h slog.Handler