Skip to content

Commit

Permalink
contrib/gin-gonic: add option to ignore request (#1061)
Browse files Browse the repository at this point in the history
This commit adds an option allowing users to configure a function which
decides whether a request should be traced or not.

Fixes #1060
  • Loading branch information
mackjmr authored Dec 1, 2021
1 parent 6d1a7d0 commit c48db64
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contrib/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
}
log.Debug("contrib/gin-gonic/gin: Configuring Middleware: Service: %s, %#v", service, cfg)
return func(c *gin.Context) {
if cfg.ignoreRequest(c) {
return
}
resource := cfg.resourceNamer(c)
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(cfg.serviceName),
Expand Down
29 changes: 29 additions & 0 deletions contrib/gin-gonic/gin/gintrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"html/template"
"net/http/httptest"
"strings"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
Expand Down Expand Up @@ -340,6 +341,34 @@ func TestResourceNamerSettings(t *testing.T) {
})
}

func TestIgnoreRequestSettings(t *testing.T) {
router := gin.New()
router.Use(Middleware("foobar", WithIgnoreRequest(func(c *gin.Context) bool {
return strings.HasPrefix(c.Request.URL.Path, "/skip")
})))

router.GET("/OK", func(c *gin.Context) {
c.Writer.Write([]byte("OK"))
})

router.GET("/skip", func(c *gin.Context) {
c.Writer.Write([]byte("Skip"))
})

for path, shouldSkip := range map[string]bool{
"/OK": false,
"/skip": true,
"/skipfoo": true,
} {
mt := mocktracer.Start()
defer mt.Reset()

r := httptest.NewRequest("GET", "http://localhost"+path, nil)
router.ServeHTTP(httptest.NewRecorder(), r)
assert.Equal(t, shouldSkip, len(mt.FinishedSpans()) == 0)
}
}

func TestServiceName(t *testing.T) {
t.Run("default", func(t *testing.T) {
assert := assert.New(t)
Expand Down
10 changes: 10 additions & 0 deletions contrib/gin-gonic/gin/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type config struct {
analyticsRate float64
resourceNamer func(c *gin.Context) string
serviceName string
ignoreRequest func(c *gin.Context) bool
}

func newConfig(service string) *config {
Expand All @@ -36,6 +37,7 @@ func newConfig(service string) *config {
analyticsRate: rate,
resourceNamer: defaultResourceNamer,
serviceName: service,
ignoreRequest: func(_ *gin.Context) bool { return false },
}
}

Expand Down Expand Up @@ -73,6 +75,14 @@ func WithResourceNamer(namer func(c *gin.Context) string) Option {
}
}

// WithIgnoreRequest specifies a function to use for determining if the
// incoming HTTP request tracing should be skipped.
func WithIgnoreRequest(f func(c *gin.Context) bool) Option {
return func(cfg *config) {
cfg.ignoreRequest = f
}
}

func defaultResourceNamer(c *gin.Context) string {
// getName is a hacky way to check whether *gin.Context implements the FullPath()
// method introduced in v1.4.0, falling back to the previous implementation otherwise.
Expand Down

0 comments on commit c48db64

Please sign in to comment.