-
Notifications
You must be signed in to change notification settings - Fork 11.8k
/
request_tracing.go
108 lines (89 loc) · 3.63 KB
/
request_tracing.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
package middleware
import (
"context"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/web"
)
type contextKey struct{}
var routeOperationNameKey = contextKey{}
// ProvideRouteOperationName creates a named middleware responsible for populating
// the context with the route operation name that can be used later in the request pipeline.
// Implements routing.RegisterNamedMiddleware.
func ProvideRouteOperationName(name string) web.Handler {
return func(res http.ResponseWriter, req *http.Request, c *web.Context) {
c.Req = addRouteNameToContext(c.Req, name)
}
}
func addRouteNameToContext(req *http.Request, operationName string) *http.Request {
// don't set route name if it's set
if _, exists := RouteOperationName(req); exists {
return req
}
ctx := context.WithValue(req.Context(), routeOperationNameKey, operationName)
return req.WithContext(ctx)
}
var unnamedHandlers = []struct {
pathPattern *regexp.Regexp
handler string
}{
{handler: "public-assets", pathPattern: regexp.MustCompile("^/favicon.ico")},
{handler: "public-assets", pathPattern: regexp.MustCompile("^/public/")},
{handler: "/metrics", pathPattern: regexp.MustCompile("^/metrics")},
{handler: "/healthz", pathPattern: regexp.MustCompile("^/healthz")},
{handler: "/api/health", pathPattern: regexp.MustCompile("^/api/health")},
{handler: "/robots.txt", pathPattern: regexp.MustCompile("^/robots.txt$")},
// bundle all pprof endpoints under the same handler name
{handler: "/debug/pprof-handlers", pathPattern: regexp.MustCompile("^/debug/pprof")},
}
// RouteOperationName receives the route operation name from context, if set.
func RouteOperationName(req *http.Request) (string, bool) {
if val := req.Context().Value(routeOperationNameKey); val != nil {
op, ok := val.(string)
return op, ok
}
for _, hp := range unnamedHandlers {
if hp.pathPattern.Match([]byte(req.URL.Path)) {
return hp.handler, true
}
}
return "", false
}
func RequestTracing(tracer tracing.Tracer) web.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if strings.HasPrefix(req.URL.Path, "/public/") || req.URL.Path == "/robots.txt" || req.URL.Path == "/favicon.ico" {
next.ServeHTTP(w, req)
return
}
rw := web.Rw(w, req)
wireContext := otel.GetTextMapPropagator().Extract(req.Context(), propagation.HeaderCarrier(req.Header))
ctx, span := tracer.Start(wireContext, fmt.Sprintf("HTTP %s %s", req.Method, req.URL.Path), trace.WithLinks(trace.LinkFromContext(wireContext)))
req = req.WithContext(ctx)
next.ServeHTTP(w, req)
// Only call span.Finish when a route operation name have been set,
// meaning that not set the span would not be reported.
// TODO: do not depend on web.Context from the future
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
defer span.End()
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
}
status := rw.Status()
span.SetAttributes("http.status_code", status, attribute.Int("http.status_code", status))
span.SetAttributes("http.url", req.RequestURI, attribute.String("http.url", req.RequestURI))
span.SetAttributes("http.method", req.Method, attribute.String("http.method", req.Method))
if status >= 400 {
span.SetStatus(codes.Error, fmt.Sprintf("error with HTTP status code %s", strconv.Itoa(status)))
}
})
}
}