-
Notifications
You must be signed in to change notification settings - Fork 211
/
prometheus.go
136 lines (120 loc) · 3.92 KB
/
prometheus.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
package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/fixed"
"go.uber.org/zap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/metrics"
)
const (
subsystem = "beacons"
labelEpoch = "epoch"
labelBeacon = "beacon"
)
// BeaconStats hold metadata for each beacon value.
type BeaconStats struct {
Epoch types.EpochID
Beacon string
Weight fixed.Fixed
WeightUnit int
}
// GatherCB returns stats for the observed and calculated beacons.
type GatherCB func() ([]*BeaconStats, *BeaconStats)
// BeaconMetricsCollector is a prometheus Collector for beacon metrics.
type BeaconMetricsCollector struct {
gather GatherCB
logger *zap.Logger
registry *prometheus.Registry
observedBeaconCount *prometheus.Desc
observedBeaconWeight *prometheus.Desc
calculatedBeaconWeight *prometheus.Desc
}
var nameCalculatedWeight = prometheus.BuildFQName(metrics.Namespace, subsystem, "beacon_calculated_weight")
func MetricNameCalculatedWeight() string {
return nameCalculatedWeight
}
// NewBeaconMetricsCollector creates a prometheus Collector for beacons.
func NewBeaconMetricsCollector(cb GatherCB, logger *zap.Logger) *BeaconMetricsCollector {
bmc := &BeaconMetricsCollector{
gather: cb,
logger: logger,
observedBeaconCount: prometheus.NewDesc(
prometheus.BuildFQName(metrics.Namespace, subsystem, "beacon_observed_total"),
"Number of beacons collected from blocks for each epoch and value",
[]string{labelEpoch, labelBeacon}, nil),
observedBeaconWeight: prometheus.NewDesc(
prometheus.BuildFQName(metrics.Namespace, subsystem, "beacon_observed_weight"),
"Weight of beacons collected from blocks for each epoch and value",
[]string{labelEpoch, labelBeacon}, nil),
calculatedBeaconWeight: prometheus.NewDesc(
nameCalculatedWeight,
"Weight of the beacon calculated by the node for each epoch",
[]string{labelEpoch, labelBeacon}, nil),
}
return bmc
}
// Start registers the Collector with specified prometheus registry and starts the metrics collection.
func (bmc *BeaconMetricsCollector) Start(registry *prometheus.Registry) {
if registry != nil {
registry.MustRegister(bmc)
} else {
// use Register instead of MustRegister because during app test, multiple instances
// will register the same set of metrics with the default registry and panic
if err := prometheus.Register(bmc); err != nil {
bmc.logger.Error("failed to register beacon metrics Collector", zap.Error(err))
}
}
bmc.registry = registry
}
// Stop unregisters the Collector with specified prometheus registry and stops the metrics collection.
func (bmc *BeaconMetricsCollector) Stop() {
if bmc.registry != nil {
bmc.registry.Unregister(bmc)
} else {
prometheus.Unregister(bmc)
}
}
// Describe implements Collector.
func (bmc *BeaconMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- bmc.observedBeaconCount
ch <- bmc.observedBeaconWeight
ch <- bmc.calculatedBeaconWeight
}
// Collect implements Collector.
func (bmc *BeaconMetricsCollector) Collect(ch chan<- prometheus.Metric) {
observed, calculated := bmc.gather()
for _, ob := range observed {
epochStr := ob.Epoch.String()
ch <- prometheus.MustNewConstMetric(
bmc.observedBeaconCount,
prometheus.CounterValue,
float64(ob.WeightUnit),
epochStr,
ob.Beacon,
)
ch <- prometheus.MustNewConstMetric(
bmc.observedBeaconWeight,
prometheus.CounterValue,
ob.Weight.Float(),
epochStr,
ob.Beacon,
)
}
if calculated == nil {
return
}
// export the calculated beacon for the target epoch for ease of monitoring along with the observed beacons
ch <- prometheus.MustNewConstMetric(
bmc.calculatedBeaconWeight,
prometheus.CounterValue,
float64(0),
calculated.Epoch.String(),
calculated.Beacon,
)
}
var NumMaliciousProps = metrics.NewCounter(
"malicious_proposals",
subsystem,
"number of malicious proposals",
[]string{},
).WithLabelValues()