FrontendService: Add tracing and logging middleware (#107956)

* FrontendService: Add tracing and logging middleware

* tests!

* middleware tests

* context middleware test

* revert http_server back to previous version

* fix lint

* fix test

* use http.NotFound instead of custom http handler

* use existing tracer for package

* use otel/trace.Tracer in request_tracing middleware

* tidy up tracing in contextMiddleware

* fix 404 test

* remove spans from contextMiddleware

* comment
This commit is contained in:
Josh Hunt
2025-07-22 18:55:44 +01:00
committed by GitHub
parent 92404d9579
commit 1bd9541507
8 changed files with 403 additions and 19 deletions
+15 -6
View File
@@ -71,14 +71,23 @@ func RouteOperationName(req *http.Request) (string, bool) {
return "", false
}
func RequestTracing(tracer tracing.Tracer) web.Middleware {
// Paths that don't need tracing spans applied to them because of the
// little value that would provide us
func SkipTracingPaths(req *http.Request) bool {
return strings.HasPrefix(req.URL.Path, "/public/") ||
req.URL.Path == "/robots.txt" ||
req.URL.Path == "/favicon.ico" ||
req.URL.Path == "/api/health"
}
func TraceAllPaths(req *http.Request) bool {
return true
}
func RequestTracing(tracer trace.Tracer, shouldTrace func(*http.Request) bool) web.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// skip tracing for a few endpoints
if strings.HasPrefix(req.URL.Path, "/public/") ||
req.URL.Path == "/robots.txt" ||
req.URL.Path == "/favicon.ico" ||
req.URL.Path == "/api/health" {
if !shouldTrace(req) {
next.ServeHTTP(w, req)
return
}