forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics.go
193 lines (163 loc) · 5.8 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/metric"
"github.com/MetalBlockchain/metalgo/utils/wrappers"
"github.com/MetalBlockchain/metalgo/vms/platformvm/blocks"
)
var _ Metrics = (*metrics)(nil)
type Metrics interface {
metric.APIInterceptor
// Mark that an option vote that we initially preferred was accepted.
MarkOptionVoteWon()
// Mark that an option vote that we initially preferred was rejected.
MarkOptionVoteLost()
// Mark that the given block was accepted.
MarkAccepted(blocks.Block) error
// Mark that a validator set was created.
IncValidatorSetsCreated()
// Mark that a validator set was cached.
IncValidatorSetsCached()
// Mark that we spent the given time computing validator diffs.
AddValidatorSetsDuration(time.Duration)
// Mark that we computed a validator diff at a height with the given
// difference from the top.
AddValidatorSetsHeightDiff(uint64)
// Mark that this much stake is staked on the node.
SetLocalStake(uint64)
// Mark that this much stake is staked in the network.
SetTotalStake(uint64)
// Mark when this node will unstake from the Primary Network.
SetTimeUntilUnstake(time.Duration)
// Mark when this node will unstake from a subnet.
SetTimeUntilSubnetUnstake(subnetID ids.ID, timeUntilUnstake time.Duration)
}
func New(
namespace string,
registerer prometheus.Registerer,
) (Metrics, error) {
blockMetrics, err := newBlockMetrics(namespace, registerer)
m := &metrics{
blockMetrics: blockMetrics,
timeUntilUnstake: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "time_until_unstake",
Help: "Time (in ns) until this node leaves the Primary Network's validator set",
}),
timeUntilSubnetUnstake: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "time_until_unstake_subnet",
Help: "Time (in ns) until this node leaves the subnet's validator set",
},
[]string{"subnetID"},
),
localStake: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "local_staked",
Help: "Amount (in nAVAX) of AVAX staked on this node",
}),
totalStake: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "total_staked",
Help: "Amount (in nAVAX) of AVAX staked on the Primary Network",
}),
numVotesWon: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "votes_won",
Help: "Total number of votes this node has won",
}),
numVotesLost: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "votes_lost",
Help: "Total number of votes this node has lost",
}),
validatorSetsCached: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "validator_sets_cached",
Help: "Total number of validator sets cached",
}),
validatorSetsCreated: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "validator_sets_created",
Help: "Total number of validator sets created from applying difflayers",
}),
validatorSetsHeightDiff: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "validator_sets_height_diff_sum",
Help: "Total number of validator sets diffs applied for generating validator sets",
}),
validatorSetsDuration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "validator_sets_duration_sum",
Help: "Total amount of time generating validator sets in nanoseconds",
}),
}
errs := wrappers.Errs{Err: err}
apiRequestMetrics, err := metric.NewAPIInterceptor(namespace, registerer)
m.APIInterceptor = apiRequestMetrics
errs.Add(
err,
registerer.Register(m.timeUntilUnstake),
registerer.Register(m.timeUntilSubnetUnstake),
registerer.Register(m.localStake),
registerer.Register(m.totalStake),
registerer.Register(m.numVotesWon),
registerer.Register(m.numVotesLost),
registerer.Register(m.validatorSetsCreated),
registerer.Register(m.validatorSetsCached),
registerer.Register(m.validatorSetsHeightDiff),
registerer.Register(m.validatorSetsDuration),
)
return m, errs.Err
}
type metrics struct {
metric.APIInterceptor
blockMetrics *blockMetrics
timeUntilUnstake prometheus.Gauge
timeUntilSubnetUnstake *prometheus.GaugeVec
localStake prometheus.Gauge
totalStake prometheus.Gauge
numVotesWon, numVotesLost prometheus.Counter
validatorSetsCached prometheus.Counter
validatorSetsCreated prometheus.Counter
validatorSetsHeightDiff prometheus.Gauge
validatorSetsDuration prometheus.Gauge
}
func (m *metrics) MarkOptionVoteWon() {
m.numVotesWon.Inc()
}
func (m *metrics) MarkOptionVoteLost() {
m.numVotesLost.Inc()
}
func (m *metrics) MarkAccepted(b blocks.Block) error {
return b.Visit(m.blockMetrics)
}
func (m *metrics) IncValidatorSetsCreated() {
m.validatorSetsCreated.Inc()
}
func (m *metrics) IncValidatorSetsCached() {
m.validatorSetsCached.Inc()
}
func (m *metrics) AddValidatorSetsDuration(d time.Duration) {
m.validatorSetsDuration.Add(float64(d))
}
func (m *metrics) AddValidatorSetsHeightDiff(d uint64) {
m.validatorSetsHeightDiff.Add(float64(d))
}
func (m *metrics) SetLocalStake(s uint64) {
m.localStake.Set(float64(s))
}
func (m *metrics) SetTotalStake(s uint64) {
m.totalStake.Set(float64(s))
}
func (m *metrics) SetTimeUntilUnstake(timeUntilUnstake time.Duration) {
m.timeUntilUnstake.Set(float64(timeUntilUnstake))
}
func (m *metrics) SetTimeUntilSubnetUnstake(subnetID ids.ID, timeUntilUnstake time.Duration) {
m.timeUntilSubnetUnstake.WithLabelValues(subnetID.String()).Set(float64(timeUntilUnstake))
}