Skip to content

Commit

Permalink
modify graphite serializer to use first the name of the metric followed
Browse files Browse the repository at this point in the history
by the tags
  • Loading branch information
verterok committed Mar 28, 2016
1 parent a15fed3 commit a79c6bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
18 changes: 9 additions & 9 deletions plugins/outputs/librato/librato_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(0.0, "test1"),
&Gauge{
Name: "value1.test1.value",
Name: "test1.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 0.0,
},
Expand All @@ -95,7 +95,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(1.0, "test2"),
&Gauge{
Name: "value1.test2.value",
Name: "test2.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 1.0,
},
Expand All @@ -104,7 +104,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(10, "test3"),
&Gauge{
Name: "value1.test3.value",
Name: "test3.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 10.0,
},
Expand All @@ -113,7 +113,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(int32(112345), "test4"),
&Gauge{
Name: "value1.test4.value",
Name: "test4.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 112345.0,
},
Expand All @@ -122,7 +122,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(int64(112345), "test5"),
&Gauge{
Name: "value1.test5.value",
Name: "test5.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 112345.0,
},
Expand All @@ -131,7 +131,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric(float32(11234.5), "test6"),
&Gauge{
Name: "value1.test6.value",
Name: "test6.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 11234.5,
},
Expand All @@ -140,7 +140,7 @@ func TestBuildGauge(t *testing.T) {
{
testutil.TestMetric("11234.5", "test7"),
&Gauge{
Name: "value1.test7.value",
Name: "test7.value1.value",
MeasureTime: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 11234.5,
},
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestBuildGaugeWithSource(t *testing.T) {
{
pt1,
&Gauge{
Name: "192_168_0_1.value1.test1.value",
Name: "test1.192_168_0_1.value1.value",
MeasureTime: time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 0.0,
Source: "192.168.0.1",
Expand All @@ -200,7 +200,7 @@ func TestBuildGaugeWithSource(t *testing.T) {
{
pt2,
&Gauge{
Name: "192_168_0_1.value1.test1.value",
Name: "test1.192_168_0_1.value1.value",
MeasureTime: time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix(),
Value: 1.0,
},
Expand Down
30 changes: 21 additions & 9 deletions plugins/serializers/graphite/graphite.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ func (s *GraphiteSerializer) SerializeBucketName(metric telegraf.Metric, field_n
// Write graphite metric
var serializedBucketName string
if name == field_name {
serializedBucketName = fmt.Sprintf("%s.%s",
tag_str,
strings.Replace(name, ".", "_", -1))
serializedBucketName = fmt.Sprintf("%s", tag_str)

} else {
serializedBucketName = fmt.Sprintf("%s.%s.%s",
serializedBucketName = fmt.Sprintf("%s.%s",
tag_str,
strings.Replace(name, ".", "_", -1),
strings.Replace(field_name, ".", "_", -1))
}
if s.Prefix != "" {
Expand All @@ -69,16 +67,30 @@ func buildTags(metric telegraf.Metric) string {
sort.Strings(keys)

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


// escape ., / and " "
chars_to_escape := []string{".", "/", " "}

for i, k := range keys {
tag_value := strings.Replace(tags[k], ".", "_", -1)
tag_value := tags[k]
for _, should_escape := range chars_to_escape {
tag_value = strings.Replace(tag_value, should_escape, "_", -1)
}
if i == 0 {
tag_str += tag_value
} else {
Expand Down
26 changes: 13 additions & 13 deletions plugins/serializers/graphite/graphite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func TestGraphiteTags(t *testing.T) {
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)

tags1 := buildTags(m1)
tags1 := buildTags(m1, )
tags2 := buildTags(m2)
tags3 := buildTags(m3)

assert.Equal(t, "192_168_0_1", tags1)
assert.Equal(t, "192_168_0_1.first.second", tags2)
assert.Equal(t, "first.second", tags3)
assert.Equal(t, "192_168_0_1." + m1.Name(), tags1)
assert.Equal(t, "192_168_0_1." + m2.Name() + ".first.second", tags2)
assert.Equal(t, m3.Name() + ".first.second", tags3)
}

func TestSerializeMetricNoHost(t *testing.T) {
Expand All @@ -58,8 +58,8 @@ func TestSerializeMetricNoHost(t *testing.T) {
assert.NoError(t, err)

expS := []string{
fmt.Sprintf("cpu0.us-west-2.cpu.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("cpu0.us-west-2.cpu.usage_busy 8.5 %d", now.Unix()),
fmt.Sprintf("cpu.cpu0.us-west-2.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("cpu.cpu0.us-west-2.usage_busy 8.5 %d", now.Unix()),
}
sort.Strings(mS)
sort.Strings(expS)
Expand All @@ -85,8 +85,8 @@ func TestSerializeMetricHost(t *testing.T) {
assert.NoError(t, err)

expS := []string{
fmt.Sprintf("localhost.cpu0.us-west-2.cpu.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("localhost.cpu0.us-west-2.cpu.usage_busy 8.5 %d", now.Unix()),
fmt.Sprintf("localhost.cpu.cpu0.us-west-2.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("localhost.cpu.cpu0.us-west-2.usage_busy 8.5 %d", now.Unix()),
}
sort.Strings(mS)
sort.Strings(expS)
Expand All @@ -112,8 +112,8 @@ func TestSerializeMetricPrefix(t *testing.T) {
assert.NoError(t, err)

expS := []string{
fmt.Sprintf("prefix.localhost.cpu0.us-west-2.cpu.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("prefix.localhost.cpu0.us-west-2.cpu.usage_busy 8.5 %d", now.Unix()),
fmt.Sprintf("prefix.localhost.cpu.cpu0.us-west-2.usage_idle 91.5 %d", now.Unix()),
fmt.Sprintf("prefix.localhost.cpu.cpu0.us-west-2.usage_busy 8.5 %d", now.Unix()),
}
sort.Strings(mS)
sort.Strings(expS)
Expand All @@ -135,7 +135,7 @@ func TestSerializeBucketNameNoHost(t *testing.T) {
s := GraphiteSerializer{}
mS := s.SerializeBucketName(m, "usage_idle")

expS := fmt.Sprintf("cpu0.us-west-2.cpu.usage_idle")
expS := fmt.Sprintf("cpu.cpu0.us-west-2.usage_idle")
assert.Equal(t, expS, mS)
}

Expand All @@ -155,7 +155,7 @@ func TestSerializeBucketNameHost(t *testing.T) {
s := GraphiteSerializer{}
mS := s.SerializeBucketName(m, "usage_idle")

expS := fmt.Sprintf("localhost.cpu0.us-west-2.cpu.usage_idle")
expS := fmt.Sprintf("localhost.cpu.cpu0.us-west-2.usage_idle")
assert.Equal(t, expS, mS)
}

Expand All @@ -175,6 +175,6 @@ func TestSerializeBucketNamePrefix(t *testing.T) {
s := GraphiteSerializer{Prefix: "prefix"}
mS := s.SerializeBucketName(m, "usage_idle")

expS := fmt.Sprintf("prefix.localhost.cpu0.us-west-2.cpu.usage_idle")
expS := fmt.Sprintf("prefix.localhost.cpu.cpu0.us-west-2.usage_idle")
assert.Equal(t, expS, mS)
}

0 comments on commit a79c6bd

Please sign in to comment.