-
Notifications
You must be signed in to change notification settings - Fork 923
/
metrics.go
193 lines (160 loc) · 4.67 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
package das
import (
"context"
"fmt"
"sync/atomic"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/libs/utils"
)
const (
jobTypeLabel = "job_type"
headerWidthLabel = "header_width"
failedLabel = "failed"
)
var meter = otel.Meter("das")
type metrics struct {
sampled metric.Int64Counter
sampleTime metric.Float64Histogram
getHeaderTime metric.Float64Histogram
newHead metric.Int64Counter
lastSampledTS uint64
clientReg metric.Registration
}
func (d *DASer) InitMetrics() error {
sampled, err := meter.Int64Counter("das_sampled_headers_counter",
metric.WithDescription("sampled headers counter"))
if err != nil {
return err
}
sampleTime, err := meter.Float64Histogram("das_sample_time_hist",
metric.WithDescription("duration of sampling a single header"))
if err != nil {
return err
}
getHeaderTime, err := meter.Float64Histogram("das_get_header_time_hist",
metric.WithDescription("duration of getting header from header store"))
if err != nil {
return err
}
newHead, err := meter.Int64Counter("das_head_updated_counter",
metric.WithDescription("amount of times DAS'er advanced network head"))
if err != nil {
return err
}
lastSampledTS, err := meter.Int64ObservableGauge("das_latest_sampled_ts",
metric.WithDescription("latest sampled timestamp"))
if err != nil {
return err
}
busyWorkers, err := meter.Int64ObservableGauge("das_busy_workers_amount",
metric.WithDescription("number of active parallel workers in DAS'er"))
if err != nil {
return err
}
networkHead, err := meter.Int64ObservableGauge("das_network_head",
metric.WithDescription("most recent network head"))
if err != nil {
return err
}
sampledChainHead, err := meter.Int64ObservableGauge("das_sampled_chain_head",
metric.WithDescription("height of the sampled chain - all previous headers have been successfully sampled"))
if err != nil {
return err
}
totalSampled, err := meter.Int64ObservableGauge("das_total_sampled_headers",
metric.WithDescription("total sampled headers gauge"),
)
if err != nil {
return err
}
d.sampler.metrics = &metrics{
sampled: sampled,
sampleTime: sampleTime,
getHeaderTime: getHeaderTime,
newHead: newHead,
}
callback := func(ctx context.Context, observer metric.Observer) error {
stats, err := d.sampler.stats(ctx)
if err != nil {
log.Errorf("observing stats: %s", err.Error())
return err
}
for jobType, amount := range stats.workersByJobType() {
observer.ObserveInt64(busyWorkers, amount,
metric.WithAttributes(
attribute.String(jobTypeLabel, string(jobType)),
))
}
observer.ObserveInt64(networkHead, int64(stats.NetworkHead))
observer.ObserveInt64(sampledChainHead, int64(stats.SampledChainHead))
if ts := atomic.LoadUint64(&d.sampler.metrics.lastSampledTS); ts != 0 {
observer.ObserveInt64(lastSampledTS, int64(ts))
}
observer.ObserveInt64(totalSampled, int64(stats.totalSampled()))
return nil
}
d.sampler.metrics.clientReg, err = meter.RegisterCallback(callback,
lastSampledTS,
busyWorkers,
networkHead,
sampledChainHead,
totalSampled,
)
if err != nil {
return fmt.Errorf("registering metrics callback: %w", err)
}
return nil
}
func (m *metrics) close() error {
if m == nil {
return nil
}
return m.clientReg.Unregister()
}
// observeSample records the time it took to sample a header +
// the amount of sampled contiguous headers
func (m *metrics) observeSample(
ctx context.Context,
h *header.ExtendedHeader,
sampleTime time.Duration,
jobType jobType,
err error,
) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.sampleTime.Record(ctx, sampleTime.Seconds(),
metric.WithAttributes(
attribute.Bool(failedLabel, err != nil),
attribute.Int(headerWidthLabel, len(h.DAH.RowRoots)),
attribute.String(jobTypeLabel, string(jobType)),
))
m.sampled.Add(ctx, 1,
metric.WithAttributes(
attribute.Bool(failedLabel, err != nil),
attribute.Int(headerWidthLabel, len(h.DAH.RowRoots)),
attribute.String(jobTypeLabel, string(jobType)),
))
atomic.StoreUint64(&m.lastSampledTS, uint64(time.Now().UTC().Unix()))
}
// observeGetHeader records the time it took to get a header from the header store.
func (m *metrics) observeGetHeader(ctx context.Context, d time.Duration) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.getHeaderTime.Record(ctx, d.Seconds())
}
// observeNewHead records the network head.
func (m *metrics) observeNewHead(ctx context.Context) {
if m == nil {
return
}
ctx = utils.ResetContextOnError(ctx)
m.newHead.Add(ctx, 1)
}