track http status codes in metrics

This commit is contained in:
sm
2018-01-26 18:07:20 -08:00
parent cf10974419
commit 5997dc44d8
4 changed files with 24 additions and 2 deletions
+8 -1
View File
@@ -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