send metric tags as riemann tags

allow sending measurement name as tag

Signed-off-by: Fabio Berchtold <fabio.berchtold@swisscom.com>
This commit is contained in:
JamesClonk 2016-10-04 12:18:34 +02:00
parent ce5054c850
commit 747f39a7da
3 changed files with 41 additions and 26 deletions

2
Godeps
View File

@ -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

View File

@ -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

View File

@ -15,6 +15,7 @@ type Riemann struct {
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"]
@ -102,12 +105,16 @@ func buildEvents(p telegraf.Metric, s string) []*raidman.Event {
event := &raidman.Event{
Host: host,
Service: serviceName(s, p.Name(), p.Tags(), fieldName),
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
}
serviceStrings = append(serviceStrings, f)
return strings.Join(serviceStrings, s)
func (r *Riemann) service(name string, field string) string {
var serviceStrings []string
if !r.MeasurementAsTag {
serviceStrings = append(serviceStrings, name)
}
serviceStrings = append(serviceStrings, field)
return strings.Join(serviceStrings, r.Separator)
}
func init() {