-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
106 lines (88 loc) · 3.3 KB
/
main.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
package main
import (
"flag"
"net/http"
"strings"
"time"
"github.com/chamilad/kibana-prometheus-exporter/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
addr = flag.String("web.listen-address", ":9684", "The address to listen on for HTTP requests.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "The address to listen on for HTTP requests.")
kibanaURI = flag.String("kibana.uri", "", "The Kibana API to fetch metrics from")
kibanaUsername = flag.String("kibana.username", "", "The username to use for Kibana API")
kibanaPassword = flag.String("kibana.password", "", "The password to use for Kibana API")
kibanaSkipTLS = flag.Bool("kibana.skip-tls", false, "Skip TLS verification for TLS secured Kibana URLs")
debug = flag.Bool("debug", false, "Output verbose details during metrics collection, use for development only")
wait = flag.Bool(
"wait",
false,
"Wait for Kibana to be responsive before starting, setting this to false would cause the exporter to error out instead of waiting",
)
namespace = "kibana"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
flag.Parse()
*kibanaURI = strings.TrimSpace(*kibanaURI)
*kibanaUsername = strings.TrimSpace(*kibanaUsername)
*kibanaPassword = strings.TrimSpace(*kibanaPassword)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
if *kibanaURI == "" {
log.Fatal().Msg("required flag -kibana.uri not provided, aborting")
}
*kibanaURI = strings.TrimSuffix(*kibanaURI, "/")
log.Printf("using Kibana URL: %s", *kibanaURI)
collector, err := exporter.NewCollector(*kibanaURI, *kibanaUsername, *kibanaPassword, *kibanaSkipTLS)
if err != nil {
log.Fatal().Msgf("error while initializing collector: %s", err)
}
exporter, err := exporter.NewExporter(namespace, collector)
if err != nil {
log.Fatal().Msgf("error while initializing exporter: %s", err)
}
if *wait {
// blocking wait for Kibana to be responsive
collector.WaitForConnection()
} else if !collector.TestConnection() {
log.Fatal().Msg("not waiting for Kibana to be responsive")
}
prometheus.MustRegister(exporter)
// readable output
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err = w.Write([]byte(`<html>
<head><title>Kibana Exporter</title></head>
<body>
<h1>Kibana Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
log.Warn().Msgf("error while writing response to /metrics call: %s", err)
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
w.WriteHeader(http.StatusOK)
return
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
})
http.Handle(*metricsPath, promhttp.Handler())
log.Info().Msgf("starting metrics server at %s", *addr)
// CWE-676, https://app.deepsource.com/directory/analyzers/go/issues/GO-S2114
server := &http.Server{
Addr: *addr,
ReadHeaderTimeout: 3 * time.Second, // low timeout since the response is straightforward
}
err = server.ListenAndServe()
log.Fatal().Msgf("%s", err)
}