This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
trace.go
87 lines (75 loc) · 2.71 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
package httputil // import "github.com/signalfx/signalfx-go-tracing/contrib/internal/httputil"
//go:generate sh -c "go run make_responsewriter.go | gofmt > trace_gen.go"
import (
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/signalfx/signalfx-go-tracing/ddtrace"
"github.com/signalfx/signalfx-go-tracing/ddtrace/ext"
"github.com/signalfx/signalfx-go-tracing/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, spanopts ...ddtrace.StartSpanOption) {
originalURL := url.URL{
Scheme: "http",
Host: r.Host,
RawPath: r.URL.RawPath,
Path: r.URL.Path,
RawQuery: r.URL.RawQuery,
}
if r.TLS != nil {
originalURL.Scheme = "https"
}
opts := append([]ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeWeb),
tracer.ServiceName(service),
tracer.ResourceName(resource),
tracer.Tag(ext.HTTPMethod, r.Method),
tracer.Tag(ext.HTTPURL, originalURL.String()),
}, 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()
w = wrapResponseWriter(w, span)
if v := os.Getenv("SIGNALFX_TRACE_RESPONSE_HEADER_ENABLED"); !strings.EqualFold(v, "false") {
if traceParent, ok := tracer.FormatAsTraceParent(span.Context()); ok {
w.Header().Add("Access-Control-Expose-Headers", "Server-Timing")
w.Header().Add("Server-Timing", traceParent)
}
}
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) {
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)))
}
}