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-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/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
"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) {
|
2021-04-20 08:22:22 -05:00
|
|
|
ctx := context.WithValue(c.Req.Context(), routeOperationNameKey, name)
|
2021-09-01 04:18:30 -05:00
|
|
|
c.Req = c.Req.WithContext(ctx)
|
2021-04-20 08:22:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteOperationNameFromContext receives the route operation name from context, if set.
|
|
|
|
func RouteOperationNameFromContext(ctx context.Context) (string, bool) {
|
|
|
|
if val := ctx.Value(routeOperationNameKey); val != nil {
|
|
|
|
op, ok := val.(string)
|
|
|
|
return op, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
func RequestTracing(tracer tracing.Tracer) web.Handler {
|
2021-10-11 07:30:59 -05:00
|
|
|
return func(res http.ResponseWriter, req *http.Request, c *web.Context) {
|
2021-04-20 08:22:22 -05:00
|
|
|
if strings.HasPrefix(c.Req.URL.Path, "/public/") ||
|
|
|
|
c.Req.URL.Path == "robots.txt" {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
rw := res.(web.ResponseWriter)
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
wireContext := otel.GetTextMapPropagator().Extract(req.Context(), propagation.HeaderCarrier(req.Header))
|
|
|
|
ctx, span := tracer.Start(req.Context(), fmt.Sprintf("HTTP %s %s", req.Method, req.URL.Path), trace.WithLinks(trace.LinkFromContext(wireContext)))
|
2017-09-10 07:13:57 -05:00
|
|
|
|
2021-09-01 04:18:30 -05:00
|
|
|
c.Req = req.WithContext(ctx)
|
2021-09-07 02:23:16 -05:00
|
|
|
c.Map(c.Req)
|
2017-09-10 07:13:57 -05:00
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
2021-04-20 08:22:22 -05:00
|
|
|
// Only call span.Finish when a route operation name have been set,
|
|
|
|
// meaning that not set the span would not be reported.
|
|
|
|
if routeOperation, exists := RouteOperationNameFromContext(c.Req.Context()); exists {
|
2022-01-20 04:10:12 -06:00
|
|
|
defer span.End()
|
|
|
|
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
|
2021-04-20 08:22:22 -05:00
|
|
|
}
|
|
|
|
|
2017-09-10 07:13:57 -05:00
|
|
|
status := rw.Status()
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
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))
|
2017-09-18 02:10:15 -05:00
|
|
|
if status >= 400 {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetStatus(codes.Error, fmt.Sprintf("error with HTTP status code %s", strconv.Itoa(status)))
|
2017-09-18 02:10:15 -05:00
|
|
|
}
|
2017-09-10 07:13:57 -05:00
|
|
|
}
|
|
|
|
}
|