From c989923bb662dd0f9dd95d7d9cda51683e210162 Mon Sep 17 00:00:00 2001 From: Flc Date: Wed, 2 Apr 2025 23:09:27 +0800 Subject: [PATCH 1/4] feat(otelmux): update http.route attribute to support request.Pattern --- CHANGELOG.md | 1 + .../github.com/gorilla/mux/otelmux/mux.go | 15 ++-- .../gorilla/mux/otelmux/test/mux_test.go | 69 +++++++++++++------ 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b89474c4e23..7a1181802a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Jaeger remote sampler's probabilistic strategy now uses the same sampling algorithm as `trace.TraceIDRatioBased` in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#6892) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test` package. Use `Version` instead. +- Update `http.route` attribute to support `request.Pattern` in `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. ### Fixed diff --git a/instrumentation/github.com/gorilla/mux/otelmux/mux.go b/instrumentation/github.com/gorilla/mux/otelmux/mux.go index 73eec8584f7..95c6514440b 100644 --- a/instrumentation/github.com/gorilla/mux/otelmux/mux.go +++ b/instrumentation/github.com/gorilla/mux/otelmux/mux.go @@ -114,12 +114,15 @@ func (tw traceware) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - routeStr := "" - route := mux.CurrentRoute(r) - if route != nil { - routeStr, _ = route.GetPathTemplate() - if routeStr == "" { - routeStr, _ = route.GetPathRegexp() + routeStr := r.Pattern + + if routeStr == "" { + route := mux.CurrentRoute(r) + if route != nil { + routeStr, _ = route.GetPathTemplate() + if routeStr == "" { + routeStr, _ = route.GetPathRegexp() + } } } diff --git a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go index 78866e6a6ef..1d9c3c3fef6 100644 --- a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go +++ b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go @@ -94,29 +94,54 @@ func TestSDKIntegration(t *testing.T) { router.HandleFunc("/user/{id:[0-9]+}", ok) router.HandleFunc("/book/{title}", ok) - r0 := httptest.NewRequest("GET", "/user/123", nil) - r1 := httptest.NewRequest("GET", "/book/foo", nil) - w := httptest.NewRecorder() - router.ServeHTTP(w, r0) - router.ServeHTTP(w, r1) + tests := []struct { + name string + path string + reqFunc func(r *http.Request) + expected string + }{ + { + name: "user route", + path: "/user/123", + reqFunc: nil, + expected: "/user/{id:[0-9]+}", + }, + { + name: "book route", + path: "/book/foo", + reqFunc: nil, + expected: "/book/{title}", + }, + { + name: "book route with custom pattern", + path: "/book/bar", + reqFunc: func(r *http.Request) { r.Pattern = "/book/{custom}" }, + expected: "/book/{custom}", + }, + } - require.Len(t, sr.Ended(), 2) - assertSpan(t, sr.Ended()[0], - "/user/{id:[0-9]+}", - trace.SpanKindServer, - attribute.String("net.host.name", "foobar"), - attribute.Int("http.status_code", http.StatusOK), - attribute.String("http.method", "GET"), - attribute.String("http.route", "/user/{id:[0-9]+}"), - ) - assertSpan(t, sr.Ended()[1], - "/book/{title}", - trace.SpanKindServer, - attribute.String("net.host.name", "foobar"), - attribute.Int("http.status_code", http.StatusOK), - attribute.String("http.method", "GET"), - attribute.String("http.route", "/book/{title}"), - ) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer sr.Reset() + + r := httptest.NewRequest("GET", tt.path, nil) + if tt.reqFunc != nil { + tt.reqFunc(r) + } + + w := httptest.NewRecorder() + router.ServeHTTP(w, r) + + assertSpan(t, sr.Ended()[0], + tt.expected, + trace.SpanKindServer, + attribute.String("net.host.name", "foobar"), + attribute.Int("http.status_code", http.StatusOK), + attribute.String("http.method", "GET"), + attribute.String("http.route", tt.expected), + ) + }) + } } func TestNotFoundIsNotError(t *testing.T) { From cc3948caece4875ef407b444e72b9a7ea4482661 Mon Sep 17 00:00:00 2001 From: Flc Date: Wed, 2 Apr 2025 23:10:33 +0800 Subject: [PATCH 2/4] docs(changelog): update entry for http.route attribute to reflect support for request.Pattern --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a1181802a4..1c9c07bc4ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Jaeger remote sampler's probabilistic strategy now uses the same sampling algorithm as `trace.TraceIDRatioBased` in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#6892) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test` package. Use `Version` instead. -- Update `http.route` attribute to support `request.Pattern` in `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. +- Update `http.route` attribute to support `request.Pattern` in `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#7108) ### Fixed From 116c29d4e03d768296d92955e1ce036a52ff0032 Mon Sep 17 00:00:00 2001 From: Flc Date: Wed, 2 Apr 2025 23:43:22 +0800 Subject: [PATCH 3/4] test(mux): assert single span is ended in http route tests --- instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go index 1d9c3c3fef6..db4e23d333d 100644 --- a/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go +++ b/instrumentation/github.com/gorilla/mux/otelmux/test/mux_test.go @@ -132,6 +132,7 @@ func TestSDKIntegration(t *testing.T) { w := httptest.NewRecorder() router.ServeHTTP(w, r) + require.Len(t, sr.Ended(), 1) assertSpan(t, sr.Ended()[0], tt.expected, trace.SpanKindServer, From b7baee1a2e1f393d81104e0517f4bf4bbe36eda7 Mon Sep 17 00:00:00 2001 From: Flc Date: Sat, 12 Apr 2025 22:31:13 +0800 Subject: [PATCH 4/4] fix: resolve merge conflicts --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec557a4d2e9..d05bacf0ae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm The `OTEL_SEMCONV_STABILITY_OPT_IN=http/dup` environment variable can be still used to emit both the v1.20.0 and v1.26.0 semantic conventions. It is however impossible to emit only the 1.20.0 semantic conventions, as the next release will drop support for that environment variable. (#6899) - Update the Jaeger remote sampler to use "github.com/jaegertracing/jaeger-idl/proto-gen/api_v2" in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#7061) -- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test` package. Use `Version` instead. - Update `http.route` attribute to support `request.Pattern` in `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#7108) ### Fixed @@ -62,6 +61,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The deprecated `go.opentelemetry.io/contrib/config` package is removed, use `go.opentelemetry.io/contrib/otelconf` instead. (#6894) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda`, use `Version` function instead. (#7058) - The deprecated `SemVersion` function in `go.opentelemetry.io/contrib/samplers/probability/consistent` is removed, use `Version` instead. (#7072) +- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/test` package, use `Version` instead. (#7077) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`, use `Version` function instead. (#7084) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`, use `Version` function instead. (#7085) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/test`, use `Version` function instead. (#7142) @@ -83,11 +83,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`, use `Version` function instead. (#7154) - The deprecated `DefaultAttributeSetter` in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws` is removed, use the `DefaultAttributeBuilder` function instead. (#7127) - The deprecated `UnaryClientInterceptor` function is removed in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` use `NewClientHandler` function instead. (#7125) +- The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`, use `Version` function instead. (#7167) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace`, use `Version` function instead. (#7144) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace/test`, use `Version` function instead. (#7144) - The deprecated `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters/interceptor` package is removed, use `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters` instead. (#7110) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`, use `Version` function instead. (#7143) - The deprecated `SemVersion` function is removed in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/test`, use `Version` function instead. (#7143) +- The deprecated `SQSAttributeSetter` function is removed in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws` package, use `SQSAttributeBuilder` instead. (#7145)