forked from DataDog/dd-trace-go
/
trace.go
75 lines (64 loc) · 2.47 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
// 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-2020 Datadog, Inc.
package httputil // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/httputil"
//go:generate sh -c "go run make_responsewriter.go | gofmt > trace_gen.go"
import (
"fmt"
"net/http"
"strconv"
"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"
)
// TraceAndServe will apply tracing to the given http.Handler using the passed tracer under the given service and resource.
func TraceAndServe(h http.Handler, w http.ResponseWriter, r *http.Request, service, resource string,
finishopts []ddtrace.FinishOption, spanopts ...ddtrace.StartSpanOption) {
opts := append([]ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeWeb),
tracer.ServiceName(service),
tracer.ResourceName(resource),
tracer.Tag(ext.HTTPMethod, r.Method),
tracer.Tag(ext.HTTPURL, r.URL.Path),
}, spanopts...)
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
}
span, ctx := tracer.StartSpanFromContext(r.Context(), "http.request", opts...)
defer span.Finish(finishopts...)
w = wrapResponseWriter(w, span)
h.ServeHTTP(w, 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
span ddtrace.Span
status int
}
func newResponseWriter(w http.ResponseWriter, span ddtrace.Span) *responseWriter {
return &responseWriter{w, span, 0}
}
// Write writes the data to the connection as part of an HTTP reply.
// We explicitely 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
w.span.SetTag(ext.HTTPCode, strconv.Itoa(status))
if status >= 500 && status < 600 {
w.span.SetTag(ext.Error, fmt.Errorf("%d: %s", status, http.StatusText(status)))
}
}