mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
package prometheus
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/foomo/gotsrpc"
|
|
p "github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
func InstrumentService(s http.HandlerFunc) (handler http.HandlerFunc) {
|
|
counterVec := p.NewSummaryVec(p.SummaryOpts{
|
|
Namespace: "gotsrpc",
|
|
Subsystem: "service",
|
|
Name: "time_nanoseconds",
|
|
Help: "nanoseconds to unmarshal requests, execute a service function and marshal its reponses",
|
|
}, []string{"package", "service", "func", "type"})
|
|
p.MustRegister(counterVec)
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
*r = *gotsrpc.RequestWithStatsContext(r)
|
|
s.ServeHTTP(w, r)
|
|
stats := gotsrpc.GetStatsForRequest(r)
|
|
if stats != nil {
|
|
counterVec.WithLabelValues(stats.Package, stats.Service, stats.Func, "unmarshalling").Observe(float64(stats.Unmarshalling))
|
|
counterVec.WithLabelValues(stats.Package, stats.Service, stats.Func, "execution").Observe(float64(stats.Execution))
|
|
counterVec.WithLabelValues(stats.Package, stats.Service, stats.Func, "marshalling").Observe(float64(stats.Marshalling))
|
|
}
|
|
}
|
|
}
|
|
|
|
func InstrumentGoRPCService() gotsrpc.GoRPCCallStatsHandlerFun {
|
|
callsCounter := p.NewSummaryVec(p.SummaryOpts{
|
|
Namespace: "gorpc",
|
|
Subsystem: "service",
|
|
Name: "time_seconds",
|
|
Help: "seconds to execute a service method",
|
|
}, []string{"package", "service", "func", "type"})
|
|
p.MustRegister(callsCounter)
|
|
|
|
return func(stats *gotsrpc.CallStats) {
|
|
callsCounter.WithLabelValues(stats.Package, stats.Service, stats.Func, "execution").Observe(float64(stats.Execution))
|
|
}
|
|
}
|