Add configurable separator graphite serializer and output (#7545)

This commit is contained in:
ihard
2020-05-21 03:15:18 +03:00
committed by GitHub
parent 10560e5a10
commit 94c75b51a8
10 changed files with 327 additions and 8 deletions

View File

@@ -22,7 +22,7 @@ method is used, otherwise the [Template Pattern](templates) is used.
prefix = "telegraf"
## Graphite template pattern
template = "host.tags.measurement.field"
## Graphite templates patterns
## 1. Template for cpu
## 2. Template for disk*
@@ -35,6 +35,8 @@ method is used, otherwise the [Template Pattern](templates) is used.
## Support Graphite tags, recommended to enable when using Graphite 1.1 or later.
# graphite_tag_support = false
## Character for separating metric name and field for Graphite tags
# graphite_separator = "."
```
#### graphite_tag_support
@@ -54,5 +56,12 @@ cpu,cpu=cpu-total,dc=us-east-1,host=tars usage_idle=98.09,usage_user=0.89 145532
cpu.usage_user;cpu=cpu-total;dc=us-east-1;host=tars 0.89 1455320690
cpu.usage_idle;cpu=cpu-total;dc=us-east-1;host=tars 98.09 1455320690
```
With set option `graphite_separator` to "_"
```
cpu,cpu=cpu-total,dc=us-east-1,host=tars usage_idle=98.09,usage_user=0.89 1455320660004257758
=>
cpu_usage_user;cpu=cpu-total;dc=us-east-1;host=tars 0.89 1455320690
cpu_usage_idle;cpu=cpu-total;dc=us-east-1;host=tars 98.09 1455320690
```
[templates]: /docs/TEMPLATE_PATTERN.md

View File

@@ -39,6 +39,7 @@ type GraphiteSerializer struct {
Prefix string
Template string
TagSupport bool
Separator string
Templates []*GraphiteTemplate
}
@@ -55,7 +56,7 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
if fieldValue == "" {
continue
}
bucket := SerializeBucketNameWithTags(metric.Name(), metric.Tags(), s.Prefix, fieldName)
bucket := SerializeBucketNameWithTags(metric.Name(), metric.Tags(), s.Prefix, s.Separator, fieldName)
metricString := fmt.Sprintf("%s %s %d\n",
// insert "field" section of template
bucket,
@@ -246,6 +247,7 @@ func SerializeBucketNameWithTags(
measurement string,
tags map[string]string,
prefix string,
separator string,
field string,
) string {
var out string
@@ -259,13 +261,13 @@ func SerializeBucketNameWithTags(
sort.Strings(tagsCopy)
if prefix != "" {
out = prefix + "."
out = prefix + separator
}
out += measurement
if field != "value" {
out += "." + field
out += separator + field
}
out = sanitize(out)

View File

@@ -102,6 +102,7 @@ func TestSerializeMetricNoHostWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -251,6 +252,7 @@ func TestSerializeMetricHostWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -305,6 +307,7 @@ func TestSerializeValueFieldWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -380,6 +383,7 @@ func TestSerializeValueStringWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -433,6 +437,7 @@ func TestSerializeValueBooleanWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -505,6 +510,7 @@ func TestSerializeFieldWithSpacesWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -558,6 +564,7 @@ func TestSerializeTagWithSpacesWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -668,6 +675,7 @@ func TestSerializeMetricPrefixWithTagSupport(t *testing.T) {
s := GraphiteSerializer{
Prefix: "prefix",
TagSupport: true,
Separator: ".",
}
buf, _ := s.Serialize(m)
mS := strings.Split(strings.TrimSpace(string(buf)), "\n")
@@ -973,6 +981,7 @@ func TestCleanWithTagsSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -1033,6 +1042,7 @@ func TestSerializeBatchWithTagsSupport(t *testing.T) {
s := GraphiteSerializer{
TagSupport: true,
Separator: ".",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {