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

contrib/gorilla/mux: option for skipping tracing #789

Merged
merged 6 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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