forked from Technofy/cloudwatch_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
194 lines (163 loc) · 5.02 KB
/
collector.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/prometheus/client_golang/prometheus"
"github.com/mtlang/cloudwatch_exporter/config"
)
// Collector represents a prometheus collector. A single collector is used for each scrape.
type Collector struct {
Target string
ScrapeTime prometheus.Gauge
ErroneousRequests prometheus.Counter
Tasks []*config.Task
}
var tasks []*config.Task
func buildTask(task config.Task) *config.Task {
var newTask = new(config.Task)
// Save the task it belongs to (Perform a deep copy)
newTask = new(config.Task)
newTask.Region = task.Region
newTask.Metrics = *new([]config.Metric)
for _, metric := range task.Metrics {
newTask.Metrics = append(newTask.Metrics, metric)
}
newTask.Name = task.Name
newTask.RoleName = task.RoleName
newTask.Account = task.Account
for _, metric := range task.Metrics {
labels := make([]string, len(metric.Dimensions))
for i, dimension := range metric.Dimensions {
labels[i] = toSnakeCase(dimension)
}
labels = append(labels, "task")
labels = append(labels, "region")
labels = append(labels, "account")
labels = append(labels, "statistic")
newTask.Desc = prometheus.NewDesc(
safeName(toSnakeCase(fmt.Sprintf("%s_%s", metric.Namespace, metric.Name))),
fmt.Sprintf("%s %s", metric.Namespace, metric.Name),
labels,
nil)
newTask.ValType = prometheus.GaugeValue
newTask.LabelNames = labels
}
return newTask
}
func getAllRegions() []string {
regionList := []string{}
session := session.Must(session.NewSession())
svc := ec2.New(session, &aws.Config{Region: aws.String("us-east-1")})
result, err := svc.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
println(err.Error())
return regionList
}
for _, region := range result.Regions {
regionList = append(regionList, *region.RegionName)
}
return regionList
}
// generateTasks creates pre-generated metrics descriptions so that only the metrics are created from them during a scrape.
func generateTasks(cfg *config.Settings) {
tasks = []*config.Task{}
for _, task := range cfg.Tasks {
if strings.EqualFold(task.Account, "all") {
region := task.Region
for _, account := range cfg.Accounts {
task.Account = account
// Exclude the account if it's in exclude_accounts
exclude := false
for _, excludeAccount := range cfg.ExcludeAccounts {
if strings.EqualFold(account, excludeAccount) {
exclude = true
}
}
if exclude {
continue
}
if strings.EqualFold(region, "all") {
allRegions := getAllRegions()
for _, regionToAdd := range allRegions {
task.Region = regionToAdd
newTask := buildTask(task)
tasks = append(tasks, newTask)
}
} else {
newTask := buildTask(task)
tasks = append(tasks, newTask)
}
}
} else {
if strings.EqualFold(task.Region, "all") {
allRegions := getAllRegions()
for _, region := range allRegions {
task.Region = region
newTask := buildTask(task)
tasks = append(tasks, newTask)
}
} else {
newTask := buildTask(task)
tasks = append(tasks, newTask)
}
}
}
}
// NewCwCollector creates a new instance of a CwCollector for a specific task
// The newly created instance will reference its parent task so that metric descriptions are not recreated on every call.
// It returns either a pointer to a new instance of cwCollector or an error.
func NewCwCollector(target string, taskName string, region string) (*Collector, error) {
// Check if task exists
_, err := settings.GetTasks(taskName)
if err != nil {
return nil, err
}
tasksToUse := tasks
if region != "" {
tasksToUse = []*config.Task{}
for _, task := range tasks {
if task.Region == region && task.Name == taskName {
tasksToUse = append(tasksToUse, task)
}
}
} else {
tasksToUse = []*config.Task{}
for _, task := range tasks {
if task.Name == taskName {
tasksToUse = append(tasksToUse, task)
}
}
}
return &Collector{
Target: target,
ScrapeTime: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cloudwatch_exporter_scrape_duration_seconds",
Help: "Time this CloudWatch scrape took, in seconds.",
}),
ErroneousRequests: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cloudwatch_exporter_erroneous_requests",
Help: "The number of erroneous request made by this scrape.",
}),
Tasks: tasksToUse,
}, nil
}
// Collect is used by the prometheus library to collect metrics
func (collector *Collector) Collect(ch chan<- prometheus.Metric) {
now := time.Now()
scrape(collector, ch)
collector.ScrapeTime.Set(time.Since(now).Seconds())
ch <- collector.ScrapeTime
ch <- collector.ErroneousRequests
}
// Describe is used by the prometheus library to create descriptions for metrics
func (collector *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.ScrapeTime.Desc()
ch <- collector.ErroneousRequests.Desc()
for _, task := range collector.Tasks {
ch <- task.Desc
}
}