forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
builders.go
266 lines (228 loc) · 6.86 KB
/
builders.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
package mb
import (
"fmt"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/joeshaw/multierror"
"github.com/pkg/errors"
)
var debugf = logp.MakeDebug("mb")
var (
// ErrEmptyConfig indicates that modules configuration list is nil or empty.
ErrEmptyConfig = errors.New("one or more modules must be configured")
// ErrAllModulesDisabled indicates that all modules are disabled. At least
// one module must be enabled.
ErrAllModulesDisabled = errors.New("all modules are disabled")
)
// NewModules builds new Modules and their associated MetricSets based on the
// provided configuration data. config is a list module config data (the data
// will be unpacked into ModuleConfig structs). r is the Register where the
// ModuleFactory's and MetricSetFactory's will be obtained from. This method
// returns a mapping of Modules to MetricSets or an error.
func NewModules(config []*common.Config, r *Register) (map[Module][]MetricSet, error) {
if config == nil || len(config) == 0 {
return nil, ErrEmptyConfig
}
baseModules, err := newBaseModulesFromConfig(config)
if err != nil {
return nil, err
}
// Create new Modules using the registered ModuleFactory's
modules, err := createModules(r, baseModules)
if err != nil {
return nil, err
}
// Create new MetricSets for each Module using the registered MetricSetFactory's
modToMetricSets, err := initMetricSets(r, modules)
if err != nil {
return nil, err
}
if len(modToMetricSets) == 0 {
return nil, ErrAllModulesDisabled
}
debugf("mb.NewModules() is returning %s", modToMetricSets)
return modToMetricSets, nil
}
// newBaseModulesFromConfig creates new BaseModules from a list of configs
// each containing ModuleConfig data.
func newBaseModulesFromConfig(config []*common.Config) ([]BaseModule, error) {
var errs multierror.Errors
baseModules := make([]BaseModule, 0, len(config))
for _, rawConfig := range config {
bm, err := newBaseModuleFromConfig(rawConfig)
if err != nil {
errs = append(errs, err)
continue
}
if bm.config.Enabled {
baseModules = append(baseModules, bm)
}
}
return baseModules, errs.Err()
}
// newBaseModuleFromConfig creates a new BaseModule from config. The returned
// BaseModule's name will always be lower case.
func newBaseModuleFromConfig(rawConfig *common.Config) (BaseModule, error) {
baseModule := BaseModule{
config: DefaultModuleConfig(),
rawConfig: rawConfig,
}
err := rawConfig.Unpack(&baseModule.config)
if err != nil {
return baseModule, err
}
// If timeout is not set, timeout is set to the same value as period
if baseModule.config.Timeout == 0 {
baseModule.config.Timeout = baseModule.config.Period
}
baseModule.name = strings.ToLower(baseModule.config.Module)
err = mustNotContainDuplicates(baseModule.config.Hosts)
if err != nil {
return baseModule, errors.Wrapf(err, "invalid hosts for module '%s'", baseModule.name)
}
return baseModule, nil
}
func createModules(r *Register, baseModules []BaseModule) ([]Module, error) {
modules := make([]Module, 0, len(baseModules))
var errs multierror.Errors
for _, bm := range baseModules {
f := r.moduleFactory(bm.Name())
if f == nil {
f = DefaultModuleFactory
}
module, err := f(bm)
if err != nil {
errs = append(errs, err)
continue
}
modules = append(modules, module)
}
err := errs.Err()
if err != nil {
return nil, err
}
return modules, nil
}
func initMetricSets(r *Register, modules []Module) (map[Module][]MetricSet, error) {
active := map[Module][]MetricSet{}
var errs multierror.Errors
for _, bms := range newBaseMetricSets(modules) {
f, hostParser, err := r.metricSetFactory(bms.Module().Name(), bms.Name())
if err != nil {
errs = append(errs, err)
continue
}
// Parse the 'host' field using the HostParser registered with the MetricSet.
if hostParser != nil {
bms.hostData, err = hostParser(bms.Module(), bms.host)
if err != nil {
errs = append(errs, errors.Wrapf(err, "host parsing failed for %v-%v",
bms.Module().Name(), bms.Name()))
continue
}
bms.host = bms.hostData.Host
} else {
// The MetricSet was registered without a HostParser so provide a
// default HostData value.
bms.hostData = HostData{URI: bms.host}
}
metricSet, err := f(bms)
if err != nil {
errs = append(errs, err)
continue
}
err = mustImplementFetcher(metricSet)
if err != nil {
errs = append(errs, err)
continue
}
err = mustHaveModule(metricSet, bms)
if err != nil {
errs = append(errs, err)
continue
}
module := metricSet.Module()
active[module] = append(active[module], metricSet)
}
err := errs.Err()
if err != nil {
return nil, err
}
return active, nil
}
// newBaseMetricSets creates a new BaseMetricSet for all MetricSets defined
// in the modules' config.
func newBaseMetricSets(modules []Module) []BaseMetricSet {
baseMetricSets := make([]BaseMetricSet, 0, len(modules))
for _, m := range modules {
hosts := []string{""}
if len(m.Config().Hosts) > 0 {
hosts = m.Config().Hosts
}
for _, name := range m.Config().MetricSets {
for _, host := range hosts {
baseMetricSets = append(baseMetricSets, BaseMetricSet{
name: strings.ToLower(name),
module: m,
host: host,
})
}
}
}
return baseMetricSets
}
// mustHaveModule returns an error if the given MetricSet's Module() method
// returns nil. This validation ensures that all MetricSet implementations
// honor the interface contract.
func mustHaveModule(ms MetricSet, base BaseMetricSet) error {
if ms.Module() == nil {
return fmt.Errorf("%s module cannot be nil in %T", base.module.Name(), ms)
}
return nil
}
// mustImplementFetcher returns an error if the given MetricSet does not
// implement one of the Fetcher interface or if it implements more than one
// of them.
func mustImplementFetcher(ms MetricSet) error {
var ifcs []string
if _, ok := ms.(EventFetcher); ok {
ifcs = append(ifcs, "EventFetcher")
}
if _, ok := ms.(EventsFetcher); ok {
ifcs = append(ifcs, "EventsFetcher")
}
switch len(ifcs) {
case 0:
return fmt.Errorf("MetricSet '%s/%s' does not implement a Fetcher "+
"interface", ms.Module().Name(), ms.Name())
case 1:
return nil
default:
return fmt.Errorf("MetricSet '%s/%s' can only implement a single "+
"Fetcher interface, but implements %v", ms.Module().Name(),
ms.Name(), ifcs)
}
}
// mustNotContainDuplicates returns an error if the given slice contains
// duplicate values.
func mustNotContainDuplicates(s []string) error {
duplicates := map[string]struct{}{}
set := make(map[string]struct{}, len(s))
for _, v := range s {
_, encountered := set[v]
if encountered {
duplicates[v] = struct{}{}
continue
}
set[v] = struct{}{}
}
if len(duplicates) > 0 {
var keys []string
for dup := range duplicates {
keys = append(keys, dup)
}
return fmt.Errorf("duplicates detected [%s]", strings.Join(keys, ", "))
}
return nil
}