fix(webui): name API spans after their route

This commit is contained in:
sam committed 2026-10-25 23:42:44 +11:00
1 parent 656feab2a4
commit 46f0643cf5
2 files changed
+33 -1

No files matched your search

@@ -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
}
+2 -1
View File
@@ -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)