|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016 Datadog, Inc. |
| 5 | + |
| 6 | +package httputil // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/httputil" |
| 7 | + |
| 8 | +//go:generate sh -c "go run make_responsewriter.go | gofmt > trace_gen.go" |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "strconv" |
| 14 | + |
| 15 | + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" |
| 16 | + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" |
| 17 | + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" |
| 18 | +) |
| 19 | + |
| 20 | +// TraceConfig defines the configuration for request tracing. |
| 21 | +type TraceConfig struct { |
| 22 | + ResponseWriter http.ResponseWriter // response writer |
| 23 | + Request *http.Request // request that is traced |
| 24 | + Service string // service name |
| 25 | + Resource string // resource name |
| 26 | + QueryParams bool // specifies that request query parameters should be appended to http.url tag |
| 27 | + FinishOpts []ddtrace.FinishOption // span finish options to be applied |
| 28 | + SpanOpts []ddtrace.StartSpanOption // additional span options to be applied |
| 29 | +} |
| 30 | + |
| 31 | +// TraceAndServe will apply tracing to the given http.Handler using the passed tracer under the given service and resource. |
| 32 | +func TraceAndServe(h http.Handler, cfg *TraceConfig) { |
| 33 | + path := cfg.Request.URL.Path |
| 34 | + if cfg.QueryParams { |
| 35 | + path += "?" + cfg.Request.URL.RawQuery |
| 36 | + } |
| 37 | + opts := append([]ddtrace.StartSpanOption{ |
| 38 | + tracer.SpanType(ext.SpanTypeWeb), |
| 39 | + tracer.ServiceName(cfg.Service), |
| 40 | + tracer.ResourceName(cfg.Resource), |
| 41 | + tracer.Tag(ext.HTTPMethod, cfg.Request.Method), |
| 42 | + tracer.Tag(ext.HTTPURL, path), |
| 43 | + }, cfg.SpanOpts...) |
| 44 | + if cfg.Request.URL.Host != "" { |
| 45 | + opts = append([]ddtrace.StartSpanOption{ |
| 46 | + tracer.Tag("http.host", cfg.Request.URL.Host), |
| 47 | + tracer.Tag("http.content-length", cfg.Request.ContentLength), |
| 48 | + }, opts...) |
| 49 | + } |
| 50 | + if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(cfg.Request.Header)); err == nil { |
| 51 | + opts = append(opts, tracer.ChildOf(spanctx)) |
| 52 | + } |
| 53 | + span, ctx := tracer.StartSpanFromContext(cfg.Request.Context(), "http.request", opts...) |
| 54 | + defer span.Finish(cfg.FinishOpts...) |
| 55 | + |
| 56 | + cfg.ResponseWriter = wrapResponseWriter(cfg.ResponseWriter, span) |
| 57 | + |
| 58 | + h.ServeHTTP(cfg.ResponseWriter, cfg.Request.WithContext(ctx)) |
| 59 | +} |
| 60 | + |
| 61 | +// responseWriter is a small wrapper around an http response writer that will |
| 62 | +// intercept and store the status of a request. |
| 63 | +type responseWriter struct { |
| 64 | + http.ResponseWriter |
| 65 | + span ddtrace.Span |
| 66 | + status int |
| 67 | +} |
| 68 | + |
| 69 | +func newResponseWriter(w http.ResponseWriter, span ddtrace.Span) *responseWriter { |
| 70 | + return &responseWriter{w, span, 0} |
| 71 | +} |
| 72 | + |
| 73 | +// Write writes the data to the connection as part of an HTTP reply. |
| 74 | +// We explicitely call WriteHeader with the 200 status code |
| 75 | +// in order to get it reported into the span. |
| 76 | +func (w *responseWriter) Write(b []byte) (int, error) { |
| 77 | + if w.status == 0 { |
| 78 | + w.WriteHeader(http.StatusOK) |
| 79 | + } |
| 80 | + return w.ResponseWriter.Write(b) |
| 81 | +} |
| 82 | + |
| 83 | +// WriteHeader sends an HTTP response header with status code. |
| 84 | +// It also sets the status code to the span. |
| 85 | +func (w *responseWriter) WriteHeader(status int) { |
| 86 | + if w.status != 0 { |
| 87 | + return |
| 88 | + } |
| 89 | + w.ResponseWriter.WriteHeader(status) |
| 90 | + w.status = status |
| 91 | + w.span.SetTag(ext.HTTPCode, strconv.Itoa(status)) |
| 92 | + if status >= 500 && status < 600 { |
| 93 | + w.span.SetTag(ext.Error, fmt.Errorf("%d: %s", status, http.StatusText(status))) |
| 94 | + } |
| 95 | +} |
0 commit comments