diff --git a/internal/infrastructure/telemetry/telemetry.go b/internal/infrastructure/telemetry/telemetry.go index 792449263..6e5f394f9 100644 --- a/internal/infrastructure/telemetry/telemetry.go +++ b/internal/infrastructure/telemetry/telemetry.go @@ -24,6 +24,7 @@ import ( "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.43.0" + "go.opentelemetry.io/otel/trace" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -161,3 +162,33 @@ func isHealth(path string) bool { func Transport(base http.RoundTripper) http.RoundTripper { return otelhttp.NewTransport(base) } + +// Routed names the request's server span after the mux pattern that serves +// it. Middleware that copies the request hides the pattern from Handler, so +// the inner mux reports it itself. +func Routed(mux *http.ServeMux) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if _, pattern := mux.Handler(r); pattern != "" { + span := trace.SpanFromContext(r.Context()) + span.SetName(spanName(r.Method, pattern)) + span.SetAttributes(semconv.HTTPRoute(routeOf(pattern))) + } + mux.ServeHTTP(w, r) + }) +} + +// spanName is "METHOD /route"; a pattern that already names its method is +// used as is. +func spanName(method, pattern string) string { + if strings.Contains(pattern, " ") { + return pattern + } + return method + " " + pattern +} + +func routeOf(pattern string) string { + if _, route, ok := strings.Cut(pattern, " "); ok { + return route + } + return pattern +} diff --git a/internal/webui/server.go b/internal/webui/server.go index 04fa11390..46e83c16a 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -22,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/infrastructure/captureintake" + "github.com/samcharles93/archie-core/internal/infrastructure/telemetry" "github.com/samcharles93/archie-core/internal/logging" "github.com/samcharles93/archie-core/internal/ratelimit" "github.com/samcharles93/archie-core/internal/secret" @@ -438,7 +439,7 @@ func (s *Server) Handler() http.Handler { } // The chain runs after the credential check and before any handler: the // principal exists only once the credential resolved - authenticated := s.requireToken(s.authorize(mux)) + authenticated := s.requireToken(s.authorize(telemetry.Routed(mux))) top.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if s.Accounts != nil && r.Method == http.MethodGet && !strings.HasPrefix(r.URL.Path, "/api/") && r.URL.Path != healthDetailedRoute { s.assets().ServeHTTP(w, r)