forked from celestiaorg/go-fraud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
48 lines (43 loc) · 1.14 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
package fraud
import (
"context"
"github.com/ipfs/go-datastore"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
)
var meter = global.MeterProvider().Meter("fraud")
// WithMetrics enables metrics to monitor fraud proofs.
func WithMetrics(store Getter) {
proofTypes := registeredProofTypes()
for _, proofType := range proofTypes {
counter, _ := meter.AsyncInt64().Gauge(string(proofType),
instrument.WithUnit(unit.Dimensionless),
instrument.WithDescription("Stored fraud proof"),
)
err := meter.RegisterCallback(
[]instrument.Asynchronous{
counter,
},
func(ctx context.Context) {
proofs, err := store.Get(ctx, proofType)
switch err {
case nil:
counter.Observe(ctx,
int64(len(proofs)),
attribute.String("proof_type", string(proofType)),
)
case datastore.ErrNotFound:
counter.Observe(ctx, 0, attribute.String("err", "not_found"))
return
default:
counter.Observe(ctx, 0, attribute.String("err", "unknown"))
}
},
)
if err != nil {
panic(err)
}
}
}