track http status codes in metrics
This commit is contained in:
parent
cf10974419
commit
5997dc44d8
@ -23,7 +23,7 @@ func main() {
|
||||
}
|
||||
|
||||
s := &hwt.Server{hn}
|
||||
hs := hwt.NewMetricsHooks(metrics.HTTPLatency)
|
||||
hs := hwt.NewMetricsHooks(metrics.HTTPLatency, metrics.HTTPCode)
|
||||
th := pb.NewHelloWorldServer(s, hs)
|
||||
sm := http.NewServeMux()
|
||||
sm.HandleFunc("/", hwt.Auth(th.ServeHTTP))
|
||||
|
9
hooks.go
9
hooks.go
@ -10,8 +10,9 @@ import (
|
||||
var reqStartKey = new(int)
|
||||
|
||||
type Timer func(path string, dur time.Duration)
|
||||
type Statuser func(path, status string)
|
||||
|
||||
func NewMetricsHooks(timer Timer) *twirp.ServerHooks {
|
||||
func NewMetricsHooks(timer Timer, status Statuser) *twirp.ServerHooks {
|
||||
hs := &twirp.ServerHooks{}
|
||||
|
||||
hs.RequestReceived = func(ctx context.Context) (context.Context, error) {
|
||||
@ -31,6 +32,12 @@ func NewMetricsHooks(timer Timer) *twirp.ServerHooks {
|
||||
}
|
||||
dur := time.Now().Sub(start)
|
||||
timer(name, dur)
|
||||
|
||||
sc, ok := twirp.StatusCode(ctx)
|
||||
if !ok {
|
||||
panic("missing code")
|
||||
}
|
||||
status(name, sc)
|
||||
}
|
||||
|
||||
return hs
|
||||
|
@ -7,3 +7,7 @@ import (
|
||||
func HTTPLatency(path string, dur time.Duration) {
|
||||
httpReqLat.WithLabelValues(path).Observe(float64(dur) / float64(time.Millisecond))
|
||||
}
|
||||
|
||||
func HTTPCode(path string, status string) {
|
||||
httpStatus.WithLabelValues(path, status).Inc()
|
||||
}
|
||||
|
@ -21,11 +21,22 @@ var (
|
||||
},
|
||||
[]string{"path"},
|
||||
)
|
||||
|
||||
httpStatus = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "hwt_http_requests_total",
|
||||
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
|
||||
},
|
||||
[]string{"path", "code"},
|
||||
)
|
||||
)
|
||||
|
||||
func RegisterPromMetrics() error {
|
||||
if err := prometheus.Register(httpReqLat); err != nil {
|
||||
return errors.Wrap(err, "registering http request latency")
|
||||
}
|
||||
if err := prometheus.Register(httpStatus); err != nil {
|
||||
return errors.Wrap(err, "registering http request status")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user