This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
351 lines (301 loc) · 8.91 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/prometheus/common/log"
"gopkg.in/ldap.v2"
"gopkg.in/yaml.v2"
)
var (
defaultCounterNameTemplate *templateString
defaultmetricNameTemplate *templateString
)
func init() {
defaultmetricNameTemplate = &templateString{
template: template.Must(
template.New("default").Parse(
"{{ .section }}_{{ .attribute }}",
),
),
}
defaultCounterNameTemplate = &templateString{
template: template.Must(
template.New("default").Parse(
"{{ .section }}_{{ .attribute }}_total",
),
),
}
}
type dnString string
func (d *dnString) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
if _, err := ldap.ParseDN(s); err != nil {
return fmt.Errorf("search is malformed: %s", err)
}
*d = (dnString)(s)
return nil
}
type filterString string
func (f *filterString) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
if _, err := ldap.CompileFilter(s); err != nil {
return fmt.Errorf("filter is malformed: %s", err)
}
*f = (filterString)(s)
return nil
}
type templateString struct {
template *template.Template
}
func (t *templateString) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
new_t, err := template.New("config supplied template").Funcs((template.FuncMap)(sprig.FuncMap())).Parse(s)
if err != nil {
return fmt.Errorf("template parse failure; error was %s, template was:\n%s", err, s)
}
t.template = new_t
return nil
}
type metricAttributeConfig struct {
Name string `yaml:"metric_name"`
Type string `yaml:"type"`
Labels []string `yaml:"labels"`
Translator templateString `yaml:"translator"`
Help string `yaml:"help"`
X map[string]interface{} `yaml:",inline"`
}
func (mac *metricAttributeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain metricAttributeConfig
if err := unmarshal((*plain)(mac)); err != nil {
return err
}
if err := checkOverflow(mac.X, "config"); err != nil {
return err
}
if mac.Type == "" {
return fmt.Errorf("type must be defined")
}
for idx, label := range mac.Labels {
if len(strings.TrimSpace(label)) != len(label) {
return fmt.Errorf("label at index %d cannot have whitespace and must be nonempty: '%s'", idx, label)
}
}
return nil
}
type attributeConfig struct {
Labels map[string]string `yaml:"labels"`
Metrics map[string]metricAttributeConfig `yaml:"metrics"`
X map[string]interface{} `yaml:",inline"`
}
func (a *attributeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain attributeConfig
if err := unmarshal((*plain)(a)); err != nil {
return err
}
if err := checkOverflow(a.X, "config"); err != nil {
return err
}
return nil
}
type scopeChoice int
func (s *scopeChoice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var choice string
if err := unmarshal(&choice); err != nil {
return err
}
// support the gopkg/ldap.v2 names for this, in addition to our shortened names.
for scope, name := range ldap.ScopeMap {
if name == choice {
*s = scopeChoice(scope)
return nil
}
}
switch choice {
case "base":
*s = ldap.ScopeBaseObject
return nil
case "single":
*s = ldap.ScopeSingleLevel
return nil
case "subtree":
*s = ldap.ScopeWholeSubtree
return nil
}
return fmt.Errorf("ldap search scope %s is unknown; supported options are 'base', 'single', and 'subtree'. Optionally, you can also use the ldap.v2's naming: %v", choice, ldap.ScopeMap)
}
type derefChoice int
func (d *derefChoice) UnmarshalYAML(unmarshal func(interface{}) error) error {
var choice string
if err := unmarshal(&choice); err != nil {
return err
}
switch choice {
case "never":
*d = ldap.NeverDerefAliases
return nil
case "search":
*d = ldap.DerefInSearching
return nil
case "base":
*d = ldap.DerefFindingBaseObj
return nil
case "always":
*d = ldap.DerefAlways
return nil
}
return fmt.Errorf("ldap deref choice %s is unknown; supported options are 'never', 'search', 'base', and 'always'", choice)
}
type metricSourceConfig struct {
Name string `yaml:"name"`
Search *dnString `yaml:"search"`
Filter *filterString `yaml:"filter"`
Scope *scopeChoice `yaml:"scope"`
Deref *derefChoice `yaml:"deref"`
CounterNameTemplate *templateString `yaml:"counter_metric_name_template"`
GaugeNameTemplate *templateString `yaml:"gauge_metric_name_template"`
Attributes attributeConfig `yaml:"attributes"`
ConstantLabels map[string]string `yaml:"labels"`
labelsFromAttributes []string
metricAttributes map[string]MetricAttribute
X map[string]interface{} `yaml:",inline"`
}
func (s *metricSourceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain metricSourceConfig
if err := unmarshal((*plain)(s)); err != nil {
return err
}
if err := checkOverflow(s.X, "config"); err != nil {
return err
}
if s.Search == nil {
return fmt.Errorf("search is either empty or undefined")
}
if s.Filter == nil {
var f = "(objectClass=*)"
s.Filter = (*filterString)(&f)
}
if s.CounterNameTemplate == nil {
s.CounterNameTemplate = defaultCounterNameTemplate
}
if s.GaugeNameTemplate == nil {
s.GaugeNameTemplate = defaultmetricNameTemplate
}
if s.Scope == nil {
// go doesn't let you take the address of a raw integer- say ldap.ScopeBaseObject for example.
// hence the new shenanigans here so we have an object.
s.Scope = new(scopeChoice)
*s.Scope = ldap.ScopeBaseObject
}
if s.Deref == nil {
// go doesn't let you take the address of a raw integer- say ldap.ScopeBaseObject for example.
// hence the new shenanigans here so we have an object.
s.Deref = new(derefChoice)
*s.Deref = ldap.DerefAlways
}
for key, value := range s.ConstantLabels {
if len(strings.TrimSpace(value)) != len(value) {
return fmt.Errorf("constant label for attribute %s cannot have whitespace and must be nonempty: '%s'", key, value)
}
}
s.metricAttributes = make(map[string]MetricAttribute)
for src, final_name := range s.Attributes.Labels {
for _, v := range s.labelsFromAttributes {
if final_name == v {
return fmt.Errorf("duplicate label names found for %s->%s; '%s' already is a label", src, final_name, final_name)
}
}
s.labelsFromAttributes = append(s.labelsFromAttributes, final_name)
}
for attr, metric_config := range s.Attributes.Metrics {
if err := checkOverflow(metric_config.X, fmt.Sprintf("attribute %s", attr)); err != nil {
return err
}
if err := s.createMetricAttribute(&metric_config, attr); err != nil {
return err
}
}
return nil
}
func (msc *metricSourceConfig) createMetricAttribute(a *metricAttributeConfig, attribute string) error {
help := a.Help
if help == "" {
log.Warnf("section %s, attribute %s: no help provided", msc.Name, attribute)
help = "No help provided"
}
setName := func(t *templateString) error {
if a.Name == "" {
var buffer bytes.Buffer
if err := t.template.Option("missingkey=error").Execute(&buffer, map[string]string{"section": msc.Name, "attribute": attribute}); err != nil {
return fmt.Errorf("attribute %s naming error: %s", attribute, err)
}
log.Debugf("templating metric name for attr %s to %s", attribute, buffer.String())
a.Name = buffer.String()
}
a.Name = fmt.Sprintf("ldap_%s", a.Name)
return nil
}
labels := a.Labels
if len(msc.labelsFromAttributes) != 0 {
labels = []string{}
labels = append(labels, msc.labelsFromAttributes...)
labels = append(labels, a.Labels...)
}
switch a.Type {
case "counter":
if err := setName(msc.CounterNameTemplate); err != nil {
return err
}
msc.metricAttributes[attribute] = (MetricAttribute)(NewCounterMetricAttribute(
a.Name,
labels,
msc.ConstantLabels,
a.Translator.template,
help,
))
case "gauge":
if err := setName(msc.GaugeNameTemplate); err != nil {
return err
}
msc.metricAttributes[attribute] = (MetricAttribute)(NewGaugeMetricAttribute(
a.Name,
labels,
msc.ConstantLabels,
a.Translator.template,
help,
))
default:
return fmt.Errorf("type %s isn't valid for attribute %s", a.Type, attribute)
}
return nil
}
func LoadConfig(data string) ([]*MetricsSource, error) {
var parsed_data []metricSourceConfig
if err := yaml.Unmarshal([]byte(data), &parsed_data); err != nil {
return nil, err
}
var sources []*MetricsSource
for _, section := range parsed_data {
sources = append(sources, NewMetricsSource((*string)(section.Search), (*string)(section.Filter), (int)(*section.Scope), (int)(*section.Deref), section.metricAttributes, section.Attributes.Labels))
}
return sources, nil
}
func LoadConfigFile(path string) ([]*MetricsSource, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return LoadConfig(string(content))
}