-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathravendb_exporter.go
105 lines (82 loc) · 2.68 KB
/
ravendb_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
package main
import (
"fmt"
"net/http"
"time"
"github.com/namsral/flag"
"github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
log = logrus.New()
timeout time.Duration
port uint
verbose bool
ravenDbURL string
caCertFile string
useAuth bool
clientCertFile string
clientKeyFile string
clientKeyPassword string
)
func serveLandingPage() {
var landingPage = []byte(`<html>
<head><title>RavenDB exporter for Prometheus</title></head>
<body>
<h1>RavenDB exporter for Prometheus</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>
`)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage) // nolint: errcheck
})
}
func serveMetrics() {
prometheus.MustRegister(newExporter())
http.Handle("/metrics", promhttp.Handler())
}
func readAndValidateConfig() {
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
flag.StringVar(&ravenDbURL, "ravendb-url", "http://localhost:8080", "RavenDB URL")
flag.UintVar(&port, "port", 9440, "Port to expose scraping endpoint on")
flag.DurationVar(&timeout, "timeout", time.Second*10, "Timeout when calling RavenDB")
flag.BoolVar(&verbose, "verbose", false, "Enable verbose logging")
flag.StringVar(&caCertFile, "ca-cert", "", "Path to CA public cert file of RavenDB server")
flag.BoolVar(&useAuth, "use-auth", false, "If set, connection to RavenDB will be authenticated with a client certificate")
flag.StringVar(&clientCertFile, "client-cert", "", "Path to client public certificate used for authentication")
flag.StringVar(&clientKeyFile, "client-key", "", "Path to client private key used for authentication")
flag.StringVar(&clientKeyPassword, "client-key-password", "", "(optional) Password for the client private keys")
flag.Parse()
log.WithFields(logrus.Fields{
"ravenDbUrl": ravenDbURL,
"caCert": caCertFile,
"useAuth": useAuth,
"clientCert": clientCertFile,
"clientKey": clientKeyFile,
"port": port,
"timeout": timeout,
"verbose": verbose,
}).Infof("RavenDB exporter configured")
if useAuth && (caCertFile == "" || clientCertFile == "" || clientKeyFile == "") {
log.Fatal("Invalid configuration: when using authentication you need to specify the CA cert, client cert and client private key")
}
}
func setupLogger() {
if verbose {
log.Level = logrus.DebugLevel
}
}
func startHTTPServer() {
listenAddr := fmt.Sprintf(":%d", port)
log.Fatal(http.ListenAndServe(listenAddr, nil))
}
func main() {
readAndValidateConfig()
setupLogger()
initializeClient()
serveLandingPage()
serveMetrics()
startHTTPServer()
}