-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
192 lines (169 loc) · 5.13 KB
/
config.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
package main
import (
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type conf struct {
Discovery discovery `yaml:"discovery"`
Static []static `yaml:"static"`
}
type discovery struct {
ExportedTagsOnMetrics exportedTagsOnMetrics `yaml:"exportedTagsOnMetrics"`
Jobs []job `yaml:"jobs"`
}
type exportedTagsOnMetrics map[string][]string
type job struct {
Regions []string `yaml:"regions"`
Type string `yaml:"type"`
RoleArns []string `yaml:"roleArns"`
AwsDimensions []string `yaml:"awsDimensions"`
SearchTags []tag `yaml:"searchTags"`
CustomTags []tag `yaml:"customTags"`
Metrics []metric `yaml:"metrics"`
Length int `yaml:"length"`
Delay int `yaml:"delay"`
Period int `yaml:"period"`
AddCloudwatchTimestamp bool `yaml:"addCloudwatchTimestamp"`
}
type static struct {
Name string `yaml:"name"`
Regions []string `yaml:"regions"`
RoleArns []string `yaml:"roleArns"`
Namespace string `yaml:"namespace"`
CustomTags []tag `yaml:"customTags"`
Dimensions []dimension `yaml:"dimensions"`
Metrics []metric `yaml:"metrics"`
}
type metric struct {
Name string `yaml:"name"`
Statistics []string `yaml:"statistics"`
AdditionalDimensions []dimension `yaml:"additionalDimensions"`
Period int `yaml:"period"`
Length int `yaml:"length"`
Delay int `yaml:"delay"`
NilToZero bool `yaml:"nilToZero"`
AddCloudwatchTimestamp bool `yaml:"addCloudwatchTimestamp"`
}
type dimension struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type tag struct {
Key string `yaml:"Key"`
Value string `yaml:"Value"`
}
func (c *conf) load(file *string) error {
yamlFile, err := ioutil.ReadFile(*file)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
return err
}
for n, job := range c.Discovery.Jobs {
if len(job.RoleArns) == 0 {
c.Discovery.Jobs[n].RoleArns = []string{""} // use current IAM role
}
}
for n, job := range c.Static {
if len(job.RoleArns) == 0 {
c.Static[n].RoleArns = []string{""} // use current IAM role
}
}
err = c.validate()
if err != nil {
return err
}
return nil
}
func (c *conf) validate() error {
if c.Discovery.Jobs == nil && c.Static == nil {
return fmt.Errorf("At least 1 Discovery job or 1 Static must be defined")
}
if c.Discovery.Jobs != nil {
for idx, job := range c.Discovery.Jobs {
err := c.validateDiscoveryJob(job, idx)
if err != nil {
return err
}
}
}
if c.Static != nil {
for idx, job := range c.Static {
err := c.validateStaticJob(job, idx)
if err != nil {
return err
}
}
}
return nil
}
func (c *conf) validateDiscoveryJob(j job, jobIdx int) error {
if j.Type != "" {
if !stringInSlice(j.Type, supportedServices) {
return fmt.Errorf("Discovery job [%d]: Service is not in known list!: %s", jobIdx, j.Type)
}
} else {
return fmt.Errorf("Discovery job [%d]: Type should not be empty", jobIdx)
}
if len(j.Regions) == 0 {
return fmt.Errorf("Discovery job [%s/%d]: Regions should not be empty", j.Type, jobIdx)
}
if len(j.Metrics) == 0 {
return fmt.Errorf("Discovery job [%s/%d]: Metrics should not be empty", j.Type, jobIdx)
}
for metricIdx, metric := range j.Metrics {
parent := fmt.Sprintf("Discovery job [%s/%d]", j.Type, jobIdx)
err := c.validateMetric(metric, metricIdx, parent, &j)
if err != nil {
return err
}
}
return nil
}
func (c *conf) validateStaticJob(j static, jobIdx int) error {
if j.Name == "" {
return fmt.Errorf("Static job [%v]: Name should not be empty", jobIdx)
}
if j.Namespace == "" {
return fmt.Errorf("Static job [%s/%d]: Namespace should not be empty", j.Name, jobIdx)
}
if len(j.Regions) == 0 {
return fmt.Errorf("Static job [%s/%d]: Regions should not be empty", j.Name, jobIdx)
}
for metricIdx, metric := range j.Metrics {
err := c.validateMetric(metric, metricIdx, fmt.Sprintf("Static job [%s/%d]", j.Name, jobIdx), nil)
if err != nil {
return err
}
}
return nil
}
func (c *conf) validateMetric(m metric, metricIdx int, parent string, discovery *job) error {
if m.Name == "" {
return fmt.Errorf("Metric [%s/%d] in %v: Name should not be empty", m.Name, metricIdx, parent)
}
if len(m.Statistics) == 0 {
return fmt.Errorf("Metric [%s/%d] in %v: Statistics should not be empty", m.Name, metricIdx, parent)
}
mPeriod := m.Period
if mPeriod == 0 && discovery != nil {
mPeriod = discovery.Period
}
if mPeriod < 1 {
return fmt.Errorf("Metric [%s/%d] in %v: Period value should be a positive integer", m.Name, metricIdx, parent)
}
mLength := m.Length
if mLength == 0 && discovery != nil {
mLength = discovery.Length
}
if mLength < mPeriod {
log.Warningf(
"Metric [%s/%d] in %v: length(%d) is smaller than period(%d). This can cause that the data requested is not ready and generate data gaps",
m.Name, metricIdx, parent, mLength, mPeriod)
}
return nil
}