From 747f39a7da06843284595c5af3bc6dae2f438092 Mon Sep 17 00:00:00 2001 From: JamesClonk Date: Tue, 4 Oct 2016 12:18:34 +0200 Subject: [PATCH] send metric tags as riemann tags allow sending measurement name as tag Signed-off-by: Fabio Berchtold --- Godeps | 2 +- etc/telegraf.conf | 4 +- plugins/outputs/riemann/riemann.go | 61 ++++++++++++++++++------------ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/Godeps b/Godeps index 749d059ce..9916078bb 100644 --- a/Godeps +++ b/Godeps @@ -1,7 +1,7 @@ github.com/Shopify/sarama 8aadb476e66ca998f2f6bb3c993e9a2daa3666b9 github.com/Sirupsen/logrus 219c8cb75c258c552e999735be6df753ffc7afdc github.com/aerospike/aerospike-client-go 7f3a312c3b2a60ac083ec6da296091c52c795c63 -github.com/amir/raidman 53c1b967405155bfc8758557863bf2e14f814687 +github.com/amir/raidman c74861fe6a7bb8ede0a010ce4485bdbb4fc4c985 github.com/aws/aws-sdk-go 13a12060f716145019378a10e2806c174356b857 github.com/beorn7/perks 3ac7bf7a47d159a033b107610db8a1b6575507a4 github.com/cenkalti/backoff 4dc77674aceaabba2c7e3da25d4c823edfb73f99 diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 2ad0bcbae..d93002533 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -436,8 +436,10 @@ # url = "localhost:5555" # ## transport protocol to use either tcp or udp # transport = "tcp" -# ## separator to use between input name and field name in Riemann service name +# ## separator to use between measurement name and field name in Riemann service name # separator = " " +# ## set measurement name as a Riemann tag instead of prepending it to the Riemann service name +# measurement_as_tag = false diff --git a/plugins/outputs/riemann/riemann.go b/plugins/outputs/riemann/riemann.go index bc49a7191..5d430bae0 100644 --- a/plugins/outputs/riemann/riemann.go +++ b/plugins/outputs/riemann/riemann.go @@ -12,9 +12,10 @@ import ( ) type Riemann struct { - URL string - Transport string - Separator string + URL string + Transport string + Separator string + MeasurementAsTag bool client *raidman.Client } @@ -24,8 +25,10 @@ var sampleConfig = ` url = "localhost:5555" ## transport protocol to use either tcp or udp transport = "tcp" - ## separator to use between input name and field name in Riemann service name + ## separator to use between measurement name and field name in Riemann service name separator = " " + ## set measurement name as a Riemann tag instead of prepending it to the Riemann service name + measurement_as_tag = false ` func (r *Riemann) Connect() error { @@ -71,7 +74,7 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error { var events []*raidman.Event for _, p := range metrics { - evs := buildEvents(p, r.Separator) + evs := r.buildEvents(p) for _, ev := range evs { events = append(events, ev) } @@ -87,7 +90,7 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error { return nil } -func buildEvents(p telegraf.Metric, s string) []*raidman.Event { +func (r *Riemann) buildEvents(p telegraf.Metric) []*raidman.Event { events := []*raidman.Event{} for fieldName, value := range p.Fields() { host, ok := p.Tags()["host"] @@ -101,13 +104,17 @@ func buildEvents(p telegraf.Metric, s string) []*raidman.Event { } event := &raidman.Event{ - Host: host, - Service: serviceName(s, p.Name(), p.Tags(), fieldName), + Host: host, + Service: r.service(p.Name(), fieldName), + Tags: r.tags(p.Name(), p.Tags()), + Attributes: p.Tags(), + Time: p.Time().Unix(), } switch value.(type) { case string: - event.State = value.(string) + state := []byte(value.(string)) + event.State = string(state[:254]) // Riemann states must be less than 255 bytes, e.g. "ok", "warning", "critical" default: event.Metric = value } @@ -118,30 +125,36 @@ func buildEvents(p telegraf.Metric, s string) []*raidman.Event { return events } -func serviceName(s string, n string, t map[string]string, f string) string { - serviceStrings := []string{} - serviceStrings = append(serviceStrings, n) +func (r *Riemann) tags(name string, tags map[string]string) []string { + var tagNames, tagValues []string - // we'll skip the 'host' tag - tagStrings := []string{} - tagNames := []string{} - - for tagName := range t { + for tagName := range tags { tagNames = append(tagNames, tagName) } sort.Strings(tagNames) + if r.MeasurementAsTag { + tagValues = append(tagValues, name) + } + for _, tagName := range tagNames { - if tagName != "host" { - tagStrings = append(tagStrings, t[tagName]) + if tagName != "host" { // we'll skip the 'host' tag + tagValues = append(tagValues, tags[tagName]) } } - var tagString string = strings.Join(tagStrings, s) - if tagString != "" { - serviceStrings = append(serviceStrings, tagString) + + return tagValues +} + +func (r *Riemann) service(name string, field string) string { + var serviceStrings []string + + if !r.MeasurementAsTag { + serviceStrings = append(serviceStrings, name) } - serviceStrings = append(serviceStrings, f) - return strings.Join(serviceStrings, s) + serviceStrings = append(serviceStrings, field) + + return strings.Join(serviceStrings, r.Separator) } func init() {