-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (132 loc) · 4.08 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
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.")
scrapingInterval = flag.Int("scraping-interval", 300, "Seconds to wait between scraping the AWS metrics if decoupled scraping.")
decoupledScraping = flag.Bool("decoupled-scraping", true, "Decouples scraping and serving of metrics.")
metricsPerQuery = flag.Int("metrics-per-query", 500, "Number of metrics made in a single GetMetricsData request")
labelsSnakeCase = flag.Bool("labels-snake-case", false, "If labels should be output in snake case instead of camel case")
supportedServices = []string{
"alb",
"apigateway",
"appsync",
"asg",
"cf",
"dynamodb",
"ebs",
"ec",
"ec2",
"ecs-svc",
"ecs-containerinsights",
"efs",
"elb",
"emr",
"es",
"firehose",
"fsx",
"kafka",
"kinesis",
"lambda",
"ngw",
"nlb",
"rds",
"redshift",
"r53r",
"s3",
"sfn",
"sns",
"sqs",
"tgw",
"tgwa",
"vpn",
"wafv2",
}
config = conf{}
)
func init() {
// Set JSON structured logging as the default log formatter
log.SetFormatter(&log.JSONFormatter{})
// Set the Output to stdout instead of the default stderr
log.SetOutput(os.Stdout)
// Only log Info severity or above.
log.SetLevel(log.InfoLevel)
}
func updateMetrics(registry *prometheus.Registry) {
tagsData, cloudwatchData := scrapeAwsData(config)
var metrics []*PrometheusMetric
metrics = append(metrics, migrateCloudwatchToPrometheus(cloudwatchData)...)
metrics = append(metrics, migrateTagsToPrometheus(tagsData)...)
metrics = ensureLabelConsistencyForMetrics(metrics)
registry.MustRegister(NewPrometheusCollector(metrics))
for _, counter := range []prometheus.Counter{cloudwatchAPICounter, cloudwatchGetMetricDataAPICounter, cloudwatchGetMetricStatisticsAPICounter, resourceGroupTaggingAPICounter, autoScalingAPICounter, apiGatewayAPICounter} {
if err := registry.Register(counter); err != nil {
log.Warning("Could not publish cloudwatch api metric")
}
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
if *debug {
log.SetLevel(log.DebugLevel)
}
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)
registry := prometheus.NewRegistry()
log.Println("Startup completed")
if *decoupledScraping {
go func() {
for {
newRegistry := prometheus.NewRegistry()
updateMetrics(newRegistry)
log.Debug("Metrics scraped.")
registry = newRegistry
time.Sleep(time.Duration(*scrapingInterval) * time.Second)
}
}()
}
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", func(w http.ResponseWriter, r *http.Request) {
if !(*decoupledScraping) {
newRegistry := prometheus.NewRegistry()
updateMetrics(newRegistry)
log.Debug("Metrics scraped.")
registry = newRegistry
}
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
DisableCompression: false,
})
handler.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(*addr, nil))
}