forked from prometheus/consul_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul_exporter.go
287 lines (257 loc) · 8.17 KB
/
consul_exporter.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
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
consul_api "github.com/hashicorp/consul/api"
consul "github.com/hashicorp/consul/consul/structs"
)
const (
namespace = "consul"
)
var (
up = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Was the last query of Consul successful.",
nil, nil,
)
clusterServers = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "raft_peers"),
"How many peers (servers) are in the Raft cluster.",
nil, nil,
)
nodeCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "serf_lan_members"),
"How many members are in the cluster.",
nil, nil,
)
serviceCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "catalog_services"),
"How many services are in the cluster.",
nil, nil,
)
serviceNodesHealthy = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "catalog_service_node_healthy"),
"Is this service healthy on this node?",
[]string{"service", "node"}, nil,
)
nodeChecks = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "health_node_status"),
"Status of health checks associated with a node.",
[]string{"check", "node"}, nil,
)
serviceChecks = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "health_service_status"),
"Status of health checks associated with a service.",
[]string{"check", "node", "service"}, nil,
)
keyValues = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "catalog_kv"),
"The values for selected keys in Consul's key/value catalog. Keys with non-numeric values are omitted.",
[]string{"key"}, nil,
)
)
// Exporter collects Consul stats from the given server and exports them using
// the prometheus metrics package.
type Exporter struct {
URI string
client *consul_api.Client
kvPrefix string
kvFilter *regexp.Regexp
healthSummary bool
}
// NewExporter returns an initialized Exporter.
func NewExporter(uri, kvPrefix, kvFilter string, healthSummary bool) (*Exporter, error) {
// parse uri to extract scheme
if !strings.Contains(uri, "://") {
uri = "http://" + uri
}
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("invalid consul URL: %s", err)
}
if u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
return nil, fmt.Errorf("invalid consul URL: %s", uri)
}
// Set up our Consul client connection.
client, _ := consul_api.NewClient(&consul_api.Config{
Address: u.Host,
Scheme: u.Scheme,
})
// Init our exporter.
return &Exporter{
URI: uri,
client: client,
kvPrefix: kvPrefix,
kvFilter: regexp.MustCompile(kvFilter),
healthSummary: healthSummary,
}, nil
}
// Describe describes all the metrics ever exported by the Consul exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- up
ch <- clusterServers
ch <- nodeCount
ch <- serviceCount
ch <- serviceNodesHealthy
ch <- nodeChecks
ch <- serviceChecks
ch <- keyValues
}
// Collect fetches the stats from configured Consul location and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// How many peers are in the Consul cluster?
peers, err := e.client.Status().Peers()
if err != nil {
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 0,
)
log.Errorf("Query error is %v", err)
return
}
// We'll use peers to decide that we're up.
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 1,
)
ch <- prometheus.MustNewConstMetric(
clusterServers, prometheus.GaugeValue, float64(len(peers)),
)
// How many nodes are registered?
nodes, _, err := e.client.Catalog().Nodes(&consul_api.QueryOptions{})
if err != nil {
// FIXME: How should we handle a partial failure like this?
} else {
ch <- prometheus.MustNewConstMetric(
nodeCount, prometheus.GaugeValue, float64(len(nodes)),
)
}
// Query for the full list of services.
serviceNames, _, err := e.client.Catalog().Services(&consul_api.QueryOptions{})
if err != nil {
// FIXME: How should we handle a partial failure like this?
return
}
ch <- prometheus.MustNewConstMetric(
serviceCount, prometheus.GaugeValue, float64(len(serviceNames)),
)
if e.healthSummary {
e.collectHealthSummary(ch, serviceNames)
}
checks, _, err := e.client.Health().State("any", &consul_api.QueryOptions{})
if err != nil {
log.Errorf("Failed to query service health: %v", err)
return
}
for _, hc := range checks {
var passing float64
if hc.Status == consul.HealthPassing {
passing = 1
}
if hc.ServiceID == "" {
ch <- prometheus.MustNewConstMetric(
nodeChecks, prometheus.GaugeValue, passing, hc.CheckID, hc.Node,
)
} else {
ch <- prometheus.MustNewConstMetric(
serviceChecks, prometheus.GaugeValue, passing, hc.CheckID, hc.Node, hc.ServiceID,
)
}
}
e.collectKeyValues(ch)
}
// collectHealthSummary collects health information about every node+service
// combination. It will cause one lookup query per service.
func (e *Exporter) collectHealthSummary(ch chan<- prometheus.Metric, serviceNames map[string][]string) {
for s := range serviceNames {
service, _, err := e.client.Health().Service(s, "", false, &consul_api.QueryOptions{})
if err != nil {
log.Errorf("Failed to query service health: %v", err)
continue
}
for _, entry := range service {
// We have a Node, a Service, and one or more Checks. Our
// service-node combo is passing if all checks have a `status`
// of "passing."
passing := 1.
for _, hc := range entry.Checks {
if hc.Status != consul.HealthPassing {
passing = 0
break
}
}
ch <- prometheus.MustNewConstMetric(
serviceNodesHealthy, prometheus.GaugeValue, passing, entry.Service.ID, entry.Node.Node,
)
}
}
}
func (e *Exporter) collectKeyValues(ch chan<- prometheus.Metric) {
if e.kvPrefix == "" {
return
}
kv := e.client.KV()
pairs, _, err := kv.List(e.kvPrefix, &consul_api.QueryOptions{})
if err != nil {
log.Errorf("Error fetching key/values: %s", err)
return
}
for _, pair := range pairs {
if e.kvFilter.MatchString(pair.Key) {
val, err := strconv.ParseFloat(string(pair.Value), 64)
if err == nil {
ch <- prometheus.MustNewConstMetric(
keyValues, prometheus.GaugeValue, val, pair.Key,
)
}
}
}
}
func init() {
prometheus.MustRegister(version.NewCollector("consul_exporter"))
}
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9107", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
consulServer = flag.String("consul.server", "http://localhost:8500", "HTTP API address of a Consul server or agent. (prefix with https:// to connect over HTTPS)")
healthSummary = flag.Bool("consul.health-summary", true, "Generate a health summary for each service instance. Needs n+1 queries to collect all information.")
kvPrefix = flag.String("kv.prefix", "", "Prefix from which to expose key/value pairs.")
kvFilter = flag.String("kv.filter", ".*", "Regex that determines which keys to expose.")
)
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("consul_exporter"))
os.Exit(0)
}
log.Infoln("Starting consul_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
exporter, err := NewExporter(*consulServer, *kvPrefix, *kvFilter, *healthSummary)
if err != nil {
log.Fatalln(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Consul Exporter</title></head>
<body>
<h1>Consul Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}