Skip to content

Commit

Permalink
Updated Riemann output:
Browse files Browse the repository at this point in the history
* Customizable 'separator' option instead of hard-coded '_'

* String values are sent as "State" instead of "Metric", preventing
  Riemann from rejecting them

* Riemann service name is set to an (ugly) combination of input name &
  (sorted) tags' values...this allows connecting different events for
  the same input together on the Riemann side

closes #642
  • Loading branch information
Dhruv Bansal authored and sparrc committed Feb 9, 2016
1 parent c09884c commit b382a3f
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions plugins/outputs/riemann/riemann.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"os"
"sort"
"strings"

"github.com/amir/raidman"
"github.com/influxdata/telegraf"
Expand All @@ -13,6 +15,7 @@ import (
type Riemann struct {
URL string
Transport string
Separator string

client *raidman.Client
}
Expand All @@ -22,6 +25,8 @@ 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 = " "
`

func (r *Riemann) Connect() error {
Expand Down Expand Up @@ -55,7 +60,7 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error {

var events []*raidman.Event
for _, p := range metrics {
evs := buildEvents(p)
evs := buildEvents(p, r.Separator)
for _, ev := range evs {
events = append(events, ev)
}
Expand All @@ -70,7 +75,7 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error {
return nil
}

func buildEvents(p telegraf.Metric) []*raidman.Event {
func buildEvents(p telegraf.Metric, s string) []*raidman.Event {
events := []*raidman.Event{}
for fieldName, value := range p.Fields() {
host, ok := p.Tags()["host"]
Expand All @@ -85,15 +90,48 @@ func buildEvents(p telegraf.Metric) []*raidman.Event {

event := &raidman.Event{
Host: host,
Service: p.Name() + "_" + fieldName,
Metric: value,
Service: serviceName(s, p.Name(), p.Tags(), fieldName),
}

switch value.(type) {
case string:
event.State = value.(string)
default:
event.Metric = value
}

events = append(events, event)
}

return events
}

func serviceName(s string, n string, t map[string]string, f string) string {
serviceStrings := []string{}
serviceStrings = append(serviceStrings, n)

// we'll skip the 'host' tag
tagStrings := []string{}
tagNames := []string{}

for tagName := range t {
tagNames = append(tagNames, tagName)
}
sort.Strings(tagNames)

for _, tagName := range tagNames {
if tagName != "host" {
tagStrings = append(tagStrings, t[tagName])
}
}
var tagString string = strings.Join(tagStrings, s)
if tagString != "" {
serviceStrings = append(serviceStrings, tagString)
}
serviceStrings = append(serviceStrings, f)
return strings.Join(serviceStrings, s)
}

func init() {
outputs.Add("riemann", func() telegraf.Output {
return &Riemann{}
Expand Down

0 comments on commit b382a3f

Please sign in to comment.