-
Notifications
You must be signed in to change notification settings - Fork 885
/
metrics.go
286 lines (233 loc) · 7.36 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package eds
import (
"context"
"errors"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/celestiaorg/celestia-node/libs/utils"
)
const (
failedKey = "failed"
sizeKey = "eds_size"
putResultKey = "result"
putOK putResult = "ok"
putExists putResult = "exists"
putFailed putResult = "failed"
opNameKey = "op"
longOpResultKey = "result"
longOpUnresolved longOpResult = "unresolved"
longOpOK longOpResult = "ok"
longOpFailed longOpResult = "failed"
dagstoreShardStatusKey = "shard_status"
)
var meter = otel.Meter("eds_store")
type putResult string
type longOpResult string
type metrics struct {
putTime metric.Float64Histogram
getCARTime metric.Float64Histogram
getCARBlockstoreTime metric.Float64Histogram
getDAHTime metric.Float64Histogram
removeTime metric.Float64Histogram
getTime metric.Float64Histogram
hasTime metric.Float64Histogram
listTime metric.Float64Histogram
shardFailureCount metric.Int64Counter
longOpTime metric.Float64Histogram
gcTime metric.Float64Histogram
clientReg metric.Registration
closerFn func() error
}
func (s *Store) WithMetrics() error {
putTime, err := meter.Float64Histogram("eds_store_put_time_histogram",
metric.WithDescription("eds store put time histogram(s)"))
if err != nil {
return err
}
getCARTime, err := meter.Float64Histogram("eds_store_get_car_time_histogram",
metric.WithDescription("eds store get car time histogram(s)"))
if err != nil {
return err
}
getCARBlockstoreTime, err := meter.Float64Histogram("eds_store_get_car_blockstore_time_histogram",
metric.WithDescription("eds store get car blockstore time histogram(s)"))
if err != nil {
return err
}
getDAHTime, err := meter.Float64Histogram("eds_store_get_dah_time_histogram",
metric.WithDescription("eds store get dah time histogram(s)"))
if err != nil {
return err
}
removeTime, err := meter.Float64Histogram("eds_store_remove_time_histogram",
metric.WithDescription("eds store remove time histogram(s)"))
if err != nil {
return err
}
getTime, err := meter.Float64Histogram("eds_store_get_time_histogram",
metric.WithDescription("eds store get time histogram(s)"))
if err != nil {
return err
}
hasTime, err := meter.Float64Histogram("eds_store_has_time_histogram",
metric.WithDescription("eds store has time histogram(s)"))
if err != nil {
return err
}
listTime, err := meter.Float64Histogram("eds_store_list_time_histogram",
metric.WithDescription("eds store list time histogram(s)"))
if err != nil {
return err
}
shardFailureCount, err := meter.Int64Counter("eds_store_shard_failure_counter",
metric.WithDescription("eds store OpShardFail counter"))
if err != nil {
return err
}
longOpTime, err := meter.Float64Histogram("eds_store_long_operation_time_histogram",
metric.WithDescription("eds store long operation time histogram(s)"))
if err != nil {
return err
}
gcTime, err := meter.Float64Histogram("eds_store_gc_time",
metric.WithDescription("dagstore gc time histogram(s)"))
if err != nil {
return err
}
dagStoreShards, err := meter.Int64ObservableGauge("eds_store_dagstore_shards",
metric.WithDescription("dagstore amount of shards by status"))
if err != nil {
return err
}
closerFn, err := s.cache.Load().EnableMetrics()
if err != nil {
return err
}
callback := func(_ context.Context, observer metric.Observer) error {
stats := s.dgstr.Stats()
for status, amount := range stats {
observer.ObserveInt64(dagStoreShards, int64(amount),
metric.WithAttributes(
attribute.String(dagstoreShardStatusKey, status.String()),
))
}
return nil
}
clientReg, err := meter.RegisterCallback(callback, dagStoreShards)
if err != nil {
return err
}
s.metrics = &metrics{
putTime: putTime,
getCARTime: getCARTime,
getCARBlockstoreTime: getCARBlockstoreTime,
getDAHTime: getDAHTime,
removeTime: removeTime,
getTime: getTime,
hasTime: hasTime,
listTime: listTime,
shardFailureCount: shardFailureCount,
longOpTime: longOpTime,
gcTime: gcTime,
clientReg: clientReg,
closerFn: closerFn,
}
return nil
}
func (m *metrics) close() error {
if m == nil {
return nil
}
return errors.Join(m.closerFn(), m.clientReg.Unregister())
}
func (m *metrics) observeGCtime(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.gcTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeShardFailure(ctx context.Context, shardKey string) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.shardFailureCount.Add(ctx, 1, metric.WithAttributes(attribute.String("shard_key", shardKey)))
}
func (m *metrics) observePut(ctx context.Context, dur time.Duration, result putResult, size uint) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.putTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.String(putResultKey, string(result)),
attribute.Int(sizeKey, int(size))))
}
func (m *metrics) observeLongOp(ctx context.Context, opName string, dur time.Duration, result longOpResult) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.longOpTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.String(opNameKey, opName),
attribute.String(longOpResultKey, string(result))))
}
func (m *metrics) observeGetCAR(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.getCARTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeCARBlockstore(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.getCARBlockstoreTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeGetDAH(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.getDAHTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeRemove(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.removeTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeGet(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.getTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeHas(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.hasTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}
func (m *metrics) observeList(ctx context.Context, dur time.Duration, failed bool) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.listTime.Record(ctx, dur.Seconds(), metric.WithAttributes(
attribute.Bool(failedKey, failed)))
}