Add support for Graphite 1.1.x tags (#4165)
This commit is contained in:
committed by
Daniel Nelson
parent
703be4f124
commit
7660315e45
@@ -87,6 +87,35 @@ func TestSerializeMetricNoHost(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricNoHostWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"usage_idle": float64(91.5),
|
||||
"usage_busy": float64(8.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu.usage_idle;cpu=cpu0;datacenter=us-west-2 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu.usage_busy;cpu=cpu0;datacenter=us-west-2 8.5 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricHost(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
@@ -115,6 +144,36 @@ func TestSerializeMetricHost(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricHostWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"usage_idle": float64(91.5),
|
||||
"usage_busy": float64(8.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu.usage_idle;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu.usage_busy;cpu=cpu0;datacenter=us-west-2;host=localhost 8.5 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
// test that a field named "value" gets ignored.
|
||||
func TestSerializeValueField(t *testing.T) {
|
||||
now := time.Now()
|
||||
@@ -140,6 +199,32 @@ func TestSerializeValueField(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeValueFieldWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"value": float64(91.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||
}
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
// test that a field named "value" gets ignored in middle of template.
|
||||
func TestSerializeValueField2(t *testing.T) {
|
||||
now := time.Now()
|
||||
@@ -189,6 +274,28 @@ func TestSerializeValueString(t *testing.T) {
|
||||
assert.Equal(t, "", mS[0])
|
||||
}
|
||||
|
||||
func TestSerializeValueStringWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"value": "asdasd",
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "", mS[0])
|
||||
}
|
||||
|
||||
func TestSerializeValueBoolean(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
@@ -219,6 +326,36 @@ func TestSerializeValueBoolean(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeValueBooleanWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"enabled": true,
|
||||
"disabled": false,
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu.enabled;cpu=cpu0;datacenter=us-west-2;host=localhost 1 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu.disabled;cpu=cpu0;datacenter=us-west-2;host=localhost 0 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeValueUnsigned(t *testing.T) {
|
||||
now := time.Unix(0, 0)
|
||||
tags := map[string]string{}
|
||||
@@ -262,6 +399,32 @@ func TestSerializeFieldWithSpaces(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeFieldWithSpacesWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
`field\ with\ spaces`: float64(91.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||
}
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
// test that tags with spaces get fixed.
|
||||
func TestSerializeTagWithSpaces(t *testing.T) {
|
||||
now := time.Now()
|
||||
@@ -289,6 +452,32 @@ func TestSerializeTagWithSpaces(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeTagWithSpacesWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": `cpu\ 0`,
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
`field_with_spaces`: float64(91.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu.field_with_spaces;cpu=cpu_0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||
}
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
// test that a field named "value" gets ignored at beginning of template.
|
||||
func TestSerializeValueField3(t *testing.T) {
|
||||
now := time.Now()
|
||||
@@ -371,6 +560,37 @@ func TestSerializeMetricPrefix(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricPrefixWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
"host": "localhost",
|
||||
"cpu": "cpu0",
|
||||
"datacenter": "us-west-2",
|
||||
}
|
||||
fields := map[string]interface{}{
|
||||
"usage_idle": float64(91.5),
|
||||
"usage_busy": float64(8.5),
|
||||
}
|
||||
m, err := metric.New("cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
s := GraphiteSerializer{
|
||||
Prefix: "prefix",
|
||||
TagSupport: true,
|
||||
}
|
||||
buf, _ := s.Serialize(m)
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("prefix.cpu.usage_idle;cpu=cpu0;datacenter=us-west-2;host=localhost 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("prefix.cpu.usage_busy;cpu=cpu0;datacenter=us-west-2;host=localhost 8.5 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeBucketNameNoHost(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
@@ -579,6 +799,100 @@ func TestClean(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanWithTagsSupport(t *testing.T) {
|
||||
now := time.Unix(1234567890, 0)
|
||||
tests := []struct {
|
||||
name string
|
||||
metric_name string
|
||||
tags map[string]string
|
||||
fields map[string]interface{}
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
"Base metric",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost"},
|
||||
map[string]interface{}{"usage_busy": float64(8.5)},
|
||||
"cpu.usage_busy;host=localhost 8.5 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Dot and whitespace in tags",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "label.dot and space": "value with.dot"},
|
||||
map[string]interface{}{"usage_busy": float64(8.5)},
|
||||
"cpu.usage_busy;host=localhost;label.dot_and_space=value_with.dot 8.5 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Field with space",
|
||||
"system",
|
||||
map[string]string{"host": "localhost"},
|
||||
map[string]interface{}{"uptime_format": "20 days, 23:26"},
|
||||
"", // yes nothing. graphite don't serialize string fields
|
||||
},
|
||||
{
|
||||
"Allowed punct",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "tag": "-_:="},
|
||||
map[string]interface{}{"usage_busy": float64(10)},
|
||||
"cpu.usage_busy;host=localhost;tag=-_:= 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Special conversions to hyphen",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "tag": "/@*"},
|
||||
map[string]interface{}{"usage_busy": float64(10)},
|
||||
"cpu.usage_busy;host=localhost;tag=--- 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Special drop chars",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "tag": `\no slash`},
|
||||
map[string]interface{}{"usage_busy": float64(10)},
|
||||
"cpu.usage_busy;host=localhost;tag=no_slash 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Empty tag & value field",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost"},
|
||||
map[string]interface{}{"value": float64(10)},
|
||||
"cpu;host=localhost 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Unicode Letters allowed",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "tag": "μnicodε_letters"},
|
||||
map[string]interface{}{"value": float64(10)},
|
||||
"cpu;host=localhost;tag=μnicodε_letters 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Other Unicode not allowed",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "tag": "“☢”"},
|
||||
map[string]interface{}{"value": float64(10)},
|
||||
"cpu;host=localhost;tag=___ 10 1234567890\n",
|
||||
},
|
||||
{
|
||||
"Newline in tags",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost", "label": "some\nthing\nwith\nnewline"},
|
||||
map[string]interface{}{"usage_busy": float64(8.5)},
|
||||
"cpu.usage_busy;host=localhost;label=some_thing_with_newline 8.5 1234567890\n",
|
||||
},
|
||||
}
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m, err := metric.New(tt.metric_name, tt.tags, tt.fields, now)
|
||||
assert.NoError(t, err)
|
||||
actual, _ := s.Serialize(m)
|
||||
require.Equal(t, tt.expected, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializeBatch(t *testing.T) {
|
||||
now := time.Unix(1234567890, 0)
|
||||
tests := []struct {
|
||||
@@ -607,3 +921,34 @@ func TestSerializeBatch(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerializeBatchWithTagsSupport(t *testing.T) {
|
||||
now := time.Unix(1234567890, 0)
|
||||
tests := []struct {
|
||||
name string
|
||||
metric_name string
|
||||
tags map[string]string
|
||||
fields map[string]interface{}
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
"Base metric",
|
||||
"cpu",
|
||||
map[string]string{"host": "localhost"},
|
||||
map[string]interface{}{"usage_busy": float64(8.5)},
|
||||
"cpu.usage_busy;host=localhost 8.5 1234567890\ncpu.usage_busy;host=localhost 8.5 1234567890\n",
|
||||
},
|
||||
}
|
||||
|
||||
s := GraphiteSerializer{
|
||||
TagSupport: true,
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m, err := metric.New(tt.metric_name, tt.tags, tt.fields, now)
|
||||
assert.NoError(t, err)
|
||||
actual, _ := s.SerializeBatch([]telegraf.Metric{m, m})
|
||||
require.Equal(t, tt.expected, string(actual))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user