-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
lightmetricset.go
119 lines (104 loc) · 4.4 KB
/
lightmetricset.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package mb
import (
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
)
// LightMetricSet contains the definition of a non-registered metric set
type LightMetricSet struct {
Name string
Module string
Default bool `config:"default"`
Input struct {
Module string `config:"module" validate:"required"`
MetricSet string `config:"metricset" validate:"required"`
Defaults interface{} `config:"defaults"`
} `config:"input" validate:"required"`
}
// Registration obtains a metric set registration for this light metric set, this registration
// contains a metric set factory that reprocess metric set creation taking into account the
// light metric set defaults
func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error) {
registration, err := r.metricSetRegistration(m.Input.Module, m.Input.MetricSet)
if err != nil {
return registration, errors.Wrapf(err,
"failed to start light metricset '%s/%s' using '%s/%s' metricset as input",
m.Module, m.Name,
m.Input.Module, m.Input.MetricSet)
}
originalFactory := registration.Factory
registration.IsDefault = m.Default
// Light modules factory has to override defaults and reproduce builder
// functionality with the resulting configuration, it does:
// - Override defaults
// - Call module factory if registered (it wouldn't have been called
// if light module is really a registered mixed module)
// - Call host parser if defined (it would have already been called
// without the light module defaults)
// - Finally, call the original factory for the registered metricset
registration.Factory = func(base BaseMetricSet) (MetricSet, error) {
// Override default config on base module and metricset
base.name = m.Name
baseModule, err := m.baseModule(base.module)
if err != nil {
return nil, errors.Wrapf(err, "failed to create base module for light module '%s', using base module '%s'", m.Module, base.module.Name())
}
base.module = baseModule
// Run module factory if registered, it will be called once per
// metricset, but it should be idempotent
moduleFactory := r.moduleFactory(m.Input.Module)
if moduleFactory != nil {
module, err := moduleFactory(*baseModule)
if err != nil {
return nil, errors.Wrapf(err, "module factory for module '%s' failed while creating light metricset '%s/%s'", m.Input.Module, m.Module, m.Name)
}
base.module = module
}
// At this point host parser was already run, we need to run this again
// with the overriden defaults
if registration.HostParser != nil {
base.hostData, err = registration.HostParser(base.module, base.host)
if err != nil {
return nil, errors.Wrapf(err, "host parser failed on light metricset factory for '%s/%s'", m.Module, m.Name)
}
base.host = base.hostData.Host
}
return originalFactory(base)
}
return registration, nil
}
// baseModule does the configuration overrides in the base module configuration
// taking into account the light metric set default configurations
func (m *LightMetricSet) baseModule(from Module) (*BaseModule, error) {
// Initialize config using input defaults as raw config
rawConfig, err := common.NewConfigFrom(m.Input.Defaults)
if err != nil {
return nil, errors.Wrap(err, "invalid input defaults")
}
// Copy values from user configuration
if err = from.UnpackConfig(rawConfig); err != nil {
return nil, errors.Wrap(err, "failed to copy values from user configuration")
}
// Create the base module
baseModule, err := newBaseModuleFromConfig(rawConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to create base module")
}
baseModule.name = m.Module
return &baseModule, nil
}