From 60aa570dcdee552065bc72b5a46c2b17c948bc5b Mon Sep 17 00:00:00 2001 From: Rehan Pasha Date: Tue, 25 Jun 2024 08:08:01 -0400 Subject: [PATCH 1/6] Squashed commit of the following: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit f5116f4472a90ace1b336eecae28589d77311d92 Author: Rehan Pasha Date: Tue Jun 25 08:04:38 2024 -0400 Fixing lint error Signed-off-by: Rehan pasha commit 084e6725cdcce008bfae8a9cedbceeca65caf71c Author: Rehan Pasha Date: Tue Jun 11 14:42:26 2024 +0530 Squashed commit of the following: commit e6f9504a62f70f45c9e742a054ce05187f175176 Author: Pasha, Rehan Date: Tue Jun 11 14:34:25 2024 +0530 Update gintrace.go Signed-off-by: Pasha, Rehan commit 8f532bdd4dee57093934a2ce3f717b3ad657d0c4 Author: Pasha, Rehan Date: Tue Jun 11 14:31:49 2024 +0530 Update option.go Signed-off-by: Pasha, Rehan commit 52b9271c55f98f64bd10838209bde7fcced30dcc Merge: ec30e0b1 c7b10074 Author: Rehan Pasha Date: Tue Jun 11 14:30:06 2024 +0530 Merge branch 'rehanosp' of https://github.com/fidelity-external-staging/open-telemetry-opentelemetry-go-contrib into rehanosp commit ec30e0b158ca3a00c6df0e5495f34f990a59c153 Author: Rehan Pasha Date: Tue Jun 11 14:29:42 2024 +0530 Fixing the test commit c7b10074e6e4b90b16b384fe661d351d8ffbe035 Author: Rehan Pasha Date: Tue Jun 11 14:23:20 2024 +0530 Fixing the test commit 969a646a0cf4f0d430fd5fa4255f65c52d5bfee5 Merge: 737ff9e1 8b12e629 Author: Rehan Pasha Date: Tue Jun 11 14:06:24 2024 +0530 Merge branch 'main' of https://github.com/fidelity-external-staging/open-telemetry-opentelemetry-go-contrib into rehanosp commit 737ff9e1563fe7603819ad14c7b253146f1e2501 Author: Rehan Pasha Date: Fri Jun 7 18:33:59 2024 +0530 Squashed commit of the following: commit 93a2b553456d9bbb19b59da9d1e611ee096412a7 Merge: 73dd86e7 85969a37 Author: Rehan Pasha Date: Fri Jun 7 18:25:10 2024 +0530 Merge branch 'main' of https://github.com/fidelity-external-staging/open-telemetry-opentelemetry-go-contrib into rehanosp commit 73dd86e74e37ce595707fd8daa75df1af934706a Author: Rehan Pasha Date: Fri Jun 7 16:17:30 2024 +0530 feat: Update WithGinFilter to use GinFilter type The `WithGinFilter` function in `option.go` has been updated to use the `GinFilter` type instead of the generic `Filter` type. This change ensures that only `GinFilter` instances are added to the list of filters used by the handler. commit c0330a053d35a2294ae2010ab3bc13eb8a6906b3 Author: Rehan Pasha Date: Fri Jun 7 15:42:30 2024 +0530 Fixing the filter and adding test https://github.com/open-telemetry/opentelemetry-go-contrib/pull/5743#issuecomment-2154226335 commit 1facc34d155f18e7d85bedc2ecfeee2515000c2c Merge: ce53f638 3488eb8c Author: Rehan Pasha Date: Tue May 28 13:50:32 2024 +0530 Merge branch 'rehanosp' of https://github.com/fidelity-external-staging/open-telemetry-opentelemetry-go-contrib into rehanosp commit ce53f6387e9688a539ef3202aa0a8b1915b2b0f2 Author: Rehan Pasha Date: Tue May 28 13:50:12 2024 +0530 I’ve added my DCO signoff at the project’s request. There are no other changes. Signed-off-by: Rehan Pasha commit 3488eb8ca8b6a89a19b7699e6a4e1be26962dbf0 Merge: cce7c22e 606c2759 Author: Rehan Pasha Date: Tue May 28 13:35:07 2024 +0530 Merge branch 'rehanosp' of https://github.com/fidelity-external-staging/open-telemetry-opentelemetry-go-contrib into rehanosp commit cce7c22e50246c476a5260c36d53cc64f9eef374 Author: Rehan Pasha Date: Tue May 28 13:34:48 2024 +0530 feat: Add *gin.Context Filter parameter Signed-off-by: Rehan Pasha commit 606c275917b354cb834c9a7fe656a3934e86ee00 Author: Rehan Pasha Date: Mon May 20 19:59:53 2024 +0530 feat: Add `*gin.Context` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3070 Signed-off-by: Rehan Pasha Signed-off-by: Rehan Pasha Signed-off-by: Rehan Pasha Signed-off-by: Brian Warner --- CHANGELOG.md | 1 + .../gin-gonic/gin/otelgin/gintrace.go | 8 +++++ .../gin-gonic/gin/otelgin/option.go | 14 ++++++++ .../gin/otelgin/test/gintrace_test.go | 35 +++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c5d1a37850..3f8b2898c87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `WithSchemaURL` option function in `go.opentelemetry.io/contrib/bridges/otelslog`. This option function is used as a replacement of `WithInstrumentationScope` to specify the semantic convention schema URL for the logged records. (#5588) - Add support for Cloud Run jobs in `go.opentelemetry.io/contrib/detectors/gcp`. (#5559) +- Add `WithGinFilter` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering with the `gin.Context`. (#5743) ### Changed diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go index e40775d7c3e..1affd4d6ca5 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go @@ -52,6 +52,14 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc { return } } + for _, f := range cfg.GinFilters { + if !f(c) { + // Serve the request to the next middleware + // if a filter rejects the request. + c.Next() + return + } + } c.Set(tracerKey, tracer) savedCtx := c.Request.Context() defer func() { diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go index 113576ca00a..6d94c44f045 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go @@ -8,6 +8,8 @@ package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.co import ( "net/http" + "github.com/gin-gonic/gin" + "go.opentelemetry.io/otel/propagation" oteltrace "go.opentelemetry.io/otel/trace" ) @@ -16,6 +18,7 @@ type config struct { TracerProvider oteltrace.TracerProvider Propagators propagation.TextMapPropagator Filters []Filter + GinFilters []GinFilter SpanNameFormatter SpanNameFormatter } @@ -23,6 +26,10 @@ type config struct { // be traced. A Filter must return true if the request should be traced. type Filter func(*http.Request) bool +// Adding new Filter parameter (*gin.Context) +// gin.Context has FullPath() method, which returns a matched route full path. +type GinFilter func(*gin.Context) bool + // SpanNameFormatter is used to set span name by http.request. type SpanNameFormatter func(r *http.Request) string @@ -70,6 +77,13 @@ func WithFilter(f ...Filter) Option { }) } +// WithGinFilter adds a filter to the list of filters used by the handler. +func WithGinFilter(f ...GinFilter) Option { + return optionFunc(func(c *config) { + c.GinFilters = append(c.GinFilters, f...) + }) +} + // WithSpanNameFormatter takes a function that will be called on every // request and the returned string will become the Span Name. func WithSpanNameFormatter(f func(r *http.Request) string) Option { 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 628955c4586..04a3d4bc9bb 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 @@ -287,3 +287,38 @@ func TestWithFilter(t *testing.T) { assert.Len(t, sr.Ended(), 1) }) } + +func TestWithGinFilter(t *testing.T) { + t.Run("custom filter filtering route", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) + + router := gin.New() + f := func(c *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" } + router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) + router.GET("/healthcheck", func(c *gin.Context) {}) + + r := httptest.NewRequest("GET", "/healthcheck", nil) + w := httptest.NewRecorder() + + router.ServeHTTP(w, r) + assert.Len(t, sr.Ended(), 0) + }) + + t.Run("custom filter not filtering route", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) + + router := gin.New() + f := func(c *gin.Context) bool { return c.Request.URL.Path != "/user/:id" } + router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) + router.GET("/user/:id", func(c *gin.Context) {}) + + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() + + router.ServeHTTP(w, r) + assert.Len(t, sr.Ended(), 1) + }) +} + From d1920b99e19e9e12feb4b8d06c3c266d8043549b Mon Sep 17 00:00:00 2001 From: Rehan Pasha Date: Tue, 23 Jul 2024 11:41:30 -0400 Subject: [PATCH 2/6] Fixing lint and format Signed-off-by: Rehan Pasha Signed-off-by: Brian Warner --- .../gin/otelgin/test/gintrace_test.go | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) 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 04a3d4bc9bb..967bd8fc626 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 @@ -289,36 +289,35 @@ func TestWithFilter(t *testing.T) { } func TestWithGinFilter(t *testing.T) { - t.Run("custom filter filtering route", func(t *testing.T) { - sr := tracetest.NewSpanRecorder() - otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) + t.Run("custom filter filtering route", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) - router := gin.New() - f := func(c *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" } - router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) - router.GET("/healthcheck", func(c *gin.Context) {}) + router := gin.New() + f := func(c *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" } + router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) + router.GET("/healthcheck", func(c *gin.Context) {}) - r := httptest.NewRequest("GET", "/healthcheck", nil) - w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/healthcheck", nil) + w := httptest.NewRecorder() - router.ServeHTTP(w, r) - assert.Len(t, sr.Ended(), 0) - }) + router.ServeHTTP(w, r) + assert.Len(t, sr.Ended(), 0) + }) - t.Run("custom filter not filtering route", func(t *testing.T) { - sr := tracetest.NewSpanRecorder() - otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) + t.Run("custom filter not filtering route", func(t *testing.T) { + sr := tracetest.NewSpanRecorder() + otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) - router := gin.New() - f := func(c *gin.Context) bool { return c.Request.URL.Path != "/user/:id" } - router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) - router.GET("/user/:id", func(c *gin.Context) {}) + router := gin.New() + f := func(c *gin.Context) bool { return c.Request.URL.Path != "/user/:id" } + router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f))) + router.GET("/user/:id", func(c *gin.Context) {}) - r := httptest.NewRequest("GET", "/user/123", nil) - w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/user/123", nil) + w := httptest.NewRecorder() - router.ServeHTTP(w, r) - assert.Len(t, sr.Ended(), 1) - }) + router.ServeHTTP(w, r) + assert.Len(t, sr.Ended(), 1) + }) } - From 440abbbf2852a412925437fac938215cc6c71f75 Mon Sep 17 00:00:00 2001 From: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:04:35 +0530 Subject: [PATCH 3/6] Update CHANGELOG.md Signed-off-by: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f8b2898c87..6810e8f4b2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Support for the `OTEL_HTTP_CLIENT_COMPATIBILITY_MODE=http/dup` environment variable in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` to emit attributes for both the v1.20.0 and v1.26.0 semantic conventions. (#5401) - The `go.opentelemetry.io/contrib/bridges/otelzerolog` module. This module provides an OpenTelemetry logging bridge for `github.com/rs/zerolog`. (#5405) +- Add `WithGinFilter` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering with the `gin.Context`. (#5743) ### Removed @@ -93,7 +94,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `WithSchemaURL` option function in `go.opentelemetry.io/contrib/bridges/otelslog`. This option function is used as a replacement of `WithInstrumentationScope` to specify the semantic convention schema URL for the logged records. (#5588) - Add support for Cloud Run jobs in `go.opentelemetry.io/contrib/detectors/gcp`. (#5559) -- Add `WithGinFilter` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering with the `gin.Context`. (#5743) ### Changed From 65e2c13af01a978b910826c0c52dbd48092d8248 Mon Sep 17 00:00:00 2001 From: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> Date: Tue, 30 Jul 2024 15:04:55 +0530 Subject: [PATCH 4/6] Update CHANGELOG.md Co-authored-by: Damien Mathieu <42@dmathieu.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6810e8f4b2e..95c7019ecdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Support for the `OTEL_HTTP_CLIENT_COMPATIBILITY_MODE=http/dup` environment variable in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` to emit attributes for both the v1.20.0 and v1.26.0 semantic conventions. (#5401) - The `go.opentelemetry.io/contrib/bridges/otelzerolog` module. This module provides an OpenTelemetry logging bridge for `github.com/rs/zerolog`. (#5405) -- Add `WithGinFilter` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering with the `gin.Context`. (#5743) +- Add `WithGinFilter` filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering requests with `*gin.Context`. (#5743) ### Removed From 3766c00b74079e04ad96e6946e07a479735d8681 Mon Sep 17 00:00:00 2001 From: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> Date: Tue, 30 Jul 2024 15:14:30 +0530 Subject: [PATCH 5/6] Update option.go Signed-off-by: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> --- instrumentation/github.com/gin-gonic/gin/otelgin/option.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go index 6d94c44f045..a4bbf4faf39 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go @@ -77,7 +77,12 @@ func WithFilter(f ...Filter) Option { }) } -// WithGinFilter adds a filter to the list of filters used by the handler. +// WithGinFilter adds a gin filter to the list of filters used by the handler. +// If any filter indicates to exclude a request then the request will not be +// traced. All gin and net/http filters must allow a request to be traced for a Span to be created. +// If no filters are provided then all requests are traced. +// Filters will be invoked for each processed request, it is advised to make them +// simple and fast. func WithGinFilter(f ...GinFilter) Option { return optionFunc(func(c *config) { c.GinFilters = append(c.GinFilters, f...) From 54aedbe20fe417ab8547caa28636b1b2dc683dcb Mon Sep 17 00:00:00 2001 From: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:29:04 +0530 Subject: [PATCH 6/6] Update option.go Signed-off-by: rehanpfmr <111350825+rehanpfmr@users.noreply.github.com> --- instrumentation/github.com/gin-gonic/gin/otelgin/option.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go index a4bbf4faf39..143ca8e849e 100644 --- a/instrumentation/github.com/gin-gonic/gin/otelgin/option.go +++ b/instrumentation/github.com/gin-gonic/gin/otelgin/option.go @@ -67,7 +67,7 @@ func WithTracerProvider(provider oteltrace.TracerProvider) Option { // WithFilter adds a filter to the list of filters used by the handler. // If any filter indicates to exclude a request then the request will not be -// traced. All filters must allow a request to be traced for a Span to be created. +// traced. All gin and net/http filters must allow a request to be traced for a Span to be created. // If no filters are provided then all requests are traced. // Filters will be invoked for each processed request, it is advised to make them // simple and fast. @@ -78,11 +78,6 @@ func WithFilter(f ...Filter) Option { } // WithGinFilter adds a gin filter to the list of filters used by the handler. -// If any filter indicates to exclude a request then the request will not be -// traced. All gin and net/http filters must allow a request to be traced for a Span to be created. -// If no filters are provided then all requests are traced. -// Filters will be invoked for each processed request, it is advised to make them -// simple and fast. func WithGinFilter(f ...GinFilter) Option { return optionFunc(func(c *config) { c.GinFilters = append(c.GinFilters, f...)