From 0abc9317be66e97187110ba82fa174ce9e9bfdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 11 Dec 2024 08:09:58 +0100 Subject: [PATCH 1/3] otelslog: Split code attributes (#6415) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow semantic conventions more strictly. See the following thread: https://github.com/open-telemetry/semantic-conventions/pull/1624#discussion_r1870985463 Follows https://github.com/open-telemetry/opentelemetry-go-contrib/pull/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 ``` --- CHANGELOG.md | 1 + bridges/otelslog/handler.go | 17 ++++++++++- bridges/otelslog/handler_test.go | 52 +++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) 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 } From 6ab504eb9c6d35245c16d5514d32f4ec12e8cc41 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 08:21:34 +0100 Subject: [PATCH 2/3] fix(deps): update kubernetes packages to v0.31.4 (#6422) --- detectors/aws/eks/go.mod | 6 +++--- detectors/aws/eks/go.sum | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/detectors/aws/eks/go.mod b/detectors/aws/eks/go.mod index 9c623c35a79..cd7984ef1cb 100644 --- a/detectors/aws/eks/go.mod +++ b/detectors/aws/eks/go.mod @@ -6,8 +6,8 @@ require ( github.com/stretchr/testify v1.10.0 go.opentelemetry.io/otel v1.32.0 go.opentelemetry.io/otel/sdk v1.32.0 - k8s.io/apimachinery v0.31.3 - k8s.io/client-go v0.31.3 + k8s.io/apimachinery v0.31.4 + k8s.io/client-go v0.31.4 ) require ( @@ -45,7 +45,7 @@ require ( google.golang.org/protobuf v1.35.2 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/api v0.31.3 // indirect + k8s.io/api v0.31.4 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/kube-openapi v0.0.0-20241127205056-99599406b04f // indirect k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect diff --git a/detectors/aws/eks/go.sum b/detectors/aws/eks/go.sum index 6b2d40a1d42..385215c3d2c 100644 --- a/detectors/aws/eks/go.sum +++ b/detectors/aws/eks/go.sum @@ -131,12 +131,12 @@ gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -k8s.io/api v0.31.3 h1:umzm5o8lFbdN/hIXbrK9oRpOproJO62CV1zqxXrLgk8= -k8s.io/api v0.31.3/go.mod h1:UJrkIp9pnMOI9K2nlL6vwpxRzzEX5sWgn8kGQe92kCE= -k8s.io/apimachinery v0.31.3 h1:6l0WhcYgasZ/wk9ktLq5vLaoXJJr5ts6lkaQzgeYPq4= -k8s.io/apimachinery v0.31.3/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= -k8s.io/client-go v0.31.3 h1:CAlZuM+PH2cm+86LOBemaJI/lQ5linJ6UFxKX/SoG+4= -k8s.io/client-go v0.31.3/go.mod h1:2CgjPUTpv3fE5dNygAr2NcM8nhHzXvxB8KL5gYc3kJs= +k8s.io/api v0.31.4 h1:I2QNzitPVsPeLQvexMEsj945QumYraqv9m74isPDKhM= +k8s.io/api v0.31.4/go.mod h1:d+7vgXLvmcdT1BCo79VEgJxHHryww3V5np2OYTr6jdw= +k8s.io/apimachinery v0.31.4 h1:8xjE2C4CzhYVm9DGf60yohpNUh5AEBnPxCryPBECmlM= +k8s.io/apimachinery v0.31.4/go.mod h1:rsPdaZJfTfLsNJSQzNHQvYoTmxhoOEofxtOsF3rtsMo= +k8s.io/client-go v0.31.4 h1:t4QEXt4jgHIkKKlx06+W3+1JOwAFU/2OPiOo7H92eRQ= +k8s.io/client-go v0.31.4/go.mod h1:kvuMro4sFYIa8sulL5Gi5GFqUPvfH2O/dXuKstbaaeg= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= k8s.io/kube-openapi v0.0.0-20241127205056-99599406b04f h1:nLHvOvs1CZ+FAEwR4EqLeRLfbtWQNlIu5g393Hq/1UM= From eb2064ce56b61515a0c4cd66eb6853be89ecb949 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:49:17 +0100 Subject: [PATCH 3/3] fix(deps): update module github.com/labstack/echo/v4 to v4.13.1 (#6424) --- .../github.com/labstack/echo/otelecho/example/go.mod | 2 +- .../github.com/labstack/echo/otelecho/example/go.sum | 4 ++-- instrumentation/github.com/labstack/echo/otelecho/go.mod | 2 +- instrumentation/github.com/labstack/echo/otelecho/go.sum | 4 ++-- instrumentation/github.com/labstack/echo/otelecho/test/go.mod | 2 +- instrumentation/github.com/labstack/echo/otelecho/test/go.sum | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/instrumentation/github.com/labstack/echo/otelecho/example/go.mod b/instrumentation/github.com/labstack/echo/otelecho/example/go.mod index f027340f262..4d43ed09914 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/example/go.mod +++ b/instrumentation/github.com/labstack/echo/otelecho/example/go.mod @@ -8,7 +8,7 @@ replace ( ) require ( - github.com/labstack/echo/v4 v4.13.0 + github.com/labstack/echo/v4 v4.13.1 go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.57.0 go.opentelemetry.io/otel v1.32.0 go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.32.0 diff --git a/instrumentation/github.com/labstack/echo/otelecho/example/go.sum b/instrumentation/github.com/labstack/echo/otelecho/example/go.sum index e50a9be3917..86bafa827f0 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/example/go.sum +++ b/instrumentation/github.com/labstack/echo/otelecho/example/go.sum @@ -9,8 +9,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/labstack/echo/v4 v4.13.0 h1:8DjSi4H/k+RqoOmwXkxW14A2H1pdPdS95+qmdJ4q1Tg= -github.com/labstack/echo/v4 v4.13.0/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= +github.com/labstack/echo/v4 v4.13.1 h1:q3+CpQlYhJwpRr9+08pX0IdlabTIpnIhrg2AKPSKhFE= +github.com/labstack/echo/v4 v4.13.1/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= diff --git a/instrumentation/github.com/labstack/echo/otelecho/go.mod b/instrumentation/github.com/labstack/echo/otelecho/go.mod index d4211af10d0..fb735eac293 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/go.mod +++ b/instrumentation/github.com/labstack/echo/otelecho/go.mod @@ -5,7 +5,7 @@ go 1.22 replace go.opentelemetry.io/contrib/propagators/b3 => ../../../../../propagators/b3 require ( - github.com/labstack/echo/v4 v4.13.0 + github.com/labstack/echo/v4 v4.13.1 github.com/stretchr/testify v1.10.0 go.opentelemetry.io/contrib/propagators/b3 v1.32.0 go.opentelemetry.io/otel v1.32.0 diff --git a/instrumentation/github.com/labstack/echo/otelecho/go.sum b/instrumentation/github.com/labstack/echo/otelecho/go.sum index 3f17cb61fcf..c87b5906ffa 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/go.sum +++ b/instrumentation/github.com/labstack/echo/otelecho/go.sum @@ -7,8 +7,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/labstack/echo/v4 v4.13.0 h1:8DjSi4H/k+RqoOmwXkxW14A2H1pdPdS95+qmdJ4q1Tg= -github.com/labstack/echo/v4 v4.13.0/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= +github.com/labstack/echo/v4 v4.13.1 h1:q3+CpQlYhJwpRr9+08pX0IdlabTIpnIhrg2AKPSKhFE= +github.com/labstack/echo/v4 v4.13.1/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= diff --git a/instrumentation/github.com/labstack/echo/otelecho/test/go.mod b/instrumentation/github.com/labstack/echo/otelecho/test/go.mod index ea2bfb4ac16..4f96623101f 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/test/go.mod +++ b/instrumentation/github.com/labstack/echo/otelecho/test/go.mod @@ -4,7 +4,7 @@ module go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otel go 1.22 require ( - github.com/labstack/echo/v4 v4.13.0 + github.com/labstack/echo/v4 v4.13.1 github.com/stretchr/testify v1.10.0 go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.57.0 go.opentelemetry.io/otel v1.32.0 diff --git a/instrumentation/github.com/labstack/echo/otelecho/test/go.sum b/instrumentation/github.com/labstack/echo/otelecho/test/go.sum index 0bc39292138..f74a6b818a9 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/test/go.sum +++ b/instrumentation/github.com/labstack/echo/otelecho/test/go.sum @@ -9,8 +9,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/labstack/echo/v4 v4.13.0 h1:8DjSi4H/k+RqoOmwXkxW14A2H1pdPdS95+qmdJ4q1Tg= -github.com/labstack/echo/v4 v4.13.0/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= +github.com/labstack/echo/v4 v4.13.1 h1:q3+CpQlYhJwpRr9+08pX0IdlabTIpnIhrg2AKPSKhFE= +github.com/labstack/echo/v4 v4.13.1/go.mod h1:61j7WN2+bp8V21qerqRs4yVlVTGyOagMBpF0vE7VcmM= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=