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/net/http: add dynamic resource naming #1142

Merged
merged 17 commits into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 4 additions & 0 deletions contrib/net/http/http.go
Expand Up @@ -53,6 +53,7 @@ func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// WrapHandler wraps an http.Handler with tracing using the given service and resource.
// If the WithResourceNamer option is provided as part of opts, it will take precedence over the resource argument.
func WrapHandler(h http.Handler, service, resource string, opts ...Option) http.Handler {
cfg := new(config)
defaults(cfg)
Expand All @@ -65,6 +66,9 @@ func WrapHandler(h http.Handler, service, resource string, opts ...Option) http.
h.ServeHTTP(w, req)
return
}
if dynamicResource := cfg.resourceNamer(req); dynamicResource != "" {
resource = dynamicResource
jcogilvie marked this conversation as resolved.
Show resolved Hide resolved
}
TraceAndServe(h, w, req, &ServeConfig{
Service: service,
Resource: resource,
Expand Down
9 changes: 9 additions & 0 deletions contrib/net/http/option.go
Expand Up @@ -22,6 +22,7 @@ type config struct {
spanOpts []ddtrace.StartSpanOption
finishOpts []ddtrace.FinishOption
ignoreRequest func(*http.Request) bool
resourceNamer func(*http.Request) string
}

// MuxOption has been deprecated in favor of Option.
Expand All @@ -45,6 +46,7 @@ func defaults(cfg *config) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
cfg.ignoreRequest = func(_ *http.Request) bool { return false }
cfg.resourceNamer = func(_ *http.Request) string { return "" }
}

// WithIgnoreRequest holds the function to use for determining if the
Expand Down Expand Up @@ -95,6 +97,13 @@ func WithSpanOptions(opts ...ddtrace.StartSpanOption) Option {
}
}

// WithResourceNamer populates the name of a resource based on a custom function.
func WithResourceNamer(namer func(req *http.Request) string) Option {
return func(cfg *config) {
cfg.resourceNamer = namer
}
}

// NoDebugStack prevents stack traces from being attached to spans finishing
// with an error. This is useful in situations where errors are frequent and
// performance is critical.
Expand Down