2020-08-18 19:27:50 +12:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metrics"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2020-08-18 19:27:50 +12:00
|
|
|
)
|
|
|
|
|
|
|
|
// Wrapper provides a HandlerFunc for metrics.Reporter implementations:
|
|
|
|
type Wrapper struct {
|
|
|
|
reporter metrics.Reporter
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a *Wrapper configured with the given metrics.Reporter:
|
|
|
|
func New(reporter metrics.Reporter) *Wrapper {
|
|
|
|
return &Wrapper{
|
|
|
|
reporter: reporter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandlerFunc instruments handlers registered to a service:
|
|
|
|
func (w *Wrapper) HandlerFunc(handlerFunction server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
|
|
|
|
// Build some tags to describe the call:
|
|
|
|
tags := metrics.Tags{
|
|
|
|
"method": req.Method(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the clock:
|
|
|
|
callTime := time.Now()
|
|
|
|
|
|
|
|
// Run the handlerFunction:
|
|
|
|
err := handlerFunction(ctx, req, rsp)
|
|
|
|
|
|
|
|
// Add a result tag:
|
|
|
|
if err != nil {
|
2020-08-27 11:18:02 +03:00
|
|
|
tags["status"] = "failure"
|
2020-08-18 19:27:50 +12:00
|
|
|
} else {
|
2020-08-27 11:18:02 +03:00
|
|
|
tags["status"] = "success"
|
2020-08-18 19:27:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instrument the result (if the DefaultClient has been configured):
|
|
|
|
w.reporter.Timing("service.handler", time.Since(callTime), tags)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|