Skip to content

Commit

Permalink
Include all tag values in graphite output
Browse files Browse the repository at this point in the history
closes #595
  • Loading branch information
sparrc committed Jan 28, 2016
1 parent c549ab9 commit 4ad1bcb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
48 changes: 40 additions & 8 deletions plugins/outputs/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"math/rand"
"net"
"sort"
"strings"
"time"
)
Expand Down Expand Up @@ -70,26 +71,27 @@ func (g *Graphite) Description() string {
func (g *Graphite) Write(metrics []telegraf.Metric) error {
// Prepare data
var bp []string
for _, point := range metrics {
for _, metric := range metrics {
// Get name
name := point.Name()
name := metric.Name()
// Convert UnixNano to Unix timestamps
timestamp := point.UnixNano() / 1000000000
timestamp := metric.UnixNano() / 1000000000
tag_str := buildTags(metric)

for field_name, value := range point.Fields() {
for field_name, value := range metric.Fields() {
// Convert value
value_str := fmt.Sprintf("%#v", value)
// Write graphite point
// Write graphite metric
var graphitePoint string
if name == field_name {
graphitePoint = fmt.Sprintf("%s.%s %s %d\n",
strings.Replace(point.Tags()["host"], ".", "_", -1),
tag_str,
strings.Replace(name, ".", "_", -1),
value_str,
timestamp)
} else {
graphitePoint = fmt.Sprintf("%s.%s.%s %s %d\n",
strings.Replace(point.Tags()["host"], ".", "_", -1),
tag_str,
strings.Replace(name, ".", "_", -1),
strings.Replace(field_name, ".", "_", -1),
value_str,
Expand All @@ -99,7 +101,6 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
graphitePoint = fmt.Sprintf("%s.%s", g.Prefix, graphitePoint)
}
bp = append(bp, graphitePoint)
//fmt.Printf(graphitePoint)
}
}
graphitePoints := strings.Join(bp, "")
Expand Down Expand Up @@ -127,6 +128,37 @@ func (g *Graphite) Write(metrics []telegraf.Metric) error {
return err
}

func buildTags(metric telegraf.Metric) string {
var keys []string
tags := metric.Tags()
for k := range tags {
if k == "host" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)

var tag_str string
if host, ok := tags["host"]; ok {
if len(keys) > 0 {
tag_str = strings.Replace(host, ".", "_", -1) + "."
} else {
tag_str = strings.Replace(host, ".", "_", -1)
}
}

for i, k := range keys {
tag_value := strings.Replace(tags[k], ".", "_", -1)
if i == 0 {
tag_str += tag_value
} else {
tag_str += "." + tag_value
}
}
return tag_str
}

func init() {
outputs.Add("graphite", func() telegraf.Output {
return &Graphite{}
Expand Down
37 changes: 29 additions & 8 deletions plugins/outputs/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func TestGraphiteError(t *testing.T) {
Prefix: "my.prefix",
}
// Init metrics
pt1, _ := telegraf.NewMetric(
m1, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"mymeasurement": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
// Prepare point list
var metrics []telegraf.Metric
metrics = append(metrics, pt1)
metrics = append(metrics, m1)
// Error
err1 := g.Connect()
require.NoError(t, err1)
Expand All @@ -45,29 +45,29 @@ func TestGraphiteOK(t *testing.T) {
Prefix: "my.prefix",
}
// Init metrics
pt1, _ := telegraf.NewMetric(
m1, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"mymeasurement": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
pt2, _ := telegraf.NewMetric(
m2, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
pt3, _ := telegraf.NewMetric(
m3, _ := telegraf.NewMetric(
"my_measurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
// Prepare point list
var metrics []telegraf.Metric
metrics = append(metrics, pt1)
metrics = append(metrics, pt2)
metrics = append(metrics, pt3)
metrics = append(metrics, m1)
metrics = append(metrics, m2)
metrics = append(metrics, m3)
// Start TCP server
wg.Add(1)
go TCPServer(t, &wg)
Expand Down Expand Up @@ -102,3 +102,24 @@ func TCPServer(t *testing.T, wg *sync.WaitGroup) {
conn.Close()
wg.Done()
}

func TestGraphiteTags(t *testing.T) {
m1, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
m2, _ := telegraf.NewMetric(
"mymeasurement",
map[string]string{"host": "192.168.0.1", "afoo": "first", "bfoo": "second"},
map[string]interface{}{"value": float64(3.14)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)

tags1 := buildTags(m1)
tags2 := buildTags(m2)

assert.Equal(t, "192_168_0_1", tags1)
assert.Equal(t, "192_168_0_1.first.second", tags2)
}

0 comments on commit 4ad1bcb

Please sign in to comment.