forked from BonnierNews/logstash_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstash_exporter.go
124 lines (106 loc) · 3.47 KB
/
logstash_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
package main
import (
"github.com/BonnierNews/logstash_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
_ "net/http/pprof"
"sync"
"time"
)
var (
scrapeDurations = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: collector.Namespace,
Subsystem: "exporter",
Name: "scrape_duration_seconds",
Help: "logstash_exporter: Duration of a scrape job.",
},
[]string{"collector", "result"},
)
)
// LogstashCollector collector type
type LogstashCollector struct {
collectors map[string]collector.Collector
}
// NewLogstashCollector register a logstash collector
func NewLogstashCollector(logstashEndpoint string) (*LogstashCollector, error) {
nodeStatsCollector, err := collector.NewNodeStatsCollector(logstashEndpoint)
if err != nil {
log.Fatalf("Cannot register a new collector: %v", err)
}
nodeInfoCollector, err := collector.NewNodeInfoCollector(logstashEndpoint)
if err != nil {
log.Fatalf("Cannot register a new collector: %v", err)
}
return &LogstashCollector{
collectors: map[string]collector.Collector{
"node": nodeStatsCollector,
"info": nodeInfoCollector,
},
}, nil
}
func listen(exporterBindAddress string) {
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})
log.Infoln("Starting server on", exporterBindAddress)
if err := http.ListenAndServe(exporterBindAddress, nil); err != nil {
log.Fatalf("Cannot start Logstash exporter: %s", err)
}
}
// Describe logstash metrics
func (coll LogstashCollector) Describe(ch chan<- *prometheus.Desc) {
scrapeDurations.Describe(ch)
}
// Collect logstash metrics
func (coll LogstashCollector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(coll.collectors))
for name, c := range coll.collectors {
go func(name string, c collector.Collector) {
execute(name, c, ch)
wg.Done()
}(name, c)
}
wg.Wait()
scrapeDurations.Collect(ch)
}
func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
begin := time.Now()
err := c.Collect(ch)
duration := time.Since(begin)
var result string
if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
result = "error"
} else {
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
result = "success"
}
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
}
func init() {
prometheus.MustRegister(version.NewCollector("logstash_exporter"))
}
func main() {
var (
logstashEndpoint = kingpin.Flag("logstash.endpoint", "The protocol, host and port on which logstash metrics API listens").Default("http://localhost:9600").String()
exporterBindAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9198").String()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("logstash_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logstashCollector, err := NewLogstashCollector(*logstashEndpoint)
if err != nil {
log.Fatalf("Cannot register a new Logstash Collector: %v", err)
}
prometheus.MustRegister(logstashCollector)
log.Infoln("Starting Logstash exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
listen(*exporterBindAddress)
}