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: add router wrapper #1175

Merged
merged 1 commit into from Feb 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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