Ejemplo n.º 1
0
Archivo: file.go Proyecto: 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
}
Ejemplo n.º 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 (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
}
Ejemplo n.º 4
0
Archivo: mock.go Proyecto: 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
}
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
}
Ejemplo n.º 6
0
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
}