-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
147 lines (131 loc) · 5.09 KB
/
prometheus.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
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
package bittorrent
import (
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
)
type Gauge struct {
Value uint64
Description *prometheus.Desc
Labels []*dto.LabelPair
}
func (gauge Gauge) Desc() *prometheus.Desc { return gauge.Description }
func (gauge Gauge) Write(m *dto.Metric) error {
m.Label = gauge.Labels
m.Gauge = &dto.Gauge{Value: proto.Float64(float64(gauge.Value))}
return nil
}
type Counter struct {
Value uint64
Description *prometheus.Desc
Labels []*dto.LabelPair
}
func (counter Counter) Desc() *prometheus.Desc { return counter.Description }
func (counter Counter) Write(m *dto.Metric) error {
m.Label = counter.Labels
m.Counter = &dto.Counter{Value: proto.Float64(float64(counter.Value))}
return nil
}
func (sess *Session) Describe(chan<- *prometheus.Desc) {
// Sending no descriptor at all marks the Collector as “unchecked”,
// i.e. no checks will be performed at registration time, and the
// Collector may yield any Metric it sees fit in its Collect method.
}
var (
promPeersConnectedDesc = prometheus.NewDesc("bittorrent_peers_connected", "", nil, nil)
promUploadedRawDesc = prometheus.NewDesc("bittorrent_uploaded_raw_bytes", "", nil, nil)
promDownloadedRawDesc = prometheus.NewDesc("bittorrent_downloaded_raw_bytes", "", nil, nil)
promPeersRejectedDesc = prometheus.NewDesc(
"bittorrent_peers_rejected_total",
"The total number of incoming peer connections that have been rejected",
[]string{"reason"},
nil,
)
promTorrentsDesc = prometheus.NewDesc(
"bittorrent_torrents",
"The number of registered torrents, grouped by status",
[]string{"status"},
nil,
)
)
var (
promPeersRejectedSessionLimitLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"session_limit"})
promPeersRejectedPeerIncomingCallbackLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"peer_incoming_callback"})
promPeersRejectedShutdownLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"shutdown"})
promPeersRejectedPeerHandshakeInfoHashCallbackLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"peer_handshake_info_hash_callback"})
promPeersRejectedUnknownTorrentLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"unknown_torrent"})
promPeersRejectedStoppedTorrentLabel = prometheus.MakeLabelPairs(promPeersRejectedDesc, []string{"stopped_torrent"})
promTorrentsStoppedLabel = prometheus.MakeLabelPairs(promTorrentsDesc, []string{"stopped"})
promTorrentsLeechingLabel = prometheus.MakeLabelPairs(promTorrentsDesc, []string{"leeching"})
promTorrentsSeedingLabel = prometheus.MakeLabelPairs(promTorrentsDesc, []string{"seeding"})
promTorrentsActionLabel = prometheus.MakeLabelPairs(promTorrentsDesc, []string{"action"})
)
func (sess *Session) Collect(ch chan<- prometheus.Metric) {
// bittorrent_peers_connected
ch <- Gauge{
Value: sess.statistics.numConnectedPeers.Load(),
Description: promPeersConnectedDesc,
}
// bittorrent_uploaded_raw_bytes
ch <- Counter{
Value: sess.statistics.uploadedRaw.Load(),
Description: promUploadedRawDesc,
}
// bittorrent_downloaded_raw_bytes
ch <- Counter{
Value: sess.statistics.downloadedRaw.Load(),
Description: promDownloadedRawDesc,
}
// bittorrent_peers_rejected_total
ch <- Counter{
Value: sess.statistics.numRejectedPeers.peerIncomingCallback.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedPeerIncomingCallbackLabel,
}
ch <- Counter{
Value: sess.statistics.numRejectedPeers.peerHandshakeInfoHashCallback.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedPeerHandshakeInfoHashCallbackLabel,
}
ch <- Counter{
Value: sess.statistics.numRejectedPeers.sessionLimit.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedSessionLimitLabel,
}
ch <- Counter{
Value: sess.statistics.numRejectedPeers.shutdown.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedShutdownLabel,
}
ch <- Counter{
Value: sess.statistics.numRejectedPeers.unknownTorrent.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedUnknownTorrentLabel,
}
ch <- Counter{
Value: sess.statistics.numRejectedPeers.stoppedTorrent.Load(),
Description: promPeersRejectedDesc,
Labels: promPeersRejectedStoppedTorrentLabel,
}
// bittorrent_torrents
ch <- Gauge{
Value: sess.statistics.numTorrents.stopped.Load(),
Description: promTorrentsDesc,
Labels: promTorrentsStoppedLabel,
}
ch <- Gauge{
Value: sess.statistics.numTorrents.leeching.Load(),
Description: promTorrentsDesc,
Labels: promTorrentsLeechingLabel,
}
ch <- Gauge{
Value: sess.statistics.numTorrents.seeding.Load(),
Description: promTorrentsDesc,
Labels: promTorrentsSeedingLabel,
}
ch <- Gauge{
Value: sess.statistics.numTorrents.action.Load(),
Description: promTorrentsDesc,
Labels: promTorrentsActionLabel,
}
}