From 73630da10a779e06dc2e3d4025946b9df397edf2 Mon Sep 17 00:00:00 2001 From: Julio Guerra Date: Tue, 22 Feb 2022 17:38:04 +0100 Subject: [PATCH] contrib/gorilla/mux: add a router monitoring wrapper (#1175) --- contrib/gorilla/mux/mux.go | 28 ++++++++++++---------------- contrib/gorilla/mux/option.go | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/contrib/gorilla/mux/mux.go b/contrib/gorilla/mux/mux.go index 96284b0b82..2207230f10 100644 --- a/contrib/gorilla/mux/mux.go +++ b/contrib/gorilla/mux/mux.go @@ -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" @@ -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 @@ -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 { diff --git a/contrib/gorilla/mux/option.go b/contrib/gorilla/mux/option.go index 6dbd6482bc..2d960244c5 100644 --- a/contrib/gorilla/mux/option.go +++ b/contrib/gorilla/mux/option.go @@ -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" @@ -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