From 95894274e1cf9714805851c41c628a8812788fe3 Mon Sep 17 00:00:00 2001 From: Qingsheng Ma Date: Wed, 5 Jun 2024 18:20:42 +0800 Subject: [PATCH 1/3] Using to set attribute --- CHANGELOG.md | 1 + .../gin-gonic/gin/otelgin/gintrace.go | 4 +-- .../gin/otelgin/test/gintrace_test.go | 36 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 245710157ac..f5d88e79bf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The superfluous `response.WriteHeader` call in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` when the response writer is flushed. (#5634) - Custom attributes targeting metrics recorded by the `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` are not ignored anymore. (#5129) +- Using `c.FullPath()` to set `http.route` attribute in [`otelgin`](https://github.com/NeoCN/opentelemetry-go-contrib/blob/main_backup/go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin) ### Deprecated diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go index df0fbf9c52b..e40775d7c3e 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go @@ -60,6 +60,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { ctx := cfg.Propagators.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header)) opts := []oteltrace.SpanStartOption{ oteltrace.WithAttributes(semconvutil.HTTPServerRequest(service, c.Request)...), + oteltrace.WithAttributes(semconv.HTTPRoute(c.FullPath())), oteltrace.WithSpanKind(oteltrace.SpanKindServer), } var spanName string @@ -70,9 +71,6 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { } if spanName == "" { spanName = fmt.Sprintf("HTTP %s route not found", c.Request.Method) - } else { - rAttr := semconv.HTTPRoute(spanName) - opts = append(opts, oteltrace.WithAttributes(rAttr)) } ctx, span := tracer.Start(ctx, spanName, opts...) defer span.End() diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go index afd6460983a..628955c4586 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go @@ -179,6 +179,42 @@ func TestSpanName(t *testing.T) { } } +func TestHTTPRouteWithSpanNameFormatter(t *testing.T) { + sr := tracetest.NewSpanRecorder() + provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) + + router := gin.New() + router.Use(otelgin.Middleware("foobar", + otelgin.WithTracerProvider(provider), + otelgin.WithSpanNameFormatter(func(r *http.Request) string { + return r.URL.Path + }), + ), + ) + router.GET("/user/:id", func(c *gin.Context) { + id := c.Param("id") + _, _ = c.Writer.Write([]byte(id)) + }) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + + // do and verify the request + router.ServeHTTP(w, r) + response := w.Result() + require.Equal(t, http.StatusOK, response.StatusCode) + + // verify traces look good + spans := sr.Ended() + require.Len(t, spans, 1) + span := spans[0] + assert.Equal(t, "/user/123", span.Name()) + assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind()) + attr := span.Attributes() + assert.Contains(t, attr, attribute.String("http.method", "GET")) + assert.Contains(t, attr, attribute.String("http.route", "/user/:id")) +} + func TestHTML(t *testing.T) { sr := tracetest.NewSpanRecorder() provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) From c95052726f76afb79794b5ec203d69b4d6368c19 Mon Sep 17 00:00:00 2001 From: Qingsheng Ma Date: Wed, 5 Jun 2024 19:54:47 +0800 Subject: [PATCH 2/3] fix link in CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5d88e79bf3..b6ab02e188a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The superfluous `response.WriteHeader` call in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` when the response writer is flushed. (#5634) - Custom attributes targeting metrics recorded by the `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` are not ignored anymore. (#5129) -- Using `c.FullPath()` to set `http.route` attribute in [`otelgin`](https://github.com/NeoCN/opentelemetry-go-contrib/blob/main_backup/go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin) +- Using `c.FullPath()` to set `http.route` attribute in [`otelgin`](go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin) ### Deprecated From 123385febda393202791c1f46773d6de1afb7fee Mon Sep 17 00:00:00 2001 From: Qingsheng Ma Date: Wed, 5 Jun 2024 20:01:38 +0800 Subject: [PATCH 3/3] update CHANGELOG entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6ab02e188a..b45186dfcc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The superfluous `response.WriteHeader` call in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` when the response writer is flushed. (#5634) - Custom attributes targeting metrics recorded by the `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` are not ignored anymore. (#5129) -- Using `c.FullPath()` to set `http.route` attribute in [`otelgin`](go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin) +- Use `c.FullPath()` method to set `http.route` attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#5734) ### Deprecated