/
leafz.go
203 lines (180 loc) · 6.67 KB
/
leafz.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
// Copyright 2017-2019 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package collector has various collector utilities and implementations.
package collector
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// isLeafzEndpoint returns wether an endpoint is a leafz or not.
func isLeafzEndpoint(system, endpoint string) bool {
return system == CoreSystem && endpoint == "leafz"
}
// leafzCollector is responsible to gather metrics on leaf nodes.
type leafzCollector struct {
sync.Mutex
httpClient *http.Client
servers []*CollectedServer
leafNodesTotal *prometheus.Desc
leafMetrics *leafMetrics
}
// newLeafzCollector creates a new instance of a leafzCollector.
func newLeafzCollector(system, endpoint string, servers []*CollectedServer) prometheus.Collector {
nc := &leafzCollector{httpClient: http.DefaultClient}
nc.leafNodesTotal = prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_nodes_total"),
"nodes_total",
[]string{"server_id"},
nil)
nc.leafMetrics = newLeafMetrics(system, endpoint)
nc.servers = make([]*CollectedServer, len(servers))
for i, s := range servers {
nc.servers[i] = &CollectedServer{
ID: s.ID,
URL: s.URL + "/leafz",
}
}
return nc
}
// Describe destribes the list of prometheus descriptors available
// to be scraped.
func (nc *leafzCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nc.leafNodesTotal
nc.leafMetrics.Describe(ch)
}
// Collect gathers the server leafz metrics.
func (nc *leafzCollector) Collect(ch chan<- prometheus.Metric) {
for _, server := range nc.servers {
var resp Leafz
if err := getMetricURL(nc.httpClient, server.URL, &resp); err != nil {
Debugf("ignoring server %s: %v", server.ID, err)
continue
}
for _, lf := range resp.Leafs {
nc.leafMetrics.Collect(server, lf, ch)
}
ch <- prometheus.MustNewConstMetric(nc.leafNodesTotal, prometheus.GaugeValue,
float64(resp.LeafNodes), server.ID)
}
}
// leafMetrics has all of the prometheus descriptors related to
// each of the leaf node connections of the server being scraped.
type leafMetrics struct {
info *prometheus.Desc
connRtt *prometheus.Desc
connInMsgs *prometheus.Desc
connOutMsgs *prometheus.Desc
connInBytes *prometheus.Desc
connOutBytes *prometheus.Desc
connSubscriptionsTotal *prometheus.Desc
connSubscriptions *prometheus.Desc
}
// newLeafMetrics initializes a new instance of leafMetrics.
func newLeafMetrics(system, endpoint string) *leafMetrics {
leaf := &leafMetrics{
info: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "info"),
"info",
[]string{"server_id", "account", "ip", "port"},
nil),
connRtt: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_rtt"),
"rtt",
[]string{"server_id", "account", "ip", "port"},
nil),
connInMsgs: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_in_msgs"),
"in_msgs",
[]string{"server_id", "account", "ip", "port"},
nil),
connOutMsgs: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_out_msgs"),
"out_msgs",
[]string{"server_id", "account", "ip", "port"},
nil),
connInBytes: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_in_bytes"),
"in_bytes",
[]string{"server_id", "account", "ip", "port"},
nil),
connOutBytes: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_out_bytes"),
"out_bytes",
[]string{"server_id", "account", "ip", "port"},
nil),
connSubscriptionsTotal: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_subscriptions_total"),
"subscriptions_total",
[]string{"server_id", "account", "ip", "port"},
nil),
connSubscriptions: prometheus.NewDesc(
prometheus.BuildFQName(system, endpoint, "conn_subscriptions"),
"subscriptions",
[]string{"server_id", "account", "ip", "port", "subscription"},
nil),
}
return leaf
}
// Describe destribes the list of prometheus descriptors available
// to be scraped.
func (lm *leafMetrics) Describe(ch chan<- *prometheus.Desc) {
ch <- lm.info
ch <- lm.connRtt
ch <- lm.connInMsgs
ch <- lm.connOutMsgs
ch <- lm.connInBytes
ch <- lm.connOutBytes
ch <- lm.connSubscriptionsTotal
ch <- lm.connSubscriptions
}
// Collect collects all the metrics about the a leafnode connection.
func (lm *leafMetrics) Collect(server *CollectedServer, lf *Leaf, ch chan<- prometheus.Metric) {
rtt, _ := time.ParseDuration(lf.RTT)
ch <- prometheus.MustNewConstMetric(lm.connRtt, prometheus.GaugeValue, rtt.Seconds(),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
ch <- prometheus.MustNewConstMetric(lm.connInMsgs, prometheus.GaugeValue, float64(lf.InMsgs),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
ch <- prometheus.MustNewConstMetric(lm.connOutMsgs, prometheus.GaugeValue, float64(lf.OutMsgs),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
ch <- prometheus.MustNewConstMetric(lm.connInBytes, prometheus.GaugeValue, float64(lf.InBytes),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
ch <- prometheus.MustNewConstMetric(lm.connOutBytes, prometheus.GaugeValue, float64(lf.OutBytes),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
ch <- prometheus.MustNewConstMetric(lm.connSubscriptionsTotal, prometheus.GaugeValue, float64(lf.Subscriptions),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port))
for _, sub := range lf.SubscriptionsList {
ch <- prometheus.MustNewConstMetric(lm.connSubscriptions, prometheus.GaugeValue, float64(0.0),
server.ID, lf.Account, lf.IP, fmt.Sprint(lf.Port), sub)
}
}
// Leafz output
type Leafz struct {
LeafNodes int `json:"leafnodes"`
Leafs []*Leaf `json:"leafs"`
}
// Leaf output
type Leaf struct {
Account string `json:"account"`
IP string `json:"ip"`
Port int `json:"port"`
RTT string `json:"rtt"`
InMsgs int `json:"in_msgs"`
OutMsgs int `json:"out_msgs"`
InBytes int `json:"in_bytes"`
OutBytes int `json:"out_bytes"`
Subscriptions int `json:"subscriptions"`
SubscriptionsList []string `json:"subscriptions_list"`
}