aboutsummaryrefslogtreecommitdiffstats
path: root/routers/common/middleware.go
diff options
context:
space:
mode:
Diffstat (limited to 'routers/common/middleware.go')
-rw-r--r--routers/common/middleware.go27
1 files changed, 20 insertions, 7 deletions
diff --git a/routers/common/middleware.go b/routers/common/middleware.go
index 12b0c67b01..2ba02de8ed 100644
--- a/routers/common/middleware.go
+++ b/routers/common/middleware.go
@@ -9,6 +9,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/cache"
+ "code.gitea.io/gitea/modules/gtprof"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/reqctx"
"code.gitea.io/gitea/modules/setting"
@@ -43,14 +44,26 @@ func ProtocolMiddlewares() (handlers []any) {
func RequestContextHandler() func(h http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
- profDesc := fmt.Sprintf("%s: %s", req.Method, req.RequestURI)
+ return http.HandlerFunc(func(respOrig http.ResponseWriter, req *http.Request) {
+ // this response writer might not be the same as the one in context.Base.Resp
+ // because there might be a "gzip writer" in the middle, so the "written size" here is the compressed size
+ respWriter := context.WrapResponseWriter(respOrig)
+
+ profDesc := fmt.Sprintf("HTTP: %s %s", req.Method, req.RequestURI)
ctx, finished := reqctx.NewRequestContext(req.Context(), profDesc)
defer finished()
+ ctx, span := gtprof.GetTracer().Start(ctx, gtprof.TraceSpanHTTP)
+ req = req.WithContext(ctx)
+ defer func() {
+ chiCtx := chi.RouteContext(req.Context())
+ span.SetAttributeString(gtprof.TraceAttrHTTPRoute, chiCtx.RoutePattern())
+ span.End()
+ }()
+
defer func() {
if err := recover(); err != nil {
- RenderPanicErrorPage(resp, req, err) // it should never panic
+ RenderPanicErrorPage(respWriter, req, err) // it should never panic
}
}()
@@ -62,7 +75,7 @@ func RequestContextHandler() func(h http.Handler) http.Handler {
_ = req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
}
})
- next.ServeHTTP(context.WrapResponseWriter(resp), req)
+ next.ServeHTTP(respWriter, req)
})
}
}
@@ -71,11 +84,11 @@ func ChiRoutePathHandler() func(h http.Handler) http.Handler {
// make sure chi uses EscapedPath(RawPath) as RoutePath, then "%2f" could be handled correctly
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
- ctx := chi.RouteContext(req.Context())
+ chiCtx := chi.RouteContext(req.Context())
if req.URL.RawPath == "" {
- ctx.RoutePath = req.URL.EscapedPath()
+ chiCtx.RoutePath = req.URL.EscapedPath()
} else {
- ctx.RoutePath = req.URL.RawPath
+ chiCtx.RoutePath = req.URL.RawPath
}
next.ServeHTTP(resp, req)
})