Skip to content

Commit

Permalink
contrib/gorilla/mux: add option for skipping tracing (#789)
Browse files Browse the repository at this point in the history
This update adds an option called WithSkippingFunc, which can be used for
skipping tracing based on request path.

Fixes #790
  • Loading branch information
stroem authored Dec 28, 2020
1 parent 402d87b commit ec4ee98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contrib/gorilla/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func NewRouter(opts ...RouterOption) *Router {
// We only need to rewrite this function to be able to trace
// all the incoming requests to the underlying multiplexer
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.config.ignoreRequest(req) {
r.Router.ServeHTTP(w, req)
return
}
var (
match mux.RouteMatch
spanopts []ddtrace.StartSpanOption
Expand Down
34 changes: 34 additions & 0 deletions contrib/gorilla/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,40 @@ func TestAnalyticsSettings(t *testing.T) {
})
}

func TestIgnoreRequestOption(t *testing.T) {
tests := []struct {
url string
spanCount int
}{
{
url: "/skip",
spanCount: 0,
},
{
url: "/200",
spanCount: 1,
},
}
mux := NewRouter(WithIgnoreRequest(func(req *http.Request) bool {
return req.URL.Path == "/skip"
}))
mux.Handle("/skip", okHandler()).Host("localhost")
mux.Handle("/200", okHandler()).Host("localhost")

for _, test := range tests {
t.Run(test.url, func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
r := httptest.NewRequest("GET", "http://localhost"+test.url, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)

spans := mt.FinishedSpans()
assert.Equal(t, test.spanCount, len(spans))
})
}
}

func TestResourceNamer(t *testing.T) {
staticName := "static resource name"
staticNamer := func(*Router, *http.Request) string {
Expand Down
10 changes: 10 additions & 0 deletions contrib/gorilla/mux/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type routerConfig struct {
finishOpts []ddtrace.FinishOption // span finish options to be applied
analyticsRate float64
resourceNamer func(*Router, *http.Request) string
ignoreRequest func(*http.Request) bool
}

// RouterOption represents an option that can be passed to NewRouter.
Expand All @@ -37,6 +38,15 @@ func defaults(cfg *routerConfig) {
cfg.serviceName = svc
}
cfg.resourceNamer = defaultResourceNamer
cfg.ignoreRequest = func(_ *http.Request) bool { return false }
}

// WithIgnoreRequest holds the function to use for determining if the
// incoming HTTP request tracing should be skipped.
func WithIgnoreRequest(f func(*http.Request) bool) RouterOption {
return func(cfg *routerConfig) {
cfg.ignoreRequest = f
}
}

// WithServiceName sets the given service name for the router.
Expand Down

0 comments on commit ec4ee98

Please sign in to comment.