2017-09-10 07:13:57 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2021-04-20 08:22:22 -05:00
|
|
|
"context"
|
2017-09-11 11:43:41 -05:00
|
|
|
"fmt"
|
2017-09-10 07:13:57 -05:00
|
|
|
"net/http"
|
2022-06-14 00:58:20 -05:00
|
|
|
"regexp"
|
2022-01-20 04:10:12 -06:00
|
|
|
"strconv"
|
2021-04-20 08:22:22 -05:00
|
|
|
"strings"
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
2023-10-03 07:54:20 -05:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
|
2022-01-20 04:10:12 -06:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2017-09-10 07:13:57 -05:00
|
|
|
)
|
|
|
|
|
2021-04-20 08:22:22 -05:00
|
|
|
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.
|
2021-10-11 07:30:59 -05:00
|
|
|
func ProvideRouteOperationName(name string) web.Handler {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request, c *web.Context) {
|
2022-08-25 03:11:27 -05:00
|
|
|
c.Req = addRouteNameToContext(c.Req, name)
|
2021-04-20 08:22:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 03:11:27 -05:00
|
|
|
func addRouteNameToContext(req *http.Request, operationName string) *http.Request {
|
|
|
|
// don't set route name if it's set
|
2023-03-31 08:38:09 -05:00
|
|
|
if _, exists := RouteOperationName(req); exists {
|
2022-08-25 03:11:27 -05:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(req.Context(), routeOperationNameKey, operationName)
|
|
|
|
return req.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
2022-06-14 00:58:20 -05:00
|
|
|
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")},
|
2022-09-22 05:03:20 -05:00
|
|
|
{handler: "/api/health", pathPattern: regexp.MustCompile("^/api/health")},
|
2022-06-14 00:58:20 -05:00
|
|
|
{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")},
|
|
|
|
}
|
|
|
|
|
2023-03-31 08:38:09 -05:00
|
|
|
// RouteOperationName receives the route operation name from context, if set.
|
|
|
|
func RouteOperationName(req *http.Request) (string, bool) {
|
2022-06-14 00:58:20 -05:00
|
|
|
if val := req.Context().Value(routeOperationNameKey); val != nil {
|
2021-04-20 08:22:22 -05:00
|
|
|
op, ok := val.(string)
|
|
|
|
return op, ok
|
|
|
|
}
|
|
|
|
|
2022-06-14 00:58:20 -05:00
|
|
|
for _, hp := range unnamedHandlers {
|
|
|
|
if hp.pathPattern.Match([]byte(req.URL.Path)) {
|
|
|
|
return hp.handler, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:22:22 -05:00
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
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))
|
2022-08-11 09:11:34 -05:00
|
|
|
ctx, span := tracer.Start(wireContext, fmt.Sprintf("HTTP %s %s", req.Method, req.URL.Path), trace.WithLinks(trace.LinkFromContext(wireContext)))
|
2022-08-09 07:58:50 -05:00
|
|
|
|
|
|
|
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
|
2023-03-31 08:38:09 -05:00
|
|
|
if routeOperation, exists := RouteOperationName(web.FromContext(req.Context()).Req); exists {
|
2022-08-09 07:58:50 -05:00
|
|
|
defer span.End()
|
|
|
|
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
|
|
|
|
}
|
|
|
|
|
|
|
|
status := rw.Status()
|
|
|
|
|
2023-10-03 07:54:20 -05:00
|
|
|
span.SetAttributes(semconv.HTTPStatusCode(status))
|
|
|
|
span.SetAttributes(semconv.HTTPURL(req.RequestURI))
|
|
|
|
span.SetAttributes(semconv.HTTPMethod(req.Method))
|
2022-08-09 07:58:50 -05:00
|
|
|
if status >= 400 {
|
|
|
|
span.SetStatus(codes.Error, fmt.Sprintf("error with HTTP status code %s", strconv.Itoa(status)))
|
|
|
|
}
|
|
|
|
})
|
2017-09-10 07:13:57 -05:00
|
|
|
}
|
|
|
|
}
|