Automatic route/pattern naming for http.ServeMux
#6193
Labels
area: instrumentation
Related to an instrumentation package
enhancement
New feature or request
instrumentation: otelhttp
Problem Statement
Go recently added more sophisticated pattern matching to
http.ServeMux
, including matching methods and path variables. I think this patterns can make good span names: They are descriptive, but also bounded (unlike request paths).Furthermore, in Go 1.23,
http.ServeMux
populateshttp.Request.Pattern
, making the pattern that matched a request available for handlers and, more importantly, middlewares. It would be great ifotelhttp
provided, out of the box, an option to automatically name spans fromhttp.ServeMux
patterns.Proposed Solution
A middleware can be used to name spans using the mux pattern that matched:
I'm currently using this in this fashion:
I think this could be a nice addition to the
otelhttp
package, so users don't need to writespanNameFromPattern
on their own. We could fashion it as an option, which transparently adds this middleware:Or perhaps expose it as a middleware itself:
Furthermore, I think some more sophistication could be added to the middleware, for example checking if the pattern already includes a method, and add it if it doesn't.
Alternatives
Have users write this middleware themselves, or set span names manually in application logic.
Additional Context
I'd be happy to make a PR to
otelhttp
if you think this can be useful to have in there :)The text was updated successfully, but these errors were encountered: