Skip to content

Commit 0abc931

Browse files
authored
otelslog: Split code attributes (#6415)
Follow semantic conventions more strictly. See the following thread: open-telemetry/semantic-conventions#1624 (comment) Follows #6253 There is no noticeable performance overhead: ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/contrib/bridges/otelslog cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ Handler/(WithSource).Handle-16 260.4n ± 21% 234.0n ± 5% -10.14% (p=0.035 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ Handler/(WithSource).Handle-16 248.0 ± 0% 248.0 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ Handler/(WithSource).Handle-16 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal ```
1 parent 8e19210 commit 0abc931

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2828
- Fixed the value for configuring the OTLP exporter to use `grpc` instead of `grpc/protobuf` in `go.opentelemetry.io/contrib/config`. (#6338)
2929
- Allow marshaling types in `go.opentelemetry.io/contrib/config`. (#6347)
3030
- Removed the redundant handling of panic from the `HTML` function in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#6373)
31+
- 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)
3132

3233
<!-- Released section -->
3334
<!-- Don't change this section unless doing release -->

bridges/otelslog/handler.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"log/slog"
5151
"runtime"
5252
"slices"
53+
"strings"
5354

5455
"go.opentelemetry.io/otel/log"
5556
"go.opentelemetry.io/otel/log/global"
@@ -192,9 +193,11 @@ func (h *Handler) convertRecord(r slog.Record) log.Record {
192193
if h.source {
193194
fs := runtime.CallersFrames([]uintptr{r.PC})
194195
f, _ := fs.Next()
196+
funcName, namespace := splitFuncName(f.Function)
195197
record.AddAttributes(
196198
log.String(string(semconv.CodeFilepathKey), f.File),
197-
log.String(string(semconv.CodeFunctionKey), f.Function),
199+
log.String(string(semconv.CodeFunctionKey), funcName),
200+
log.String(string(semconv.CodeNamespaceKey), namespace),
198201
log.Int(string(semconv.CodeLineNumberKey), f.Line),
199202
)
200203
}
@@ -476,3 +479,15 @@ func convert(v slog.Value) log.Value {
476479
return log.StringValue(fmt.Sprintf("unhandled: (%s) %+v", v.Kind(), v.Any()))
477480
}
478481
}
482+
483+
// splitFuncName splits package path-qualified function name into
484+
// function name and package full name (namespace). E.g. it splits
485+
// "github.com/my/repo/pkg.foo" into
486+
// "foo" and "github.com/my/repo/pkg".
487+
func splitFuncName(f string) (string, string) {
488+
i := strings.LastIndexByte(f, '.')
489+
if i < 0 {
490+
return "", ""
491+
}
492+
return f[i+1:], f[:i]
493+
}

bridges/otelslog/handler_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (h *wrapper) Handle(ctx context.Context, r slog.Record) error {
230230
func TestSLogHandler(t *testing.T) {
231231
// Capture the PC of this line
232232
pc, file, line, _ := runtime.Caller(0)
233-
funcName := runtime.FuncForPC(pc).Name()
233+
funcName, namespace := splitFuncName(runtime.FuncForPC(pc).Name())
234234

235235
cases := []testCase{
236236
{
@@ -414,6 +414,7 @@ func TestSLogHandler(t *testing.T) {
414414
checks: [][]check{{
415415
hasAttr(string(semconv.CodeFilepathKey), file),
416416
hasAttr(string(semconv.CodeFunctionKey), funcName),
417+
hasAttr(string(semconv.CodeNamespaceKey), namespace),
417418
hasAttr(string(semconv.CodeLineNumberKey), int64(line)),
418419
}},
419420
options: []Option{WithSource(true)},
@@ -510,6 +511,42 @@ func TestHandlerEnabled(t *testing.T) {
510511
assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed")
511512
}
512513

514+
func TestSplitFuncName(t *testing.T) {
515+
testCases := []struct {
516+
fullFuncName string
517+
wantFuncName string
518+
wantNamespace string
519+
}{
520+
{
521+
fullFuncName: "github.com/my/repo/pkg.foo",
522+
wantFuncName: "foo",
523+
wantNamespace: "github.com/my/repo/pkg",
524+
},
525+
{
526+
fullFuncName: "net/http.Get",
527+
wantFuncName: "Get",
528+
wantNamespace: "net/http",
529+
},
530+
{
531+
fullFuncName: "invalid",
532+
wantFuncName: "",
533+
wantNamespace: "",
534+
},
535+
{
536+
fullFuncName: ".",
537+
wantFuncName: "",
538+
wantNamespace: "",
539+
},
540+
}
541+
for _, tc := range testCases {
542+
t.Run(tc.fullFuncName, func(t *testing.T) {
543+
gotFuncName, gotNamespace := splitFuncName(tc.fullFuncName)
544+
assert.Equal(t, tc.wantFuncName, gotFuncName)
545+
assert.Equal(t, tc.wantNamespace, gotNamespace)
546+
})
547+
}
548+
}
549+
513550
func BenchmarkHandler(b *testing.B) {
514551
var (
515552
h slog.Handler
@@ -639,5 +676,18 @@ func BenchmarkHandler(b *testing.B) {
639676
})
640677
})
641678

679+
b.Run("(WithSource).Handle", func(b *testing.B) {
680+
handlers := make([]*Handler, b.N)
681+
for i := range handlers {
682+
handlers[i] = NewHandler("", WithSource(true))
683+
}
684+
685+
b.ReportAllocs()
686+
b.ResetTimer()
687+
for n := 0; n < b.N; n++ {
688+
err = handlers[n].Handle(ctx, record)
689+
}
690+
})
691+
642692
_, _ = h, err
643693
}

0 commit comments

Comments
 (0)