This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
forked from larseen/pgbouncer_exporter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpgbouncer_exporter.go
65 lines (54 loc) · 1.66 KB
/
pgbouncer_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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
const (
namespace = "pgbouncer"
indexHTML = `
<html>
<head>
<title>PgBouncer Exporter</title>
</head>
<body>
<h1>PgBouncer Exporter</h1>
<p>
<a href='%s'>Metrics</a>
</p>
</body>
</html>`
)
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9127", "Address on which to expose metrics and web interface.")
connectionStringPointer = flag.String("pgBouncer.connectionString", "postgres://postgres:@localhost:6543/pgbouncer?sslmode=disable",
"Connection string for accessing pgBouncer. Can also be set using environment variable DATA_SOURCE_NAME")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("pgbouncer_exporter"))
os.Exit(0)
}
connectionString := getEnv("DATA_SOURCE_NAME", *connectionStringPointer)
exporter := NewExporter(connectionString, namespace)
prometheus.MustRegister(exporter)
log.Infoln("Starting pgbouncer exporter version: ", version.Info())
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(indexHTML, *metricsPath)))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}