-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_rds_exporter.go
executable file
·107 lines (86 loc) · 2.5 KB
/
aws_rds_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
package main
import (
"fmt"
"net/http"
"os"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/alecrajeev/aws_rds_exporter/collector"
)
type promHTTPLogger struct {
logger log.Logger
}
func (l promHTTPLogger) Println(v ...interface{}) {
level.Error(l.logger).Log("msg", fmt.Sprint(v...))
}
type rdsOpts struct {
awsRegion string
}
func run() int {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9785").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
opts = rdsOpts{}
)
kingpin.Flag("rds.region", "AWS Region to query").Default("us-east-1").StringVar(&opts.awsRegion)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
fmt.Printf("Starting aws_rds_exporter...")
fmt.Printf("\n")
exporter, err := collector.NewExporter(opts.awsRegion)
if err != nil {
fmt.Printf("Error with rds client")
return 1
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath,
promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
ErrorLog: &promHTTPLogger{
logger: logger,
},
},
),
),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>AWS RDS Exporter</title></head>
<body>
<h1>AWS RDS Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<h2>Options</h2>
<h2>Build</h2>
</body>
</html>`))
})
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
http.HandleFunc("/-/ready", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
})
fmt.Printf("\n")
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
return 0
}
func main() {
exCode := run()
os.Exit(exCode)
}