-
Notifications
You must be signed in to change notification settings - Fork 415
/
http.go
97 lines (90 loc) · 3.29 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
// Package http provides functions to trace the net/http package (https://golang.org/pkg/net/http).
package http // import "github.com/DataDog/dd-trace-go/v2/contrib/net/http"
import (
"net/http"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/log"
)
// ServeMux is an HTTP request multiplexer that traces all the incoming requests.
type ServeMux struct {
*http.ServeMux
cfg *config
}
// NewServeMux allocates and returns an http.ServeMux augmented with the
// global tracer.
func NewServeMux(opts ...Option) *ServeMux {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn.apply(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, componentName))
log.Debug("contrib/net/http: Configuring ServeMux: %#v", cfg)
return &ServeMux{
ServeMux: http.NewServeMux(),
cfg: cfg,
}
}
// ServeHTTP dispatches the request to the handler
// whose pattern most closely matches the request URL.
// We only need to rewrite this function to be able to trace
// all the incoming requests to the underlying multiplexer
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if mux.cfg.ignoreRequest(r) {
mux.ServeMux.ServeHTTP(w, r)
return
}
// get the resource associated to this request
_, route := mux.Handler(r)
resource := mux.cfg.resourceNamer(r)
if resource == "" {
resource = r.Method + " " + route
}
so := make([]tracer.StartSpanOption, len(mux.cfg.spanOpts), len(mux.cfg.spanOpts)+1)
copy(so, mux.cfg.spanOpts)
so = append(so, httptrace.HeaderTagsFromRequest(r, mux.cfg.headerTags))
TraceAndServe(mux.ServeMux, w, r, &ServeConfig{
Service: mux.cfg.serviceName,
Resource: resource,
SpanOpts: so,
Route: route,
})
}
// 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)
for _, fn := range opts {
fn.apply(cfg)
}
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.SpanKind, ext.SpanKindServer))
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.Component, componentName))
log.Debug("contrib/net/http: Wrapping Handler: Service: %s, Resource: %s, %#v", service, resource, cfg)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if cfg.ignoreRequest(req) {
h.ServeHTTP(w, req)
return
}
resc := resource
if r := cfg.resourceNamer(req); r != "" {
resc = r
}
so := make([]tracer.StartSpanOption, len(cfg.spanOpts), len(cfg.spanOpts)+1)
copy(so, cfg.spanOpts)
so = append(so, httptrace.HeaderTagsFromRequest(req, cfg.headerTags))
TraceAndServe(h, w, req, &ServeConfig{
Service: service,
Resource: resc,
FinishOpts: cfg.finishOpts,
SpanOpts: so,
})
})
}