diff --git a/pkg/srv/http/http.go b/pkg/srv/http/http.go index b87defc..4c5f6a2 100644 --- a/pkg/srv/http/http.go +++ b/pkg/srv/http/http.go @@ -10,6 +10,7 @@ import ( "net" "net/http" "os" + "regexp" "strings" "time" @@ -25,11 +26,12 @@ import ( ) var ( - httpMeter metric.Meter - httpTracer trace.Tracer - defReadTimeout = 10 * time.Second - defWriteTimeout = 10 * time.Second - defIdleTimeout = 15 * time.Second + httpMeter metric.Meter + httpTracer trace.Tracer + httpPatternWithMethodRegexp = regexp.MustCompile(`(\w+) .*`) + defReadTimeout = 10 * time.Second + defWriteTimeout = 10 * time.Second + defIdleTimeout = 15 * time.Second ) func prepHTTPServer(opts *opts.AppHTTP) *http.Server { @@ -84,12 +86,16 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server { } }), otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string { - _, pattern := mux.Handler(r) - if pattern != "" { - return pattern // Use the route pattern as the span name, e.g., "/users/{id}" + endpoint := r.URL.Path + if _, pattern := mux.Handler(r); pattern != "" { + endpoint = pattern } - // Fallback to the default naming convention if no route is found. - return operation + " " + r.URL.Path + + if httpPatternWithMethodRegexp.MatchString(endpoint) { + return endpoint + } + + return fmt.Sprintf("%s %s", r.Method, endpoint) })) // Set timeouts from defaults, override