// newMetricSet instantiates a new MetricSet using the given configuration. // The ModuleFactory and MetricSetFactory are obtained from the global // Registry. func newMetricSet(t testing.TB, config interface{}) mb.MetricSet { c, err := common.NewConfigFrom(config) if err != nil { t.Fatal(err) } m, err := mb.NewModules([]*common.Config{c}, mb.Registry) if err != nil { t.Fatal(err) } if !assert.Len(t, m, 1) { t.FailNow() } var metricSet mb.MetricSet for _, v := range m { if !assert.Len(t, v, 1) { t.FailNow() } metricSet = v[0] break } if !assert.NotNil(t, metricSet) { t.FailNow() } return metricSet }
// 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() }
// TestConfigValidation validates that the configuration and the DSN are // validated when the MetricSet is created. func TestConfigValidation(t *testing.T) { tests := []struct { in interface{} err string }{ { // Missing 'hosts' in: map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, }, err: "missing required field accessing config", }, { // Invalid DSN in: map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, "hosts": []string{"127.0.0.1"}, }, err: "config error for host '127.0.0.1': invalid DSN: missing the slash separating the database name", }, { // Local unix socket in: map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, "hosts": []string{"user@unix(/path/to/socket)/"}, }, }, { // TCP on a remote host, e.g. Amazon RDS: in: map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, "hosts": []string{"id:password@tcp(your-amazonaws-uri.com:3306)/}"}, }, }, { // TCP on a remote host with user/pass specified separately in: map[string]interface{}{ "module": "mysql", "metricsets": []string{"status"}, "hosts": []string{"tcp(your-amazonaws-uri.com:3306)/}"}, "username": "******", "password": "******", }, }, } for i, test := range tests { c, err := common.NewConfigFrom(test.in) if err != nil { t.Fatal(err) } _, err = mb.NewModules([]*common.Config{c}, mb.Registry) if err != nil && test.err == "" { t.Errorf("unexpected error in testcase %d: %v", i, err) continue } if test.err != "" && assert.Error(t, err, "expected '%v' in testcase %d", test.err, i) { assert.Contains(t, err.Error(), test.err, "testcase %d", i) continue } } }