-
Notifications
You must be signed in to change notification settings - Fork 376
/
collector_bandwidth.go
52 lines (43 loc) · 1.28 KB
/
collector_bandwidth.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
package ipfsutil
import (
metrics "github.com/libp2p/go-libp2p-core/metrics"
prometheus "github.com/prometheus/client_golang/prometheus"
)
var (
protocolsBandwidthInDesc = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "bandwidth", "in"),
"protocol bandwidth in",
[]string{"protocol_id"}, nil,
)
protocolsBandwidthOutDesc = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "bandwidth", "out"),
"protocol bandwidth out",
[]string{"protocol_id"}, nil,
)
)
// BandwidthCollector is a prometheus.Collector
var _ prometheus.Collector = (*BandwidthCollector)(nil)
type BandwidthCollector struct {
reporter *metrics.BandwidthCounter
}
func NewBandwidthCollector(reporter *metrics.BandwidthCounter) *BandwidthCollector {
return &BandwidthCollector{reporter}
}
func (bc *BandwidthCollector) Collect(cmetric chan<- prometheus.Metric) {
for p, s := range bc.reporter.GetBandwidthByProtocol() {
if p == "" {
continue
}
cmetric <- prometheus.MustNewConstMetric(
protocolsBandwidthInDesc,
prometheus.GaugeValue,
s.RateIn, string(p))
cmetric <- prometheus.MustNewConstMetric(
protocolsBandwidthOutDesc,
prometheus.GaugeValue,
s.RateOut, string(p))
}
}
func (bc *BandwidthCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(bc, ch)
}