Support multiple templates for graphite serializers (#7136)
This commit is contained in:
@@ -144,6 +144,97 @@ func TestSerializeMetricHost(t *testing.T) {
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricHostWithMultipleTemplates(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),
|
||||
}
|
||||
m1, err := metric.New("cpu", tags, fields, now)
|
||||
m2, err := metric.New("new_cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
templates, defaultTemplate, err := InitGraphiteTemplates([]string{
|
||||
"cp* tags.measurement.host.field",
|
||||
"new_cpu tags.host.measurement.field",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, defaultTemplate, "")
|
||||
|
||||
s := GraphiteSerializer{
|
||||
Templates: templates,
|
||||
}
|
||||
|
||||
buf, _ := s.Serialize(m1)
|
||||
buf2, _ := s.Serialize(m2)
|
||||
|
||||
buf = append(buf, buf2...)
|
||||
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_busy 8.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.localhost.new_cpu.usage_idle 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.localhost.new_cpu.usage_busy 8.5 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricHostWithMultipleTemplatesWithDefault(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),
|
||||
}
|
||||
m1, err := metric.New("cpu", tags, fields, now)
|
||||
m2, err := metric.New("new_cpu", tags, fields, now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
templates, defaultTemplate, err := InitGraphiteTemplates([]string{
|
||||
"cp* tags.measurement.host.field",
|
||||
"tags.host.measurement.field",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, defaultTemplate, "tags.host.measurement.field")
|
||||
|
||||
s := GraphiteSerializer{
|
||||
Templates: templates,
|
||||
Template: defaultTemplate,
|
||||
}
|
||||
|
||||
buf, _ := s.Serialize(m1)
|
||||
buf2, _ := s.Serialize(m2)
|
||||
|
||||
buf = append(buf, buf2...)
|
||||
|
||||
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
expS := []string{
|
||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_idle 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.cpu.localhost.usage_busy 8.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.localhost.new_cpu.usage_idle 91.5 %d", now.Unix()),
|
||||
fmt.Sprintf("cpu0.us-west-2.localhost.new_cpu.usage_busy 8.5 %d", now.Unix()),
|
||||
}
|
||||
sort.Strings(mS)
|
||||
sort.Strings(expS)
|
||||
assert.Equal(t, expS, mS)
|
||||
}
|
||||
|
||||
func TestSerializeMetricHostWithTagSupport(t *testing.T) {
|
||||
now := time.Now()
|
||||
tags := map[string]string{
|
||||
|
||||
Reference in New Issue
Block a user