-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (77 loc) · 2.34 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var version = "custom-build"
var (
addr = flag.String("listen-address", ":5000", "The address to listen on.")
configFile = flag.String("config.file", "config.yml", "Path to configuration file.")
debug = flag.Bool("debug", false, "Add verbose logging")
showVersion = flag.Bool("v", false, "prints current yace version.")
cloudwatchConcurrency = flag.Int("cloudwatch-concurrency", 5, "Maximum number of concurrent requests to CloudWatch API")
tagConcurrency = flag.Int("tag-concurrency", 5, "Maximum number of concurrent requests to Resource Tagging API")
supportedServices = []string{
"alb",
"dynamodb",
"ebs",
"ec",
"ec2",
"efs",
"elb",
"emr",
"es",
"lambda",
"rds",
"s3",
"kinesis",
"vpn",
"asg",
}
config = conf{}
)
func metricsHandler(w http.ResponseWriter, req *http.Request) {
tagsData, cloudwatchData := scrapeAwsData(config)
var metrics []*PrometheusMetric
metrics = append(metrics, migrateCloudwatchToPrometheus(cloudwatchData)...)
metrics = append(metrics, migrateTagsToPrometheus(tagsData)...)
registry := prometheus.NewRegistry()
registry.MustRegister(NewPrometheusCollector(metrics))
if err := registry.Register(cloudwatchAPICounter); err != nil {
log.Fatal("Could not publish cloudwatch api metric")
}
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
DisableCompression: false,
})
handler.ServeHTTP(w, req)
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
log.Println("Parse config..")
if err := config.load(configFile); err != nil {
log.Fatal("Couldn't read ", *configFile, ":", err)
}
cloudwatchSemaphore = make(chan struct{}, *cloudwatchConcurrency)
tagSemaphore = make(chan struct{}, *tagConcurrency)
log.Println("Startup completed")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Yet another cloudwatch exporter</title></head>
<body>
<h1>Thanks for using our product :)</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
http.HandleFunc("/metrics", metricsHandler)
log.Fatal(http.ListenAndServe(*addr, nil))
}