forked from app-sre/aws-resource-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (79 loc) · 2.47 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
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "aws_resources_exporter"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9115").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
exporterMetrics *ExporterMetrics
)
func main() {
os.Exit(run())
}
func run() int {
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print(namespace))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
level.Info(logger).Log("msg", "Starting"+namespace, "version", version.Info())
level.Info(logger).Log("msg", "Build context", version.BuildContext())
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
level.Error(logger).Log("msg", "AWS_REGION has to be defined")
return 1
}
config := aws.NewConfig().WithRegion(awsRegion)
sess := session.Must(session.NewSession(config))
exporterMetrics = NewExporterMetrics(sess)
prometheus.MustRegister(
exporterMetrics,
NewRDSExporter(sess, logger),
)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>AWS Resources Exporter</title></head>
<body>
<h1>AWS Resources Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
srv := http.Server{Addr: *listenAddress}
srvc := make(chan struct{})
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
go func() {
level.Info(logger).Log("msg", "Starting HTTP server", "address", *listenAddress)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
close(srvc)
}
}()
for {
select {
case <-term:
level.Info(logger).Log("msg", "Received SIGTERM, exiting gracefully...")
return 0
case <-srvc:
return 1
}
}
}