forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.go
70 lines (64 loc) · 2.02 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bloom
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/MetalBlockchain/metalgo/utils"
)
// Metrics is a collection of commonly useful metrics when using a long-lived
// bloom filter.
type Metrics struct {
Count prometheus.Gauge
NumHashes prometheus.Gauge
NumEntries prometheus.Gauge
MaxCount prometheus.Gauge
ResetCount prometheus.Counter
}
func NewMetrics(
namespace string,
registerer prometheus.Registerer,
) (*Metrics, error) {
m := &Metrics{
Count: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "count",
Help: "Number of additions that have been performed to the bloom",
}),
NumHashes: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "hashes",
Help: "Number of hashes in the bloom",
}),
NumEntries: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "entries",
Help: "Number of bytes allocated to slots in the bloom",
}),
MaxCount: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "max_count",
Help: "Maximum number of additions that should be performed to the bloom before resetting",
}),
ResetCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "reset_count",
Help: "Number times the bloom has been reset",
}),
}
err := utils.Err(
registerer.Register(m.Count),
registerer.Register(m.NumHashes),
registerer.Register(m.NumEntries),
registerer.Register(m.MaxCount),
registerer.Register(m.ResetCount),
)
return m, err
}
// Reset the metrics to align with the provided bloom filter and max count.
func (m *Metrics) Reset(newFilter *Filter, maxCount int) {
m.Count.Set(float64(newFilter.Count()))
m.NumHashes.Set(float64(len(newFilter.hashSeeds)))
m.NumEntries.Set(float64(len(newFilter.entries)))
m.MaxCount.Set(float64(maxCount))
m.ResetCount.Inc()
}