/
config.go
185 lines (151 loc) · 4.4 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
package exportcloudwatch
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
// StatDefaultType describes how to deal with a stat that has no value
type StatDefaultType int
const (
// Prior leaves the stat set to its prior value; this is the default
Prior StatDefaultType = iota
// Zero will set the stat to 0
Zero
// NaN will set the stat to not-a-number
NaN
)
// ExportConfig describes which cloudwatch metrics we want to export. Make sure
// you call Validate.
type ExportConfig struct {
// Namespace and Name map directly to metrics in AWS Cloudwatch
Namespace, Name string
// Dimensions are the names of dimensions to pull the data of, and Statistics
// are the Statistics to pull
Dimensions, Statistics []string
// Both of these filter the metrics based on the values of the dimension
DimensionsMatch, DimensionsNoMatch map[string]*regexp.Regexp
// StatDefault determines the default value for a stat if no value is read
StatDefault StatDefaultType
// NameDerivationVersion specifies the algorithm used to convert CloudWatch metric names
// to Prometheus metric names. 0 (the default) is the "legacy"/"classic" naming scheme;
// 1 is the new naming scheme, which should result in fewer overlaps in derived metric names
NameDerivationVersion uint
// each collector maps to the statistic in the same location
collectors []*prometheus.GaugeVec
}
func (e *ExportConfig) isDynamodDBIndexMetric() bool {
if e.Namespace != "AWS/DynamoDB" {
return false
}
for _, d := range e.Dimensions {
if d == "GlobalSecondaryIndexName" {
return true
}
}
return false
}
func (e *ExportConfig) isRDSDBClusterMetric() bool {
if e.Namespace != "AWS/RDS" {
return false
}
for _, d := range e.Dimensions {
if d == "DBClusterIdentifier" {
return true
}
}
return false
}
func (e *ExportConfig) isRDSDBInstanceMetric() bool {
if e.Namespace != "AWS/RDS" {
return false
}
for _, d := range e.Dimensions {
if d == "DBInstanceIdentifier" {
return true
}
}
return false
}
func (e *ExportConfig) cloudWatchToPrometheusName(base string) string {
switch e.NameDerivationVersion {
case 0:
return cloudWatchToPrometheusNameV0(base)
case 1:
return cloudWatchToPrometheusNameV1(base)
default:
panic(fmt.Sprintf("invalid NameDerivationVersion: %d", e.NameDerivationVersion))
}
}
func (e *ExportConfig) String(i int) string {
var base string
if e.isDynamodDBIndexMetric() {
base = e.Name + "Index" + e.Statistics[i]
} else if e.isRDSDBClusterMetric() {
base = e.Name + "Cluster" + e.Statistics[i]
} else if e.isRDSDBInstanceMetric() {
base = e.Name + "Instance" + e.Statistics[i]
} else {
base = e.Name + e.Statistics[i]
}
base = strings.ToLower(e.Namespace) + "_" + e.cloudWatchToPrometheusName(base)
base = strings.ReplaceAll(base, "/", "_")
return base
}
// Validate returns an error if the configuration is incorrect and registers
// each metric with the default prometheus registry.
func (e *ExportConfig) Validate() error {
if len(e.Statistics) == 0 {
return errors.New("At least one statistic is required")
}
if e.NameDerivationVersion > 1 {
return errors.New("Invalid NameDerivationVersion (must be 0 or 1)")
}
// these to cheaply compare to other list at runtime
sort.Strings(e.Dimensions)
for k := range e.DimensionsMatch {
// verify that we are matching against dimensions we are going to be
// using
var found bool
for _, d := range e.Dimensions {
if k == d {
found = true
break
}
}
if !found {
return errors.New("DimensionsMatch name not in Dimensions")
}
}
for k := range e.DimensionsNoMatch {
// verify that we are matching against dimensions we are going to be
// using
var found bool
for _, d := range e.Dimensions {
if k == d {
found = true
break
}
}
if !found {
return errors.New("DimensionsNoMatch name not in Dimensions")
}
}
e.collectors = make([]*prometheus.GaugeVec, len(e.Statistics))
aliasedDimensions := make([]string, len(e.Dimensions))
for j, d := range e.Dimensions {
aliasedDimensions[j] = e.cloudWatchToPrometheusName(d)
}
for j := range e.Statistics {
e.collectors[j] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: e.String(j),
Help: "",
}, aliasedDimensions)
if err := prometheus.Register(e.collectors[j]); err != nil {
return errors.Wrap(err, "Namespace="+e.Namespace+" Name="+e.Name)
}
}
return nil
}