forked from prometheus-community/elasticsearch_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch_exporter.go
431 lines (364 loc) · 17.3 KB
/
elasticsearch_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"flag"
"io/ioutil"
"log"
"net"
"net/http"
_ "net/http/pprof"
"sync"
"time"
"encoding/json"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "elasticsearch"
)
type VecInfo struct {
help string
labels []string
}
var (
gaugeMetrics = map[string]string{
"indices_fielddata_memory_size_bytes": "Field data cache memory usage in bytes",
"indices_filter_cache_memory_size_bytes": "Filter cache memory usage in bytes",
"indices_query_cache_memory_size_bytes": "Query cache memory usage in bytes",
"indices_request_cache_memory_size_bytes": "Request cache memory usage in bytes",
"indices_docs": "Count of documents on this node",
"indices_docs_deleted": "Count of deleted documents on this node",
"indices_store_size_bytes": "Current size of stored index data in bytes",
"indices_segments_memory_bytes": "Current memory size of segments in bytes",
"indices_segments_count": "Count of index segments on this node",
"process_cpu_percent": "Percent CPU used by process",
"process_mem_resident_size_bytes": "Resident memory in use by process in bytes",
"process_mem_share_size_bytes": "Shared memory in use by process in bytes",
"process_mem_virtual_size_bytes": "Total virtual memory used in bytes",
"process_open_files_count": "Open file descriptors",
"process_max_files_count": "Max file descriptors for process",
}
counterMetrics = map[string]string{
"indices_fielddata_evictions": "Evictions from field data",
"indices_filter_cache_evictions": "Evictions from filter cache",
"indices_query_cache_evictions": "Evictions from query cache",
"indices_request_cache_evictions": "Evictions from request cache",
"indices_flush_total": "Total flushes",
"indices_flush_time_ms_total": "Cumulative flush time in milliseconds",
"transport_rx_packets_total": "Count of packets received",
"transport_rx_size_bytes_total": "Total number of bytes received",
"transport_tx_packets_total": "Count of packets sent",
"transport_tx_size_bytes_total": "Total number of bytes sent",
"indices_store_throttle_time_ms_total": "Throttle time for index store in milliseconds",
"indices_indexing_index_total": "Total index calls",
"indices_indexing_index_time_ms_total": "Cumulative index time in milliseconds",
"indices_merges_total": "Total merges",
"indices_merges_total_docs_total": "Cumulative docs merged",
"indices_merges_total_size_bytes_total": "Total merge size in bytes",
"indices_merges_total_time_ms_total": "Total time spent merging in milliseconds",
"indices_refresh_total": "Total refreshes",
"indices_refresh_total_time_ms_total": "Total time spent refreshing",
}
counterVecMetrics = map[string]*VecInfo{
"jvm_gc_collection_seconds_count": &VecInfo{
help: "Count of JVM GC runs",
labels: []string{"gc"},
},
"jvm_gc_collection_seconds_sum": &VecInfo{
help: "GC run time in seconds",
labels: []string{"gc"},
},
"process_cpu_time_seconds_sum": &VecInfo{
help: "Process CPU time in seconds",
labels: []string{"type"},
},
"thread_pool_completed_count": &VecInfo{
help: "Thread Pool operations completed",
labels: []string{"type"},
},
"thread_pool_rejected_count": &VecInfo{
help: "Thread Pool operations rejected",
labels: []string{"type"},
},
}
gaugeVecMetrics = map[string]*VecInfo{
"breakers_estimated_size_bytes": &VecInfo{
help: "Estimated size in bytes of breaker",
labels: []string{"breaker"},
},
"breakers_limit_size_bytes": &VecInfo{
help: "Limit size in bytes for breaker",
labels: []string{"breaker"},
},
"jvm_memory_committed_bytes": &VecInfo{
help: "JVM memory currently committed by area",
labels: []string{"area"},
},
"jvm_memory_used_bytes": &VecInfo{
help: "JVM memory currently used by area",
labels: []string{"area"},
},
"jvm_memory_max_bytes": &VecInfo{
help: "JVM memory max",
labels: []string{"area"},
},
"thread_pool_active_count": &VecInfo{
help: "Thread Pool threads active",
labels: []string{"type"},
},
"thread_pool_largest_count": &VecInfo{
help: "Thread Pool largest threads count",
labels: []string{"type"},
},
"thread_pool_queue_count": &VecInfo{
help: "Thread Pool operations queued",
labels: []string{"type"},
},
"thread_pool_threads_count": &VecInfo{
help: "Thread Pool current threads count",
labels: []string{"type"},
},
}
)
// Exporter collects Elasticsearch stats from the given server and exports
// them using the prometheus metrics package.
type Exporter struct {
URI string
mutex sync.RWMutex
up prometheus.Gauge
gauges map[string]*prometheus.GaugeVec
gaugeVecs map[string]*prometheus.GaugeVec
counters map[string]*prometheus.CounterVec
counterVecs map[string]*prometheus.CounterVec
allNodes bool
client *http.Client
}
// NewExporter returns an initialized Exporter.
func NewExporter(uri string, timeout time.Duration, allNodes bool) *Exporter {
counters := make(map[string]*prometheus.CounterVec, len(counterMetrics))
counterVecs := make(map[string]*prometheus.CounterVec, len(counterVecMetrics))
gauges := make(map[string]*prometheus.GaugeVec, len(gaugeMetrics))
gaugeVecs := make(map[string]*prometheus.GaugeVec, len(gaugeVecMetrics))
for name, info := range counterVecMetrics {
counterVecs[name] = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: name,
Help: info.help,
}, append([]string{"cluster", "node"}, info.labels...))
}
for name, info := range gaugeVecMetrics {
gaugeVecs[name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: name,
Help: info.help,
}, append([]string{"cluster", "node"}, info.labels...))
}
for name, help := range counterMetrics {
counters[name] = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: name,
Help: help,
}, []string{"cluster", "node"})
}
for name, help := range gaugeMetrics {
gauges[name] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: name,
Help: help,
}, []string{"cluster", "node"})
}
// Init our exporter.
return &Exporter{
URI: uri,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Was the Elasticsearch instance query successful?",
}),
counters: counters,
counterVecs: counterVecs,
gauges: gauges,
gaugeVecs: gaugeVecs,
allNodes: allNodes,
client: &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
if err := c.SetDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
return c, nil
},
},
},
}
}
// Describe describes all the metrics ever exported by the elasticsearch
// exporter. It implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up.Desc()
for _, vec := range e.counters {
vec.Describe(ch)
}
for _, vec := range e.gauges {
vec.Describe(ch)
}
for _, vec := range e.counterVecs {
vec.Describe(ch)
}
for _, vec := range e.gaugeVecs {
vec.Describe(ch)
}
}
// Collect fetches the stats from configured elasticsearch location and
// delivers them as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
// Reset metrics.
for _, vec := range e.gaugeVecs {
vec.Reset()
}
for _, vec := range e.counterVecs {
vec.Reset()
}
for _, vec := range e.gauges {
vec.Reset()
}
for _, vec := range e.counters {
vec.Reset()
}
defer func() { ch <- e.up }()
resp, err := e.client.Get(e.URI)
if err != nil {
e.up.Set(0)
log.Println("Error while querying Elasticsearch:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Failed to read ES response body:", err)
e.up.Set(0)
return
}
e.up.Set(1)
var allStats NodeStatsResponse
err = json.Unmarshal(body, &allStats)
if err != nil {
log.Println("Failed to unmarshal JSON into struct:", err)
return
}
// If we aren't polling all nodes, make sure we only got one response.
if !e.allNodes && len(allStats.Nodes) != 1 {
log.Println("Unexpected number of nodes returned.")
}
for _, stats := range allStats.Nodes {
// GC Stats
for collector, gcstats := range stats.JVM.GC.Collectors {
e.counterVecs["jvm_gc_collection_seconds_count"].WithLabelValues(allStats.ClusterName, stats.Host, collector).Set(float64(gcstats.CollectionCount))
e.counterVecs["jvm_gc_collection_seconds_sum"].WithLabelValues(allStats.ClusterName, stats.Host, collector).Set(float64(gcstats.CollectionTime / 1000))
}
// Breaker stats
for breaker, bstats := range stats.Breakers {
e.gaugeVecs["breakers_estimated_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, breaker).Set(float64(bstats.EstimatedSize))
e.gaugeVecs["breakers_limit_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, breaker).Set(float64(bstats.LimitSize))
}
// Thread Pool stats
for pool, pstats := range stats.ThreadPool {
e.counterVecs["thread_pool_completed_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Completed))
e.counterVecs["thread_pool_rejected_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Rejected))
e.gaugeVecs["thread_pool_active_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Active))
e.gaugeVecs["thread_pool_threads_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Active))
e.gaugeVecs["thread_pool_largest_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Active))
e.gaugeVecs["thread_pool_queue_count"].WithLabelValues(allStats.ClusterName, stats.Host, pool).Set(float64(pstats.Active))
}
// JVM Memory Stats
e.gaugeVecs["jvm_memory_committed_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, "heap").Set(float64(stats.JVM.Mem.HeapCommitted))
e.gaugeVecs["jvm_memory_used_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, "heap").Set(float64(stats.JVM.Mem.HeapUsed))
e.gaugeVecs["jvm_memory_max_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, "heap").Set(float64(stats.JVM.Mem.HeapMax))
e.gaugeVecs["jvm_memory_committed_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, "non-heap").Set(float64(stats.JVM.Mem.NonHeapCommitted))
e.gaugeVecs["jvm_memory_used_bytes"].WithLabelValues(allStats.ClusterName, stats.Host, "non-heap").Set(float64(stats.JVM.Mem.NonHeapUsed))
// Indices Stats
e.gauges["indices_fielddata_memory_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.FieldData.MemorySize))
e.counters["indices_fielddata_evictions"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.FieldData.Evictions))
e.gauges["indices_filter_cache_memory_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.FilterCache.MemorySize))
e.counters["indices_filter_cache_evictions"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.FilterCache.Evictions))
e.gauges["indices_query_cache_memory_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.QueryCache.MemorySize))
e.counters["indices_query_cache_evictions"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.QueryCache.Evictions))
e.gauges["indices_request_cache_memory_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.QueryCache.MemorySize))
e.counters["indices_request_cache_evictions"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.QueryCache.Evictions))
e.gauges["indices_docs"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Docs.Count))
e.gauges["indices_docs_deleted"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Docs.Deleted))
e.gauges["indices_segments_memory_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Segments.Memory))
e.gauges["indices_segments_count"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Segments.Count))
e.gauges["indices_store_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Store.Size))
e.counters["indices_store_throttle_time_ms_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Store.ThrottleTime))
e.counters["indices_flush_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Flush.Total))
e.counters["indices_flush_time_ms_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Flush.Time))
e.counters["indices_indexing_index_time_ms_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Indexing.IndexTime))
e.counters["indices_indexing_index_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Indexing.IndexTotal))
e.counters["indices_merges_total_time_ms_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Merges.TotalTime))
e.counters["indices_merges_total_size_bytes_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Merges.TotalSize))
e.counters["indices_merges_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Merges.Total))
e.counters["indices_refresh_total_time_ms_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Refresh.TotalTime))
e.counters["indices_refresh_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Indices.Refresh.Total))
// Transport Stats
e.counters["transport_rx_packets_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Transport.RxCount))
e.counters["transport_rx_size_bytes_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Transport.RxSize))
e.counters["transport_tx_packets_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Transport.TxCount))
e.counters["transport_tx_size_bytes_total"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Transport.TxSize))
// Process Stats
e.gauges["process_cpu_percent"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Process.CPU.Percent))
e.gauges["process_mem_resident_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Process.Memory.Resident))
e.gauges["process_mem_share_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Process.Memory.Share))
e.gauges["process_mem_virtual_size_bytes"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Process.Memory.TotalVirtual))
e.gauges["process_open_files_count"].WithLabelValues(allStats.ClusterName, stats.Host).Set(float64(stats.Process.OpenFD))
e.counterVecs["process_cpu_time_seconds_sum"].WithLabelValues(allStats.ClusterName, stats.Host, "total").Set(float64(stats.Process.CPU.Total / 1000))
e.counterVecs["process_cpu_time_seconds_sum"].WithLabelValues(allStats.ClusterName, stats.Host, "sys").Set(float64(stats.Process.CPU.Sys / 1000))
e.counterVecs["process_cpu_time_seconds_sum"].WithLabelValues(allStats.ClusterName, stats.Host, "user").Set(float64(stats.Process.CPU.User / 1000))
}
// Report metrics.
for _, vec := range e.counterVecs {
vec.Collect(ch)
}
for _, vec := range e.gaugeVecs {
vec.Collect(ch)
}
for _, vec := range e.counters {
vec.Collect(ch)
}
for _, vec := range e.gauges {
vec.Collect(ch)
}
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9108", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
esURI = flag.String("es.uri", "http://localhost:9200", "HTTP API address of an Elasticsearch node.")
esTimeout = flag.Duration("es.timeout", 5*time.Second, "Timeout for trying to get stats from Elasticsearch.")
esAllNodes = flag.Bool("es.all", false, "Export stats for all nodes in the cluster.")
)
flag.Parse()
if *esAllNodes {
*esURI = *esURI + "/_nodes/stats"
} else {
*esURI = *esURI + "/_nodes/_local/stats"
}
exporter := NewExporter(*esURI, *esTimeout, *esAllNodes)
prometheus.MustRegister(exporter)
log.Println("Starting Server:", *listenAddress)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Elasticsearch Exporter</title></head>
<body>
<h1>Elasticsearch Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}