2019-01-25 17:27:27 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewHandlerWrapper() server.HandlerWrapper {
|
|
|
|
opsCounter := prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2019-01-25 18:06:08 +03:00
|
|
|
Name: "micro_requests_total",
|
2019-01-25 19:11:45 +03:00
|
|
|
Help: "How many go-micro requests processed, partitioned by method and status",
|
2019-01-25 17:27:27 +03:00
|
|
|
},
|
|
|
|
[]string{"method", "status"},
|
|
|
|
)
|
|
|
|
|
|
|
|
timeCounter := prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2019-01-25 19:11:45 +03:00
|
|
|
Name: "micro_request_duration_microseconds",
|
2019-01-25 17:27:27 +03:00
|
|
|
Help: "Service method request latencies in microseconds",
|
|
|
|
},
|
|
|
|
[]string{"method"},
|
|
|
|
)
|
|
|
|
|
|
|
|
prometheus.MustRegister(opsCounter)
|
|
|
|
prometheus.MustRegister(timeCounter)
|
|
|
|
|
|
|
|
return func(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
name := req.Endpoint()
|
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
|
|
|
timeCounter.WithLabelValues(name).Observe(us)
|
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
if err == nil {
|
|
|
|
opsCounter.WithLabelValues(name, "success").Inc()
|
|
|
|
} else {
|
|
|
|
opsCounter.WithLabelValues(name, "fail").Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|