-
Notifications
You must be signed in to change notification settings - Fork 415
/
trace.go
114 lines (101 loc) · 3.99 KB
/
trace.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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 // import "github.com/DataDog/dd-trace-go/v2/contrib/net/http"
//go:generate sh -c "go run make_responsewriter.go | gofmt > trace_gen.go"
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/appsec"
"github.com/DataDog/dd-trace-go/v2/internal/appsec/emitter/httpsec"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/httptrace"
"github.com/DataDog/dd-trace-go/v2/internal/contrib/options"
"github.com/DataDog/dd-trace-go/v2/internal/telemetry"
)
const componentName = "net/http"
func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported(componentName)
}
// ServeConfig specifies the tracing configuration when using TraceAndServe.
type ServeConfig struct {
// Service specifies the service name to use. If left blank, the global service name
// will be inherited.
Service string
// Resource optionally specifies the resource name for this request.
Resource string
// QueryParams should be true in order to append the URL query values to the "http.url" tag.
QueryParams bool
// Route is the request matched route if any, or is empty otherwise
Route string
// RouteParams specifies framework-specific route parameters (e.g. for route /user/:id coming
// in as /user/123 we'll have {"id": "123"}). This field is optional and is used for monitoring
// by AppSec. It is only taken into account when AppSec is enabled.
RouteParams map[string]string
// FinishOpts specifies any options to be used when finishing the request span.
FinishOpts []tracer.FinishOption
// SpanOpts specifies any options to be applied to the request starting span.
SpanOpts []tracer.StartSpanOption
}
// TraceAndServe serves the handler h using the given ResponseWriter and Request, applying tracing
// according to the specified config.
func TraceAndServe(h http.Handler, w http.ResponseWriter, r *http.Request, cfg *ServeConfig) {
if cfg == nil {
cfg = new(ServeConfig)
}
opts := options.Expand(cfg.SpanOpts, 2, 3) // make a copy of cfg.SpanOpts to avoid races.
// Pre-append span.kind and component tags to the options so that they can be overridden.
opts[0] = tracer.Tag(ext.SpanKind, ext.SpanKindServer)
opts[1] = tracer.Tag(ext.Component, componentName)
if cfg.Service != "" {
opts = append(opts, tracer.ServiceName(cfg.Service))
}
if cfg.Resource != "" {
opts = append(opts, tracer.ResourceName(cfg.Resource))
}
if cfg.Route != "" {
opts = append(opts, tracer.Tag(ext.HTTPRoute, cfg.Route))
}
span, ctx := httptrace.StartRequestSpan(r, opts...)
rw, ddrw := wrapResponseWriter(w)
defer func() {
httptrace.FinishRequestSpan(span, ddrw.status, cfg.FinishOpts...)
}()
if appsec.Enabled() {
h = httpsec.WrapHandler(h, span, cfg.RouteParams, nil)
}
h.ServeHTTP(rw, r.WithContext(ctx))
}
// responseWriter is a small wrapper around an http response writer that will
// intercept and store the status of a request.
type responseWriter struct {
http.ResponseWriter
status int
}
func newResponseWriter(w http.ResponseWriter) *responseWriter {
return &responseWriter{w, 0}
}
// Status returns the status code that was monitored.
func (w *responseWriter) Status() int {
return w.status
}
// Write writes the data to the connection as part of an HTTP reply.
// We explicitly call WriteHeader with the 200 status code
// in order to get it reported into the span.
func (w *responseWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.WriteHeader(http.StatusOK)
}
return w.ResponseWriter.Write(b)
}
// WriteHeader sends an HTTP response header with status code.
// It also sets the status code to the span.
func (w *responseWriter) WriteHeader(status int) {
if w.status != 0 {
return
}
w.ResponseWriter.WriteHeader(status)
w.status = status
}