-
Notifications
You must be signed in to change notification settings - Fork 137
/
streaming.go
275 lines (248 loc) · 8.51 KB
/
streaming.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package collector
import (
"net/http"
"strconv"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// newStreamingCollector collects channelsz and serversz metrics of
// streaming servers.
func newStreamingCollector(endpoint string, servers []*CollectedServer) prometheus.Collector {
switch endpoint {
case "channelsz":
return newChannelsCollector(servers)
case "serverz":
return newServerzCollector(servers)
}
return nil
}
func isStreamingEndpoint(endpoint string) bool {
return endpoint == "channelsz" || endpoint == "serverz"
}
type serverzCollector struct {
sync.Mutex
httpClient *http.Client
servers []*CollectedServer
bytesTotal *prometheus.Desc
msgsTotal *prometheus.Desc
channels *prometheus.Desc
subs *prometheus.Desc
clients *prometheus.Desc
info *prometheus.Desc
}
func newServerzCollector(servers []*CollectedServer) prometheus.Collector {
nc := &serverzCollector{
httpClient: http.DefaultClient,
bytesTotal: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "bytes_total"),
"Total of bytes",
[]string{"server_id"},
nil,
),
msgsTotal: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "msgs_total"),
"Total of messages",
[]string{"server_id"},
nil,
),
channels: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "channels"),
"Total channels",
[]string{"server_id"},
nil,
),
subs: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "subscriptions"),
"Total subscriptions",
[]string{"server_id"},
nil,
),
clients: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "clients"),
"Total clients",
[]string{"server_id"},
nil,
),
info: prometheus.NewDesc(
prometheus.BuildFQName("nss", "server", "info"),
"Info",
[]string{"server_id", "cluster_id", "version", "go_version", "state", "role", "start_time"},
nil,
),
}
nc.servers = make([]*CollectedServer, len(servers))
for i, s := range servers {
nc.servers[i] = &CollectedServer{
ID: s.ID,
URL: s.URL + "/streaming/serverz",
}
}
return nc
}
func (nc *serverzCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nc.bytesTotal
ch <- nc.msgsTotal
ch <- nc.channels
ch <- nc.subs
ch <- nc.clients
ch <- nc.info
}
// StreamingServerz represents the metrics from streaming/serverz.
type StreamingServerz struct {
TotalBytes int `json:"total_bytes"`
TotalMsgs int `json:"total_msgs"`
Channels int `json:"channels"`
Subscriptions int `json:"subscriptions"`
Clients int `json:"clients"`
ClusterID string `json:"cluster_id"`
ServerID string `json:"server_id"`
Version string `json:"version"`
GoVersion string `json:"go"`
State string `json:"state"`
Role string `json:"role"`
StartTime string `json:"start_time"`
}
// Collect gathers the streaming server serverz metrics.
func (nc *serverzCollector) Collect(ch chan<- prometheus.Metric) {
for _, server := range nc.servers {
var resp StreamingServerz
if err := getMetricURL(nc.httpClient, server.URL, &resp); err != nil {
Debugf("ignoring server %s: %v", server.ID, err)
continue
}
ch <- prometheus.MustNewConstMetric(nc.bytesTotal, prometheus.CounterValue, float64(resp.TotalBytes), server.ID)
ch <- prometheus.MustNewConstMetric(nc.msgsTotal, prometheus.CounterValue, float64(resp.TotalMsgs), server.ID)
ch <- prometheus.MustNewConstMetric(nc.channels, prometheus.CounterValue, float64(resp.Channels), server.ID)
ch <- prometheus.MustNewConstMetric(nc.subs, prometheus.CounterValue, float64(resp.Subscriptions), server.ID)
ch <- prometheus.MustNewConstMetric(nc.clients, prometheus.CounterValue, float64(resp.Clients), server.ID)
ch <- prometheus.MustNewConstMetric(nc.info, prometheus.GaugeValue, 1, server.ID, resp.ClusterID, resp.Version, resp.GoVersion, resp.State, resp.Role, resp.StartTime)
}
}
type channelsCollector struct {
sync.Mutex
httpClient *http.Client
servers []*CollectedServer
chanBytesTotal *prometheus.Desc
chanMsgsTotal *prometheus.Desc
chanLastSeq *prometheus.Desc
subsLastSent *prometheus.Desc
subsPendingCount *prometheus.Desc
subsMaxInFlight *prometheus.Desc
}
func newChannelsCollector(servers []*CollectedServer) prometheus.Collector {
subsVariableLabels := []string{"server_id", "channel", "client_id", "inbox", "queue_name", "is_durable", "is_offline"}
nc := &channelsCollector{
httpClient: http.DefaultClient,
chanBytesTotal: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "bytes_total"),
"Total of bytes",
[]string{"server_id", "channel"},
nil,
),
chanMsgsTotal: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "msgs_total"),
"Total of messages",
[]string{"server_id", "channel"},
nil,
),
chanLastSeq: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "last_seq"),
"Last seq",
[]string{"server_id", "channel"},
nil,
),
subsLastSent: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "subs_last_sent"),
"Last message sent",
subsVariableLabels,
nil,
),
subsPendingCount: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "subs_pending_count"),
"Pending message count",
subsVariableLabels,
nil,
),
subsMaxInFlight: prometheus.NewDesc(
prometheus.BuildFQName("nss", "chan", "subs_max_inflight"),
"Max in flight message count",
subsVariableLabels,
nil,
),
}
// create our own deep copy, and tweak the urls to be polled
// for this type of endpoint
nc.servers = make([]*CollectedServer, len(servers))
for i, s := range servers {
nc.servers[i] = &CollectedServer{
ID: s.ID,
URL: s.URL + "/streaming/channelsz?subs=1",
}
}
return nc
}
func (nc *channelsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nc.chanBytesTotal
ch <- nc.chanMsgsTotal
ch <- nc.chanLastSeq
ch <- nc.subsLastSent
ch <- nc.subsPendingCount
ch <- nc.subsMaxInFlight
}
func (nc *channelsCollector) Collect(ch chan<- prometheus.Metric) {
for _, server := range nc.servers {
var resp Channelsz
if err := getMetricURL(nc.httpClient, server.URL, &resp); err != nil {
Debugf("ignoring server %s: %v", server.ID, err)
continue
}
for _, channel := range resp.Channels {
ch <- prometheus.MustNewConstMetric(nc.chanBytesTotal, prometheus.GaugeValue, float64(channel.Bytes), server.ID, channel.Name)
ch <- prometheus.MustNewConstMetric(nc.chanMsgsTotal, prometheus.GaugeValue, float64(channel.Msgs), server.ID, channel.Name)
ch <- prometheus.MustNewConstMetric(nc.chanLastSeq, prometheus.GaugeValue, float64(channel.LastSeq), server.ID, channel.Name)
for _, sub := range channel.Subscriptions {
labelValues := []string{server.ID, channel.Name, sub.ClientID, sub.Inbox,
sub.QueueName, strconv.FormatBool(sub.IsDurable), strconv.FormatBool(sub.IsOffline)}
ch <- prometheus.MustNewConstMetric(nc.subsLastSent, prometheus.GaugeValue, float64(sub.LastSent), labelValues...)
ch <- prometheus.MustNewConstMetric(nc.subsPendingCount, prometheus.GaugeValue, float64(sub.PendingCount), labelValues...)
ch <- prometheus.MustNewConstMetric(nc.subsMaxInFlight, prometheus.GaugeValue, float64(sub.MaxInflight), labelValues...)
}
}
}
}
// Channelsz lists the name of all NATS Streaming Channelsz
type Channelsz struct {
ClusterID string `json:"cluster_id"`
Now time.Time `json:"now"`
Offset int `json:"offset"`
Limit int `json:"limit"`
Count int `json:"count"`
Total int `json:"total"`
Names []string `json:"names,omitempty"`
Channels []*Channelz `json:"channels,omitempty"`
}
// Channelz describes a NATS Streaming Channel
type Channelz struct {
Name string `json:"name"`
Msgs int `json:"msgs"`
Bytes uint64 `json:"bytes"`
FirstSeq uint64 `json:"first_seq"`
LastSeq uint64 `json:"last_seq"`
Subscriptions []*Subscriptionz `json:"subscriptions,omitempty"`
}
// Subscriptionz describes a NATS Streaming Subscription
type Subscriptionz struct {
ClientID string `json:"client_id"`
Inbox string `json:"inbox"`
AckInbox string `json:"ack_inbox"`
DurableName string `json:"durable_name,omitempty"`
QueueName string `json:"queue_name,omitempty"`
IsDurable bool `json:"is_durable"`
IsOffline bool `json:"is_offline"`
MaxInflight int `json:"max_inflight"`
AckWait int `json:"ack_wait"`
LastSent uint64 `json:"last_sent"`
PendingCount int `json:"pending_count"`
IsStalled bool `json:"is_stalled"`
}