diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a27860223f..4354cc4115d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Fixed the value for configuring the OTLP exporter to use `grpc` instead of `grpc/protobuf` in `go.opentelemetry.io/contrib/config`. (#6338) - Allow marshaling types in `go.opentelemetry.io/contrib/config`. (#6347) - Removed the redundant handling of panic from the `HTML` function in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#6373) +- The `code.function` attribute emitted by `go.opentelemetry.io/contrib/bridges/otelslog` now stores just the function name instead the package path-qualified function name. The `code.namespace` attribute now stores the package path. (#6415) 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 9ca30603cdb..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 @@ -639,5 +676,18 @@ func BenchmarkHandler(b *testing.B) { }) }) + b.Run("(WithSource).Handle", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler("", WithSource(true)) + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + err = handlers[n].Handle(ctx, record) + } + }) + _, _ = h, err }