feat: standardize OpenTelemetry HTTP span names to include method and route pattern
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -25,11 +26,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
httpMeter metric.Meter
|
httpMeter metric.Meter
|
||||||
httpTracer trace.Tracer
|
httpTracer trace.Tracer
|
||||||
defReadTimeout = 10 * time.Second
|
httpPatternWithMethodRegexp = regexp.MustCompile(`(\w+) .*`)
|
||||||
defWriteTimeout = 10 * time.Second
|
defReadTimeout = 10 * time.Second
|
||||||
defIdleTimeout = 15 * time.Second
|
defWriteTimeout = 10 * time.Second
|
||||||
|
defIdleTimeout = 15 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
|
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 {
|
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
|
||||||
_, pattern := mux.Handler(r)
|
endpoint := r.URL.Path
|
||||||
if pattern != "" {
|
if _, pattern := mux.Handler(r); pattern != "" {
|
||||||
return pattern // Use the route pattern as the span name, e.g., "/users/{id}"
|
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
|
// Set timeouts from defaults, override
|
||||||
|
Reference in New Issue
Block a user