package metrics import ( "context" "strconv" "time" "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/metrics" "github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/transport" ) // Option is metrics option. type Option func(*options) // WithRequests with requests counter. func WithRequests(c metrics.Counter) Option { return func(o *options) { o.requests = c } } // WithSeconds with seconds histogram. func WithSeconds(c metrics.Observer) Option { return func(o *options) { o.seconds = c } } type options struct { // counter: _requests_code_total{kind, method, code, reason} requests metrics.Counter // histogram: _requests_seconds_bucket{kind, method} seconds metrics.Observer } // Server is middleware server-side metrics. func Server(opts ...Option) middleware.Middleware { options := options{} for _, o := range opts { o(&options) } return func(handler middleware.Handler) middleware.Handler { return func(ctx context.Context, req interface{}) (interface{}, error) { var ( code int reason string kind string method string ) startTime := time.Now() if info, ok := transport.FromServerContext(ctx); ok { kind = info.Kind() method = info.Method() } reply, err := handler(ctx, req) if se := errors.FromError(err); se != nil { code = int(se.Code) reason = se.Reason } if options.requests != nil { options.requests.With(kind, method, strconv.Itoa(code), reason).Inc() } if options.seconds != nil { options.seconds.With(kind, method).Observe(time.Since(startTime).Seconds()) } return reply, err } } } // Client is middleware client-side metrics. func Client(opts ...Option) middleware.Middleware { options := options{} for _, o := range opts { o(&options) } return func(handler middleware.Handler) middleware.Handler { return func(ctx context.Context, req interface{}) (interface{}, error) { var ( code int reason string kind string method string ) startTime := time.Now() if info, ok := transport.FromClientContext(ctx); ok { kind = info.Kind() method = info.Method() } reply, err := handler(ctx, req) if se := errors.FromError(err); se != nil { code = int(se.Code) reason = se.Reason } if options.requests != nil { options.requests.With(kind, method, strconv.Itoa(code), reason).Inc() } if options.seconds != nil { options.seconds.With(kind, method).Observe(time.Since(startTime).Seconds()) } return reply, err } } }