add requests/errors to stats

This commit is contained in:
Asim Aslam
2019-12-18 18:36:42 +00:00
parent d4bec24eb7
commit d52a111735
8 changed files with 197 additions and 59 deletions

View File

@@ -4,7 +4,9 @@ import (
"context"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/debug/stats"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server"
)
type clientWrapper struct {
@@ -55,3 +57,19 @@ func FromService(name string, c client.Client) client.Client {
},
}
}
// HandlerStats wraps a server handler to generate request/error stats
func HandlerStats(stats stats.Stats) server.HandlerWrapper {
// return a handler wrapper
return func(h server.HandlerFunc) server.HandlerFunc {
// return a function that returns a function
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// execute the handler
err := h(ctx, req, rsp)
// record the stats
stats.Record(err)
// return the error
return err
}
}
}