示例#1
0
文件: file.go 项目: IRCody/snap
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
}
示例#2
0
文件: mock.go 项目: IRCody/snap
// 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
}
示例#3
0
文件: passthru.go 项目: IRCody/snap
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
}