/
trace.go
99 lines (87 loc) · 3.51 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
// 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 "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
//go:generate sh -c "go run make_responsewriter.go | gofmt > trace_gen.go"
import (
"net/http"
"gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/httptrace"
"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/appsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo/instrumentation/httpsec"
)
// 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 []ddtrace.FinishOption
// SpanOpts specifies any options to be applied to the request starting span.
SpanOpts []ddtrace.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 := append(cfg.SpanOpts, tracer.ServiceName(cfg.Service), tracer.ResourceName(cfg.Resource))
if cfg.QueryParams {
opts = append(opts, tracer.Tag(ext.HTTPURL, r.URL.Path+"?"+r.URL.RawQuery))
}
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)
}
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
}