Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelgin: Change the Filter parameter from *http.Request to *gin.Context #4444

Closed
wants to merge 12 commits into from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Changed

- Change the `Filter` parameter from `*http.Request` to `*gin.Context` in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#4444)

### Added

- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
}
return func(c *gin.Context) {
for _, f := range cfg.Filters {
if !f(c.Request) {
if !f(c) {
// Serve the request to the next middleware
// if a filter rejects the request.
c.Next()
Expand Down
5 changes: 3 additions & 2 deletions instrumentation/github.com/gin-gonic/gin/otelgin/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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"
)
Expand All @@ -30,9 +31,9 @@ type config struct {
SpanNameFormatter SpanNameFormatter
}

// Filter is a predicate used to determine whether a given http.request should
// Filter is a predicate used to determine whether a given gin.Context should
// be traced. A Filter must return true if the request should be traced.
type Filter func(*http.Request) bool
type Filter func(*gin.Context) bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

I would rather add type GinFilter func(*gin.Context) bool and WithGinFilter and keep the old functionality. Reasons:

  1. users already happy with existing API would not need to change anything
  2. some may already have some code designed/reusable for net/http

I am letting @hanyuancheung (who is the codeowner) whether it is a good idea or not.


// SpanNameFormatter is used to set span name by http.request.
type SpanNameFormatter func(r *http.Request) string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestWithFilter(t *testing.T) {
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))

router := gin.New()
f := func(req *http.Request) bool { return req.URL.Path != "/healthcheck" }
f := func(req *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" }
router.Use(otelgin.Middleware("foobar", otelgin.WithFilter(f)))
router.GET("/healthcheck", func(c *gin.Context) {})

Expand All @@ -251,7 +251,7 @@ func TestWithFilter(t *testing.T) {
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)))

router := gin.New()
f := func(req *http.Request) bool { return req.URL.Path != "/healthcheck" }
f := func(req *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" }
router.Use(otelgin.Middleware("foobar", otelgin.WithFilter(f)))
router.GET("/user/:id", func(c *gin.Context) {})

Expand Down
Loading