census-instrumentation / opencensus-go

A stats collection and distributed tracing framework
http://opencensus.io
Apache License 2.0
2.06k stars 327 forks source link

Custome function to get monitor path from request #1213

Closed j2gg0s closed 4 years ago

j2gg0s commented 4 years ago

With census, I can stats http request easily. Thanks a lot.

I use mux for http router, it support template variable with path. Such as

r := mux.NewRouter()
s := r.PathPrefix("/products").Subrouter()
// "/products/"
s.HandleFunc("/", ProductsHandler)
// "/products/{key}/"
s.HandleFunc("/{key}/", ProductHandler)
// "/products/{key}/details"
s.HandleFunc("/{key}/details", ProductDetailsHandler)

With this, i got lots of metrics with different tags(http_path). It's meaningless to me.

Maybe census allow me to pass a callback to get path from request? Like this:

type Hanlder struct {
       func getPathFromRequest(*http.Request) string
}

func (h *http.Request) startStats(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, func(tags *addedTags)) {
    ctx, _ := tag.New(r.Context(),
        tag.Upsert(Host, r.Host),
        tag.Upsert(Path, h.getPathFromReques(r)),
        tag.Upsert(Method, r.Method))
}
james-bebbington commented 4 years ago

Thanks for the feature request. This sounds like a valuable feature, however, at this time, most development effort is focused on OpenTelemetry (currently in beta) rather than OpenCensus.

Please take a look at the following OpenTelemetry plugins and see if they fit your use case. If not, please create an issue on the OpenTelemetry boards: https://github.com/open-telemetry/opentelemetry-go/tree/master/instrumentation/othttp https://github.com/open-telemetry/opentelemetry-go-contrib/tree/master/instrumentation/gorilla/mux