You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/middleware/metrics/metrics.go

120 lines
3.1 KiB

package metrics
import (
"context"
"strconv"
"time"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/internal/httputil"
"github.com/go-kratos/kratos/v2/metrics"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/gorilla/mux"
)
// 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: <kind>_<client/server>_requests_code_total{method, path, code, reason}
requests metrics.Counter
// histogram: <kind>_<client/server>_requests_seconds_bucket{method, path}
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 (
method string
path string
code uint32
)
startTime := time.Now()
reply, err := handler(ctx, req)
if info, ok := grpc.FromServerContext(ctx); ok {
method = "POST"
path = info.FullMethod
code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err)))
} else if info, ok := http.FromServerContext(ctx); ok {
req := info.Request.WithContext(ctx)
method = req.Method
if route := mux.CurrentRoute(req); route != nil {
// /path/123 -> /path/{id}
path, _ = route.GetPathTemplate()
} else {
path = req.RequestURI
}
code = uint32(errors.Code(err))
}
if options.requests != nil {
options.requests.With(method, path, strconv.Itoa(int(code)), errors.Reason(err)).Inc()
}
if options.seconds != nil {
options.seconds.With(method, path).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 (
method string
path string
code uint32
)
startTime := time.Now()
reply, err := handler(ctx, req)
if info, ok := grpc.FromClientContext(ctx); ok {
method = "POST"
path = info.FullMethod
code = uint32(httputil.GRPCCodeFromStatus(errors.Code(err)))
} else if info, ok := http.FromClientContext(ctx); ok {
method = info.Request.Method
path = info.Request.RequestURI
code = uint32(errors.Code(err))
}
if options.requests != nil {
options.requests.With(method, path, strconv.Itoa(int(code)), errors.Reason(err)).Inc()
}
if options.seconds != nil {
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
}
return reply, err
}
}
}