func (f *filePublisher) Publish(metrics []plugin.Metric, config plugin.Config) error { log.SetFormatter(&log.TextFormatter{DisableTimestamp: true}) if _, err := config.GetBool(debug); err == nil { log.SetLevel(log.DebugLevel) } else { log.SetLevel(log.InfoLevel) } log.Debug("publishing started") filename, err := config.GetString("file") if err != nil { log.Error(err) return err } file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666) defer file.Close() if err != nil { log.Error(err) return err } log.WithFields(log.Fields{ "file": filename, "metrics-published-count": len(metrics), }).Debug("metrics published") w := bufio.NewWriter(file) for _, m := range metrics { formattedTags := formatMetricTagsAsString(m.Tags) w.WriteString(fmt.Sprintf("%v|%v|%v|%v\n", m.Timestamp, m.Namespace, m.Data, formattedTags)) } w.Flush() return nil }
// GetMetricTypes returns metric types for testing func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) { mts := []plugin.Metric{} if _, err := cfg.GetBool("test-fail"); err == nil { return mts, fmt.Errorf("testing") } if _, err := cfg.GetBool("test"); err == nil { mts = append(mts, plugin.Metric{ Namespace: plugin.NewNamespace("intel", "mock", "test"), Description: "mock description", Unit: "mock unit", }) } if _, err := cfg.GetBool("test-less"); err != nil { mts = append(mts, plugin.Metric{ Namespace: plugin.NewNamespace("intel", "mock", "foo"), Description: "mock description", Unit: "mock unit", }) } mts = append(mts, plugin.Metric{ Namespace: plugin.NewNamespace("intel", "mock", "bar"), Description: "mock description", Unit: "mock unit", }) mts = append(mts, plugin.Metric{ Namespace: plugin.NewNamespace("intel", "mock"). AddDynamicElement("host", "name of the host"). AddStaticElement("baz"), Description: "mock description", Unit: "mock unit", }) return mts, nil }
func (p *passthruProcessor) Process(metrics []plugin.Metric, config plugin.Config) ([]plugin.Metric, error) { log.SetFormatter(&log.TextFormatter{DisableTimestamp: true}) if _, err := config.GetBool(debug); err == nil { log.SetLevel(log.DebugLevel) } else { log.SetLevel(log.InfoLevel) } log.Debug("processing started") if _, err := config.GetBool("test"); err == nil { log.Debug("test configuration found") for idx, m := range metrics { if m.Namespace.Strings()[0] == "foo" { log.Print("found foo metric") metrics[idx].Data = 2 } } } return metrics, nil }