-
Notifications
You must be signed in to change notification settings - Fork 376
/
collector_host.go
94 lines (79 loc) · 2.64 KB
/
collector_host.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
package ipfsutil
import (
host "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
ma "github.com/multiformats/go-multiaddr"
prometheus "github.com/prometheus/client_golang/prometheus"
)
var (
protocolsStreamsSumDesc = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "host", "open_stream"),
"number of open stream for this protocol",
[]string{"protocol_id"}, nil,
)
connsSumOpts = prometheus.GaugeOpts{
Name: prometheus.BuildFQName("ipfs", "host", "open_connection"),
Help: "number of opened connections",
}
// protocolStreamDurationOpts = prometheus.HistogramOpts{
// Name: prometheus.BuildFQName("ipfs", "host", "stream_duration"),
// Help: "stream duration",
// Buckets: prometheus.LinearBuckets(0, 10, 6),
// }
)
const UnknownProtocol = "UnknownProtocol"
type HostCollector struct {
host host.Host
connsCollector prometheus.Gauge
// streamsCollector *prometheus.HistogramVec
}
func NewHostCollector(h host.Host) *HostCollector {
gconns := prometheus.NewGauge(connsSumOpts)
// hstreams := prometheus.NewHistogramVec(protocolStreamDurationOpts, []string{"protocol_id"})
cc := &HostCollector{
host: h,
connsCollector: gconns,
// streamsCollector: hstreams,
}
h.Network().Notify(cc)
return cc
}
func (cc *HostCollector) Collect(cmetric chan<- prometheus.Metric) {
cc.connsCollector.Collect(cmetric)
// cc.streamsCollector.Collect(cmetric)
streamsMap := make(map[protocol.ID]int)
for _, c := range cc.host.Network().Conns() {
for _, s := range c.GetStreams() {
if s.Protocol() != "" {
streamsMap[s.Protocol()]++
} else {
streamsMap[UnknownProtocol]++
}
}
}
for p, ns := range streamsMap {
cmetric <- prometheus.MustNewConstMetric(
protocolsStreamsSumDesc,
prometheus.GaugeValue,
float64(ns), string(p))
}
}
func (cc *HostCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- protocolsStreamsSumDesc
cc.connsCollector.Describe(ch)
// cc.streamsCollector.Describe(ch)
}
func (cc *HostCollector) Listen(network.Network, ma.Multiaddr) {}
func (cc *HostCollector) ListenClose(network.Network, ma.Multiaddr) {}
func (cc *HostCollector) Connected(n network.Network, c network.Conn) {
cc.connsCollector.Inc()
}
func (cc *HostCollector) Disconnected(n network.Network, c network.Conn) {
cc.connsCollector.Dec()
}
func (cc *HostCollector) OpenedStream(n network.Network, s network.Stream) {}
func (cc *HostCollector) ClosedStream(n network.Network, s network.Stream) {
// elpased := time.Since(s.Stat().Opened)
// cc.streamsCollector.WithLabelValues(string(s.Protocol())).Observe(elpased.Seconds())
}