-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
metrics.go
116 lines (107 loc) · 2.9 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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/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}
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
)
if info, ok := grpc.FromServerContext(ctx); ok {
method = "POST"
path = info.FullMethod
} else if info, ok := http.FromServerContext(ctx); ok {
method = info.Request.Method
if route := mux.CurrentRoute(info.Request); route != nil {
// /path/123 -> /path/{id}
path, _ = route.GetPathTemplate()
} else {
path = info.Request.RequestURI
}
}
startTime := time.Now()
reply, err := handler(ctx, req)
if err != nil {
code = uint32(errors.Code(err))
}
if options.requests != nil {
options.requests.With(method, path, strconv.Itoa(int(code))).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
)
if info, ok := grpc.FromClientContext(ctx); ok {
method = "POST"
path = info.FullMethod
} else if info, ok := http.FromClientContext(ctx); ok {
method = info.Request.Method
path = info.Request.RequestURI
}
startTime := time.Now()
reply, err := handler(ctx, req)
if err != nil {
code = uint32(errors.Code(err))
}
if options.requests != nil {
options.requests.With(method, path, strconv.Itoa(int(code))).Inc()
}
if options.seconds != nil {
options.seconds.With(method, path).Observe(time.Since(startTime).Seconds())
}
return reply, err
}
}
}