Skip to content

Commit

Permalink
contrib/gorilla/mux: add a router monitoring wrapper (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio-Guerra committed Feb 22, 2022
1 parent 0d15cb7 commit 73630da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
28 changes: 12 additions & 16 deletions contrib/gorilla/mux/mux.go
Expand Up @@ -7,13 +7,11 @@
package mux // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux"

import (
"math"
"net/http"
"strings"

httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"

Expand Down Expand Up @@ -76,20 +74,7 @@ func (r *Router) UseEncodedPath() *Router {

// NewRouter returns a new router instance traced with the global tracer.
func NewRouter(opts ...RouterOption) *Router {
cfg := new(routerConfig)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Measured())
log.Debug("contrib/gorilla/mux: Configuring Router: %#v", cfg)
return &Router{
Router: mux.NewRouter(),
config: cfg,
}
return WrapRouter(mux.NewRouter(), opts...)
}

// ServeHTTP dispatches the request to the handler
Expand Down Expand Up @@ -126,6 +111,17 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
})
}

// WrapRouter returns the given router wrapped with the tracing of the HTTP
// requests and responses served by the router.
func WrapRouter(router *mux.Router, opts ...RouterOption) *Router {
cfg := newConfig(opts)
log.Debug("contrib/gorilla/mux: Configuring Router: %#v", cfg)
return &Router{
Router: router,
config: cfg,
}
}

// defaultResourceNamer attempts to quantize the resource for an HTTP request by
// retrieving the path template associated with the route from the request.
func defaultResourceNamer(router *Router, req *http.Request) string {
Expand Down
14 changes: 14 additions & 0 deletions contrib/gorilla/mux/option.go
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
Expand All @@ -29,6 +30,19 @@ type routerConfig struct {
// RouterOption represents an option that can be passed to NewRouter.
type RouterOption func(*routerConfig)

func newConfig(opts []RouterOption) *routerConfig {
cfg := new(routerConfig)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Measured())
return cfg
}

func defaults(cfg *routerConfig) {
if internal.BoolEnv("DD_TRACE_MUX_ANALYTICS_ENABLED", false) {
cfg.analyticsRate = 1.0
Expand Down

0 comments on commit 73630da

Please sign in to comment.