mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
pkg/web: closure-style middlewares (#51238)
* pkg/web: closure-style middlewares Switches the middleware execution model from web.Handlers in a slice to web.Middleware. Middlewares are temporarily kept in a slice to preserve ordering, but prior to execution they are applied, forming a giant call-stack, giving granular control over the execution flow. * pkg/middleware: adapt to web.Middleware * pkg/middleware/recovery: use c.Req over req c.Req gets updated by future handlers, while req stays static. The current recovery implementation needs this newer information * pkg/web: correct middleware ordering * pkg/webtest: adapt middleware * pkg/web/hack: set w and r onto web.Context By adopting std middlewares, it may happen they invoke next(w,r) without putting their modified w,r into the web.Context, leading old-style handlers to operate on outdated fields. pkg/web now takes care of this * pkg/middleware: selectively use future context * pkg/web: accept closure-style on Use() * webtest: Middleware testing adds a utility function to web/webtest to obtain a http.ResponseWriter, http.Request and http.Handler the same as a middleware that runs would receive * *: cleanup * pkg/web: don't wrap Middleware from Router * pkg/web: require chain to write response * *: remove temp files * webtest: don't require chain write * *: cleanup
This commit is contained in:
@@ -61,37 +61,38 @@ func routeOperationName(req *http.Request) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
func RequestTracing(tracer tracing.Tracer) web.Handler {
|
||||
return func(res http.ResponseWriter, req *http.Request, c *web.Context) {
|
||||
if strings.HasPrefix(c.Req.URL.Path, "/public/") ||
|
||||
c.Req.URL.Path == "/robots.txt" ||
|
||||
c.Req.URL.Path == "/favicon.ico" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
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 := res.(web.ResponseWriter)
|
||||
rw := web.Rw(w, req)
|
||||
|
||||
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)))
|
||||
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)))
|
||||
|
||||
c.Req = req.WithContext(ctx)
|
||||
c.Next()
|
||||
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.
|
||||
if routeOperation, exists := routeOperationName(c.Req); exists {
|
||||
defer span.End()
|
||||
span.SetName(fmt.Sprintf("HTTP %s %s", req.Method, routeOperation))
|
||||
}
|
||||
// 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()
|
||||
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)))
|
||||
}
|
||||
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)))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user