-
Notifications
You must be signed in to change notification settings - Fork 885
/
metrics.go
42 lines (35 loc) · 1.01 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
package state
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
)
var meter = otel.Meter("state")
func WithMetrics(lc fx.Lifecycle, ca *CoreAccessor) {
pfbCounter, _ := meter.Int64ObservableCounter(
"pfb_count",
metric.WithDescription("Total count of submitted PayForBlob transactions"),
)
lastPfbTimestamp, _ := meter.Int64ObservableCounter(
"last_pfb_timestamp",
metric.WithDescription("Timestamp of the last submitted PayForBlob transaction"),
)
callback := func(_ context.Context, observer metric.Observer) error {
observer.ObserveInt64(pfbCounter, ca.PayForBlobCount())
observer.ObserveInt64(lastPfbTimestamp, ca.LastPayForBlob())
return nil
}
clientReg, err := meter.RegisterCallback(callback, pfbCounter, lastPfbTimestamp)
if err != nil {
panic(err)
}
lc.Append(fx.Hook{
OnStop: func(context.Context) error {
if err := clientReg.Unregister(); err != nil {
log.Warnw("failed to close metrics", "err", err)
}
return nil
},
})
}