-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathmetrics.go
More file actions
191 lines (167 loc) · 4.85 KB
/
metrics.go
File metadata and controls
191 lines (167 loc) · 4.85 KB
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
package clientinfo
import (
"fmt"
"strings"
"time"
"github.com/keep-network/keep-common/pkg/clientinfo"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/net"
)
type Source func() float64
// Names under which metrics are exposed.
//
// NOTE: ConnectedWellknownPeersCountMetricName was renamed from
// "connected_bootstrap_count" in v2.6.0. Update any Prometheus queries or
// Grafana dashboards that reference the old name.
const (
ConnectedPeersCountMetricName = "connected_peers_count"
ConnectedWellknownPeersCountMetricName = "connected_wellknown_peers_count"
EthConnectivityMetricName = "eth_connectivity"
BtcConnectivityMetricName = "btc_connectivity"
ClientInfoMetricName = "client_info"
)
const (
// DefaultNetworkMetricsTick is the default duration of the
// observation tick for network metrics.
DefaultNetworkMetricsTick = 1 * time.Minute
// DefaultEthereumMetricsTick is the default duration of the
// observation tick for Ethereum metrics.
DefaultEthereumMetricsTick = 10 * time.Minute
// DefaultBitcoinMetricsTick is the default duration of the
// observation tick for Bitcoin metrics.
DefaultBitcoinMetricsTick = 10 * time.Minute
// The duration of the observation tick for all application-specific
// metrics.
ApplicationMetricsTick = 1 * time.Minute
)
// ObserveConnectedPeersCount triggers an observation process of the
// connected_peers_count metric.
func (r *Registry) ObserveConnectedPeersCount(
netProvider net.Provider,
tick time.Duration,
) {
input := func() float64 {
connectedPeers := netProvider.ConnectionManager().ConnectedPeers()
return float64(len(connectedPeers))
}
r.observe(
ConnectedPeersCountMetricName,
input,
validateTick(tick, DefaultNetworkMetricsTick),
)
}
// ObserveConnectedWellknownPeersCount triggers an observation process of the
// connected_wellknown_peers_count metric.
func (r *Registry) ObserveConnectedWellknownPeersCount(
netProvider net.Provider,
wellknownPeers []string,
tick time.Duration,
) {
input := func() float64 {
currentCount := 0
for _, address := range wellknownPeers {
if netProvider.ConnectionManager().IsConnected(address) {
currentCount++
}
}
return float64(currentCount)
}
r.observe(
ConnectedWellknownPeersCountMetricName,
input,
validateTick(tick, DefaultNetworkMetricsTick),
)
}
// ObserveEthConnectivity triggers an observation process of the
// eth_connectivity metric.
func (r *Registry) ObserveEthConnectivity(
blockCounter chain.BlockCounter,
tick time.Duration,
) {
input := func() float64 {
_, err := blockCounter.CurrentBlock()
if err != nil {
return 0
}
return 1
}
r.observe(
EthConnectivityMetricName,
input,
validateTick(tick, DefaultEthereumMetricsTick),
)
}
// ObserveBtcConnectivity triggers an observation process of the
// btc_connectivity metric.
func (r *Registry) ObserveBtcConnectivity(
btcChain bitcoin.Chain,
tick time.Duration,
) {
input := func() float64 {
_, err := btcChain.GetLatestBlockHeight()
if err != nil {
return 0
}
return 1
}
r.observe(
BtcConnectivityMetricName,
input,
validateTick(tick, DefaultBitcoinMetricsTick),
)
}
// ObserveApplicationSource triggers an observation process of
// application-specific metrics.
func (r *Registry) ObserveApplicationSource(
application string,
inputs map[string]Source,
) {
for k, v := range inputs {
r.observe(
fmt.Sprintf("%s_%s", application, k),
v,
ApplicationMetricsTick,
)
}
}
// RegisterMetricClientInfo registers static client information labels for metrics.
func (r *Registry) RegisterMetricClientInfo(version string) {
_, err := r.NewMetricInfo(
ClientInfoMetricName,
[]clientinfo.Label{
clientinfo.NewLabel("version", version),
},
)
if err != nil {
logger.Warnf("could not register metric client info: [%v]", err)
}
}
func (r *Registry) observe(
name string,
input Source,
tick time.Duration,
) {
observer, err := r.NewMetricGaugeObserver(name, clientinfo.MetricObserverInput(input))
if err != nil {
// Check if the error is due to metric already existing (expected in some cases)
errStr := err.Error()
if strings.Contains(errStr, "already exists") {
// Metric already registered, this is expected if registerAllMetrics is called multiple times
// or if the same metric is registered in multiple places. Log at debug level.
logger.Debugf("metric [%v] already registered, skipping duplicate registration: %v", name, err)
return
}
// For other errors, log as warning
logger.Warnf("could not create gauge observer [%v]: %v", name, err)
return
}
observer.Observe(r.ctx, tick)
logger.Infof("observing %s with [%s] tick", name, tick)
}
func validateTick(tick time.Duration, defaultTick time.Duration) time.Duration {
if tick > 0 {
return tick
}
return defaultTick
}