-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
metrics.go
210 lines (191 loc) · 5.65 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package metrics
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/grpc/codes"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http/status"
)
const (
metricLabelKind = "kind"
metricLabelOperation = "operation"
metricLabelCode = "code"
metricLabelReason = "reason"
)
const (
DefaultServerSecondsHistogramName = "server_requests_seconds_bucket"
DefaultServerRequestsCounterName = "server_requests_code_total"
DefaultClientSecondsHistogramName = "client_requests_seconds_bucket"
DefaultClientRequestsCounterName = "client_requests_code_total"
)
// Option is metrics option.
type Option func(*options)
// WithRequests with requests counter.
func WithRequests(c metric.Int64Counter) Option {
return func(o *options) {
o.requests = c
}
}
// WithSeconds with seconds histogram.
// notice: the record unit in current middleware is s(Seconds)
func WithSeconds(histogram metric.Float64Histogram) Option {
return func(o *options) {
o.seconds = histogram
}
}
// DefaultRequestsCounter
// return metric.Int64Counter for WithRequests
// suggest histogramName = <client/server>_requests_code_total
func DefaultRequestsCounter(meter metric.Meter, histogramName string) (metric.Int64Counter, error) {
return meter.Int64Counter(histogramName, metric.WithUnit("{call}"))
}
// DefaultSecondsHistogram
// return metric.Float64Histogram for WithSeconds
// suggest histogramName = <client/server>_requests_seconds_bucket
func DefaultSecondsHistogram(meter metric.Meter, histogramName string) (metric.Float64Histogram, error) {
return meter.Float64Histogram(
histogramName,
metric.WithUnit("s"),
metric.WithExplicitBucketBoundaries(0.005, 0.01, 0.025, 0.05, 0.1, 0.250, 0.5, 1),
)
}
// DefaultSecondsHistogramView
// need register in sdkmetric.MeterProvider
// eg:
// view := SecondsHistogramView()
// mp := sdkmetric.NewMeterProvider(sdkmetric.WithView(view))
// otel.SetMeterProvider(mp)
func DefaultSecondsHistogramView(histogramName string) metricsdk.View {
return func(instrument metricsdk.Instrument) (metricsdk.Stream, bool) {
if instrument.Name == histogramName {
return metricsdk.Stream{
Name: instrument.Name,
Description: instrument.Description,
Unit: instrument.Unit,
Aggregation: metricsdk.AggregationExplicitBucketHistogram{
Boundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.250, 0.5, 1},
NoMinMax: true,
},
AttributeFilter: func(attribute.KeyValue) bool {
return true
},
}, true
}
return metricsdk.Stream{}, false
}
}
type options struct {
// counter: <client/server>_requests_code_total{kind, operation, code, reason}
requests metric.Int64Counter
// histogram: <client/server>_requests_seconds_bucket{kind, operation}
seconds metric.Float64Histogram
}
// Server is middleware server-side metrics.
func Server(opts ...Option) middleware.Middleware {
op := options{}
for _, o := range opts {
o(&op)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
// if requests and seconds are nil, return directly
if op.requests == nil && op.seconds == nil {
return handler(ctx, req)
}
var (
code int
reason string
kind string
operation string
)
// default code
code = status.FromGRPCCode(codes.OK)
startTime := time.Now()
if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind().String()
operation = info.Operation()
}
reply, err := handler(ctx, req)
if se := errors.FromError(err); se != nil {
code = int(se.Code)
reason = se.Reason
}
if op.requests != nil {
op.requests.Add(
ctx, 1,
metric.WithAttributes(
attribute.String(metricLabelKind, kind),
attribute.String(metricLabelOperation, operation),
attribute.Int(metricLabelCode, code),
attribute.String(metricLabelReason, reason),
),
)
}
if op.seconds != nil {
op.seconds.Record(
ctx, time.Since(startTime).Seconds(),
metric.WithAttributes(
attribute.String(metricLabelKind, kind),
attribute.String(metricLabelOperation, operation),
),
)
}
return reply, err
}
}
}
// Client is middleware client-side metrics.
func Client(opts ...Option) middleware.Middleware {
op := options{}
for _, o := range opts {
o(&op)
}
return func(handler middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var (
code int
reason string
kind string
operation string
)
// default code
code = status.FromGRPCCode(codes.OK)
startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind().String()
operation = info.Operation()
}
reply, err := handler(ctx, req)
if se := errors.FromError(err); se != nil {
code = int(se.Code)
reason = se.Reason
}
if op.requests != nil {
op.requests.Add(
ctx, 1,
metric.WithAttributes(
attribute.String(metricLabelKind, kind),
attribute.String(metricLabelOperation, operation),
attribute.Int(metricLabelCode, code),
attribute.String(metricLabelReason, reason),
),
)
}
if op.seconds != nil {
op.seconds.Record(
ctx, time.Since(startTime).Seconds(),
metric.WithAttributes(
attribute.String(metricLabelKind, kind),
attribute.String(metricLabelOperation, operation),
),
)
}
return reply, err
}
}
}