forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
92 lines (79 loc) · 2.64 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
package p2p
import (
"fmt"
"reflect"
"regexp"
"sync"
"github.com/cometbft/cometbft/libs/metrics"
)
const (
// MetricsSubsystem is a subsystem shared by all metrics exposed by this
// package.
MetricsSubsystem = "p2p"
)
// valueToLabelRegexp is used to find the golang package name and type name
// so that the name can be turned into a prometheus label where the characters
// in the label do not include prometheus special characters such as '*' and '.'.
var valueToLabelRegexp = regexp.MustCompile(`\*?(\w+)\.(.*)`)
//go:generate go run ../scripts/metricsgen -struct=Metrics
// Metrics contains metrics exposed by this package.
type Metrics struct {
// Number of peers.
Peers metrics.Gauge
// Pending bytes to be sent to a given peer.
PeerPendingSendBytes metrics.Gauge `metrics_labels:"peer_id"`
// Number of bytes of each message type received.
MessageReceiveBytesTotal metrics.Counter `metrics_labels:"message_type"`
// Number of bytes of each message type sent.
MessageSendBytesTotal metrics.Counter `metrics_labels:"message_type"`
// Time in seconds spent sleeping by the receive rate limiter
RecvRateLimiterDelay metrics.Counter `metrics_labels:"peer_id"`
// Time in seconds spent sleeping by the send rate limiter
SendRateLimiterDelay metrics.Counter `metrics_labels:"peer_id"`
}
type peerPendingMetricsCache struct {
mtx sync.Mutex
perMessageCache map[reflect.Type]*peerPendingMetricsCacheEntry
}
type peerPendingMetricsCacheEntry struct {
label string
pendingSendBytes int
pendingRecvBytes int
}
func newPeerPendingMetricsCache() *peerPendingMetricsCache {
return &peerPendingMetricsCache{
perMessageCache: make(map[reflect.Type]*peerPendingMetricsCacheEntry),
}
}
func (c *peerPendingMetricsCache) AddPendingSendBytes(msgType reflect.Type, addBytes int) {
c.mtx.Lock()
defer c.mtx.Unlock()
if entry, ok := c.perMessageCache[msgType]; ok {
entry.pendingSendBytes += addBytes
} else {
c.perMessageCache[msgType] = &peerPendingMetricsCacheEntry{
label: buildLabel(msgType),
pendingSendBytes: addBytes,
}
}
}
func (c *peerPendingMetricsCache) AddPendingRecvBytes(msgType reflect.Type, addBytes int) {
c.mtx.Lock()
defer c.mtx.Unlock()
if entry, ok := c.perMessageCache[msgType]; ok {
entry.pendingRecvBytes += addBytes
} else {
c.perMessageCache[msgType] = &peerPendingMetricsCacheEntry{
label: buildLabel(msgType),
pendingRecvBytes: addBytes,
}
}
}
func buildLabel(msgType reflect.Type) string {
s := msgType.String()
ss := valueToLabelRegexp.FindStringSubmatch(s)
return fmt.Sprintf("%s_%s", ss[1], ss[2])
}
func getMsgType(i any) reflect.Type {
return reflect.TypeOf(i)
}