/
netmetrics.go
88 lines (74 loc) · 2.75 KB
/
netmetrics.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
package p2p
import (
"math"
"regexp"
"strconv"
gossipv1 "github.com/alephium/wormhole-fork/node/pkg/proto/gossip/v1"
"github.com/alephium/wormhole-fork/node/pkg/vaa"
"github.com/alephium/wormhole-fork/node/pkg/version"
"github.com/ethereum/go-ethereum/common"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
wormholeNetworkNodeHeight = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wormhole_network_node_height",
Help: "Network height of the given guardian node per network",
}, []string{"guardian_addr", "node_id", "node_name", "network"})
wormholeNetworkNodeErrors = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wormhole_network_node_errors_count",
Help: "Number of errors the given guardian node encountered per network",
}, []string{"guardian_addr", "node_id", "node_name", "network"})
wormholeNetworkVersion = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "wormhole_network_node_version",
Help: "Network version of the given guardian node per network",
}, []string{"guardian_addr", "node_id", "node_name", "network", "version"})
)
func collectNodeMetrics(addr common.Address, peerId peer.ID, hb *gossipv1.Heartbeat) {
for _, n := range hb.Networks {
if n == nil {
continue
}
chain := vaa.ChainID(n.Id)
wormholeNetworkNodeHeight.WithLabelValues(
addr.Hex(), peerId.Pretty(), hb.NodeName, chain.String()).Set(float64(n.Height))
wormholeNetworkNodeErrors.WithLabelValues(
addr.Hex(), peerId.Pretty(), hb.NodeName, chain.String()).Set(float64(n.ErrorCount))
wormholeNetworkVersion.WithLabelValues(
addr.Hex(), peerId.Pretty(), hb.NodeName, chain.String(),
sanitizeVersion(hb.Version, version.Version())).Set(1)
}
}
var (
// Parse version string using regular expression.
// The version string should be in the format of "vX.Y.Z"
// where X, Y and Z are integers. Suffixes are ignored.
reVersion = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)`)
)
// sanitizeVersion cleans up the version string to prevent an attacker from executing a cardinality attack.
func sanitizeVersion(version string, reference string) string {
// Match groups of reVersion
components := reVersion.FindStringSubmatch(version)
referenceComponents := reVersion.FindStringSubmatch(reference)
// Compare components of the version string with the reference and ensure
// that the distance is less than 5.
for i, c := range components {
if len(referenceComponents) <= i {
return "other"
}
cInt, _ := strconv.Atoi(c)
cRefInt, _ := strconv.Atoi(referenceComponents[i])
if math.Abs(float64(cInt-cRefInt)) > 5 {
return "other"
}
}
v := reVersion.FindString(version)
if v == "" {
return "other"
}
return v
}