Skip to content

Commit ae4d493

Browse files
committed
Squashed commit of the following:
commit f5116f4472a90ace1b336eecae28589d77311d92 Author: Rehan Pasha <[email protected]> Date: Tue Jun 25 08:04:38 2024 -0400 Fixing lint error Signed-off-by: Rehan pasha <[email protected]> commit 084e672 Author: Rehan Pasha <[email protected]> Date: Tue Jun 11 14:42:26 2024 +0530 Squashed commit of the following: commit e6f9504a62f70f45c9e742a054ce05187f175176 Author: Pasha, Rehan <[email protected]> Date: Tue Jun 11 14:34:25 2024 +0530 Update gintrace.go Signed-off-by: Pasha, Rehan <[email protected]> commit 8f532bdd4dee57093934a2ce3f717b3ad657d0c4 Author: Pasha, Rehan <[email protected]> Date: Tue Jun 11 14:31:49 2024 +0530 Update option.go Signed-off-by: Pasha, Rehan <[email protected]> commit 52b9271c55f98f64bd10838209bde7fcced30dcc Merge: ec30e0b1 c7b10074 Author: Rehan Pasha <[email protected]> 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 <[email protected]> Date: Tue Jun 11 14:29:42 2024 +0530 Fixing the test commit c7b10074e6e4b90b16b384fe661d351d8ffbe035 Author: Rehan Pasha <[email protected]> Date: Tue Jun 11 14:23:20 2024 +0530 Fixing the test commit 969a646a0cf4f0d430fd5fa4255f65c52d5bfee5 Merge: 737ff9e 8b12e62 Author: Rehan Pasha <[email protected]> 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 737ff9e Author: Rehan Pasha <[email protected]> Date: Fri Jun 7 18:33:59 2024 +0530 Squashed commit of the following: commit 93a2b553456d9bbb19b59da9d1e611ee096412a7 Merge: 73dd86e7 85969a3 Author: Rehan Pasha <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Fri Jun 7 15:42:30 2024 +0530 Fixing the filter and adding test #5743 (comment) commit 1facc34 Merge: ce53f63 3488eb8 Author: Rehan Pasha <[email protected]> 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 ce53f63 Author: Rehan Pasha <[email protected]> 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 <[email protected]> commit 3488eb8 Merge: cce7c22 606c275 Author: Rehan Pasha <[email protected]> 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 cce7c22 Author: Rehan Pasha <[email protected]> Date: Tue May 28 13:34:48 2024 +0530 feat: Add *gin.Context Filter parameter Signed-off-by: Rehan Pasha <[email protected]> commit 606c275 Author: Rehan Pasha <[email protected]> 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` #3070 Signed-off-by: Rehan Pasha <[email protected]> Signed-off-by: Rehan Pasha <[email protected]> Signed-off-by: Rehan Pasha <[email protected]>
1 parent 170882f commit ae4d493

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
6262
- The `WithSchemaURL` option function in `go.opentelemetry.io/contrib/bridges/otelslog`.
6363
This option function is used as a replacement of `WithInstrumentationScope` to specify the semantic convention schema URL for the logged records. (#5588)
6464
- Add support for Cloud Run jobs in `go.opentelemetry.io/contrib/detectors/gcp`. (#5559)
65+
- Add `WithGinFilter` Filter parameter in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin` to allow filtering with the `gin.Context`. (#5743)
6566

6667
### Changed
6768

instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
5252
return
5353
}
5454
}
55+
for _, f := range cfg.GinFilters {
56+
if !f(c) {
57+
// Serve the request to the next middleware
58+
// if a filter rejects the request.
59+
c.Next()
60+
return
61+
}
62+
}
5563
c.Set(tracerKey, tracer)
5664
savedCtx := c.Request.Context()
5765
defer func() {

instrumentation/github.com/gin-gonic/gin/otelgin/option.go

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.co
88
import (
99
"net/http"
1010

11+
"github.com/gin-gonic/gin"
12+
1113
"go.opentelemetry.io/otel/propagation"
1214
oteltrace "go.opentelemetry.io/otel/trace"
1315
)
@@ -16,13 +18,18 @@ type config struct {
1618
TracerProvider oteltrace.TracerProvider
1719
Propagators propagation.TextMapPropagator
1820
Filters []Filter
21+
GinFilters []GinFilter
1922
SpanNameFormatter SpanNameFormatter
2023
}
2124

2225
// Filter is a predicate used to determine whether a given http.request should
2326
// be traced. A Filter must return true if the request should be traced.
2427
type Filter func(*http.Request) bool
2528

29+
// Adding new Filter parameter (*gin.Context)
30+
// gin.Context has FullPath() method, which returns a matched route full path.
31+
type GinFilter func(*gin.Context) bool
32+
2633
// SpanNameFormatter is used to set span name by http.request.
2734
type SpanNameFormatter func(r *http.Request) string
2835

@@ -70,6 +77,13 @@ func WithFilter(f ...Filter) Option {
7077
})
7178
}
7279

80+
// WithGinFilter adds a filter to the list of filters used by the handler.
81+
func WithGinFilter(f ...GinFilter) Option {
82+
return optionFunc(func(c *config) {
83+
c.GinFilters = append(c.GinFilters, f...)
84+
})
85+
}
86+
7387
// WithSpanNameFormatter takes a function that will be called on every
7488
// request and the returned string will become the Span Name.
7589
func WithSpanNameFormatter(f func(r *http.Request) string) Option {

instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,38 @@ func TestWithFilter(t *testing.T) {
287287
assert.Len(t, sr.Ended(), 1)
288288
})
289289
}
290+
291+
func TestWithGinFilter(t *testing.T) {
292+
t.Run("custom filter filtering route", func(t *testing.T) {
293+
sr := tracetest.NewSpanRecorder()
294+
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))
295+
296+
router := gin.New()
297+
f := func(c *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" }
298+
router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f)))
299+
router.GET("/healthcheck", func(c *gin.Context) {})
300+
301+
r := httptest.NewRequest("GET", "/healthcheck", nil)
302+
w := httptest.NewRecorder()
303+
304+
router.ServeHTTP(w, r)
305+
assert.Len(t, sr.Ended(), 0)
306+
})
307+
308+
t.Run("custom filter not filtering route", func(t *testing.T) {
309+
sr := tracetest.NewSpanRecorder()
310+
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))
311+
312+
router := gin.New()
313+
f := func(c *gin.Context) bool { return c.Request.URL.Path != "/user/:id" }
314+
router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f)))
315+
router.GET("/user/:id", func(c *gin.Context) {})
316+
317+
r := httptest.NewRequest("GET", "/user/123", nil)
318+
w := httptest.NewRecorder()
319+
320+
router.ServeHTTP(w, r)
321+
assert.Len(t, sr.Ended(), 1)
322+
})
323+
}
324+

0 commit comments

Comments
 (0)