func TestMissingFields(t *testing.T) { if testing.Verbose() { logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"}) } yml := []map[string]interface{}{ map[string]interface{}{ "include_fields": map[string]interface{}{ "equals": map[string]string{ "type": "process", }, }, }, } config := filter.FilterPluginConfig{} for _, rule := range yml { c := map[string]common.Config{} for name, ruleYml := range rule { ruleConfig, err := common.NewConfigFrom(ruleYml) assert.Nil(t, err) c[name] = *ruleConfig } config = append(config, c) } _, err := filter.New(config) assert.NotNil(t, err) }
// NewModule creates a new module func NewModule(cfg *common.Config, moduler func() Moduler) (*Module, error) { // Module config defaults config := ModuleConfig{ Period: "1s", Enabled: true, } err := cfg.Unpack(&config) if err != nil { return nil, err } filters, err := filter.New(config.Filters) if err != nil { return nil, fmt.Errorf("error initializing filters: %v", err) } logp.Debug("module", "Filters: %+v", filters) return &Module{ name: config.Module, Config: config, cfg: cfg, moduler: moduler(), metricSets: map[string]*MetricSet{}, Publish: make(chan common.MapStr), // TODO: What should be size of channel? @ruflin,20160316 wg: sync.WaitGroup{}, done: make(chan struct{}), filters: filters, }, nil }
// config reads the configuration file from disk, parses the common options // defined in BeatConfig, initializes logging, and set GOMAXPROCS if defined // in the config. Lastly it invokes the Config method implemented by the beat. func (bc *instance) config() error { var err error bc.data.RawConfig, err = cfgfile.Load("") if err != nil { return fmt.Errorf("error loading config file: %v", err) } err = bc.data.RawConfig.Unpack(&bc.data.Config) if err != nil { return fmt.Errorf("error unpacking config data: %v", err) } err = paths.InitPaths(&bc.data.Config.Path) if err != nil { return fmt.Errorf("error setting default paths: %v", err) } err = logp.Init(bc.data.Name, &bc.data.Config.Logging) if err != nil { return fmt.Errorf("error initializing logging: %v", err) } // Disable stderr logging if requested by cmdline flag logp.SetStderr() // log paths values to help with troubleshooting logp.Info(paths.Paths.String()) bc.data.filters, err = filter.New(bc.data.Config.Filters) if err != nil { return fmt.Errorf("error initializing filters: %v", err) } debugf("Filters: %+v", bc.data.filters) if bc.data.Config.Shipper.MaxProcs != nil { maxProcs := *bc.data.Config.Shipper.MaxProcs if maxProcs > 0 { runtime.GOMAXPROCS(maxProcs) } } return bc.beater.Config(bc.data) // TODO: If -configtest is set it should exit at this point. But changing // this now would mean a change in behavior. Some Beats may depend on the // Setup() method being invoked in order to do configuration validation. // If we do not change this, it means -configtest requires the outputs to // be available because the publisher is being started (this is not // desirable - elastic/beats#1213). It (may?) also cause the index template // to be loaded. }
// newModuleWrappers creates new Modules and their associated MetricSets based // on the given configuration. It constructs the supporting filters and // publisher client and stores it all in a moduleWrapper. func newModuleWrappers( modulesConfig []*common.Config, r *mb.Register, publisher *publisher.Publisher, ) ([]*moduleWrapper, error) { modules, err := mb.NewModules(modulesConfig, r) if err != nil { return nil, err } // Wrap the Modules and MetricSet's. var wrappers []*moduleWrapper var errs multierror.Errors for k, v := range modules { debugf("initializing Module type %s, %T=%+v", k.Name(), k, k) f, err := filter.New(k.Config().Filters) if err != nil { errs = append(errs, errors.Wrapf(err, "module %s", k.Name())) continue } mw := &moduleWrapper{ Module: k, filters: f, pubClient: publisher.Connect(), } wrappers = append(wrappers, mw) msws := make([]*metricSetWrapper, 0, len(v)) for _, ms := range v { debugf("initializing MetricSet type %s/%s, %T=%+v", ms.Module().Name(), ms.Name(), ms, ms) msw := &metricSetWrapper{ MetricSet: ms, module: mw, stats: new(expvar.Map).Init(), } msws = append(msws, msw) // Initialize expvar stats for this MetricSet. fetches.Set(fmt.Sprintf("%s-%s", mw.Name(), msw.Name()), msw.stats) msw.stats.Add(successesKey, 0) msw.stats.Add(failuresKey, 0) msw.stats.Add(eventsKey, 0) } mw.metricSets = msws } return wrappers, errs.Err() }
// NewModuleWrappers creates new Modules and their associated MetricSets based // on the given configuration. It constructs the supporting filters and stores // them all in a ModuleWrapper. func NewModuleWrappers(modulesConfig []*common.Config, r *mb.Register) ([]*ModuleWrapper, error) { modules, err := mb.NewModules(modulesConfig, r) if err != nil { return nil, err } // Wrap the Modules and MetricSet's. var wrappers []*ModuleWrapper var errs multierror.Errors for k, v := range modules { debugf("Initializing Module type '%s': %T=%+v", k.Name(), k, k) f, err := filter.New(k.Config().Filters) if err != nil { errs = append(errs, errors.Wrapf(err, "module %s", k.Name())) continue } mw := &ModuleWrapper{ Module: k, filters: f, } wrappers = append(wrappers, mw) msws := make([]*metricSetWrapper, 0, len(v)) for _, ms := range v { debugf("Initializing MetricSet type '%s/%s' for host '%s': %T=%+v", ms.Module().Name(), ms.Name(), ms.Host(), ms, ms) expMap, err := getMetricSetExpvarMap(mw.Name(), ms.Name()) if err != nil { return nil, err } msw := &metricSetWrapper{ MetricSet: ms, module: mw, stats: expMap, } msws = append(msws, msw) } mw.metricSets = msws } return wrappers, errs.Err() }
// config reads the configuration file from disk, parses the common options // defined in BeatConfig, initializes logging, and set GOMAXPROCS if defined // in the config. Lastly it invokes the Config method implemented by the beat. func (bc *instance) config() error { var err error bc.data.RawConfig, err = cfgfile.Load("") if err != nil { return fmt.Errorf("error loading config file: %v", err) } err = bc.data.RawConfig.Unpack(&bc.data.Config) if err != nil { return fmt.Errorf("error unpacking config data: %v", err) } err = paths.InitPaths(&bc.data.Config.Path) if err != nil { return fmt.Errorf("error setting default paths: %v", err) } err = logp.Init(bc.data.Name, &bc.data.Config.Logging) if err != nil { return fmt.Errorf("error initializing logging: %v", err) } // Disable stderr logging if requested by cmdline flag logp.SetStderr() // log paths values to help with troubleshooting logp.Info(paths.Paths.String()) bc.data.filters, err = filter.New(bc.data.Config.Filters) if err != nil { return fmt.Errorf("error initializing filters: %v", err) } debugf("Filters: %+v", bc.data.filters) if bc.data.Config.Shipper.MaxProcs != nil { maxProcs := *bc.data.Config.Shipper.MaxProcs if maxProcs > 0 { runtime.GOMAXPROCS(maxProcs) } } return bc.beater.Config(bc.data) }
func TestBadConfig(t *testing.T) { if testing.Verbose() { logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"}) } yml := []map[string]interface{}{ map[string]interface{}{ "include_fields": map[string]interface{}{ "contains": map[string]string{ "proc.name": "test", }, "fields": []string{"proc.cpu.total_p", "proc.mem", "dd"}, }, "drop_fields": map[string]interface{}{ "fields": []string{"proc.cpu"}, }, }, } config := filter.FilterPluginConfig{} for _, rule := range yml { c := map[string]common.Config{} for name, ruleYml := range rule { ruleConfig, err := common.NewConfigFrom(ruleYml) assert.Nil(t, err) c[name] = *ruleConfig } config = append(config, c) } _, err := filter.New(config) assert.NotNil(t, err) }
func GetFilters(t *testing.T, yml []map[string]interface{}) *filter.Filters { config := filter.FilterPluginConfig{} for _, rule := range yml { c := map[string]common.Config{} for name, ruleYml := range rule { ruleConfig, err := common.NewConfigFrom(ruleYml) assert.Nil(t, err) c[name] = *ruleConfig } config = append(config, c) } filters, err := filter.New(config) assert.Nil(t, err) return filters }