예제 #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
func TestFilePublish(t *testing.T) {
	metrics := []plugin.Metric{plugin.Metric{Namespace: plugin.NewNamespace("foo"), Timestamp: time.Now(), Data: 99, Tags: nil}}
	config := plugin.Config{"file": "/tmp/pub.out"}
	Convey("TestFilePublish", t, func() {
		fp := NewFilePublisher()
		So(fp, ShouldNotBeNil)
		err := fp.Publish(metrics, config)
		So(err, ShouldBeNil)
		filename, _ := config.GetString("file")
		_, err = os.Stat(filename)
		So(err, ShouldBeNil)
	})
}
func getLogger(cfg plugin.Config) *log.Entry {
	logger := log.WithFields(log.Fields{
		"plugin-name":    Name,
		"plugin-version": Version,
		"plugin-type":    "publisher",
	})

	log.SetLevel(log.WarnLevel)

	levelValue, err := cfg.GetString("log-level")
	if err == nil {
		if level, err := log.ParseLevel(strings.ToLower(levelValue)); err == nil {
			log.SetLevel(level)
		} else {
			log.WithFields(log.Fields{
				"value":             strings.ToLower(levelValue),
				"acceptable values": "warn, error, debug, info",
			}).Warn("Invalid config value")
		}
	}
	return logger
}
func (f *GraphitePublisher) Publish(metrics []plugin.Metric, cfg plugin.Config) error {
	logger := getLogger(cfg)
	logger.Debug("Publishing started")
	var tagsForPrefix []string

	logger.Debug("publishing %v metrics to %v", len(metrics), cfg)
	server, err := cfg.GetString("server")
	if err != nil {
		return err
	}
	port, err := cfg.GetInt("port")
	if err != nil {
		return err
	}
	tagConfigs, err := cfg.GetString("prefix_tags")
	if err == nil {
		tagsForPrefix = strings.Split(tagConfigs, ",")
	}
	pre, err := cfg.GetString("prefix")
	if err != nil {
		pre = ""
	}

	logger.Debug("Attempting to connect to %s:%d", server, port)
	gite, err := graphite.NewGraphiteWithMetricPrefix(server, int(port), pre)
	if err != nil {
		logger.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
		return fmt.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
	}
	defer gite.Disconnect()
	logger.Debug("Connected to %s:%s successfully", server, port)

	giteMetrics := make([]graphite.Metric, len(metrics))
	for i, m := range metrics {
		key := strings.Join(m.Namespace.Strings(), ".")
		for _, tag := range tagsForPrefix {
			nextTag, ok := m.Tags[tag]
			if ok {
				key = nextTag + "." + key
			}
		}
		data := fmt.Sprintf("%v", m.Data)
		logger.Debug("Metric ready to send %s:%s", key, data)
		giteMetrics[i] = graphite.NewMetric(key, data, m.Timestamp.Unix())
	}

	err = gite.SendMetrics(giteMetrics)
	if err != nil {
		logger.Errorf("Unable to send metrics. Error: %s", err)
		return fmt.Errorf("Unable to send metrics. Error: %s", err)
	}
	logger.Debug("Metrics sent to Graphite.")

	return nil
}