Support multiple templates for graphite serializers (#7136)
This commit is contained in:
@@ -22,6 +22,16 @@ 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*
|
||||
## 3. Default template
|
||||
# templates = [
|
||||
# "cpu tags.measurement.host.field",
|
||||
# "disk* measurement.field",
|
||||
# "host.measurement.tags.field"
|
||||
#]
|
||||
|
||||
## Support Graphite tags, recommended to enable when using Graphite 1.1 or later.
|
||||
# graphite_tag_support = false
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/filter"
|
||||
)
|
||||
|
||||
const DEFAULT_TEMPLATE = "host.tags.measurement.field"
|
||||
@@ -29,10 +30,16 @@ var (
|
||||
fieldDeleter = strings.NewReplacer(".FIELDNAME", "", "FIELDNAME.", "")
|
||||
)
|
||||
|
||||
type GraphiteTemplate struct {
|
||||
Filter filter.Filter
|
||||
Value string
|
||||
}
|
||||
|
||||
type GraphiteSerializer struct {
|
||||
Prefix string
|
||||
Template string
|
||||
TagSupport bool
|
||||
Templates []*GraphiteTemplate
|
||||
}
|
||||
|
||||
func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||
@@ -59,7 +66,15 @@ func (s *GraphiteSerializer) Serialize(metric telegraf.Metric) ([]byte, error) {
|
||||
out = append(out, point...)
|
||||
}
|
||||
default:
|
||||
bucket := SerializeBucketName(metric.Name(), metric.Tags(), s.Template, s.Prefix)
|
||||
template := s.Template
|
||||
for _, graphiteTemplate := range s.Templates {
|
||||
if graphiteTemplate.Filter.Match(metric.Name()) {
|
||||
template = graphiteTemplate.Value
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
bucket := SerializeBucketName(metric.Name(), metric.Tags(), template, s.Prefix)
|
||||
if bucket == "" {
|
||||
return out, nil
|
||||
}
|
||||
@@ -185,6 +200,45 @@ func SerializeBucketName(
|
||||
return prefix + "." + strings.Join(out, ".")
|
||||
}
|
||||
|
||||
func InitGraphiteTemplates(templates []string) ([]*GraphiteTemplate, string, error) {
|
||||
var graphiteTemplates []*GraphiteTemplate
|
||||
defaultTemplate := ""
|
||||
|
||||
for i, t := range templates {
|
||||
parts := strings.Fields(t)
|
||||
|
||||
if len(parts) == 0 {
|
||||
return nil, "", fmt.Errorf("missing template at position: %d", i)
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
if parts[0] == "" {
|
||||
return nil, "", fmt.Errorf("missing template at position: %d", i)
|
||||
} else {
|
||||
// Override default template
|
||||
defaultTemplate = t
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if len(parts) > 2 {
|
||||
return nil, "", fmt.Errorf("invalid template format: '%s'", t)
|
||||
}
|
||||
|
||||
tFilter, err := filter.Compile([]string{parts[0]})
|
||||
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
graphiteTemplates = append(graphiteTemplates, &GraphiteTemplate{
|
||||
Filter: tFilter,
|
||||
Value: parts[1],
|
||||
})
|
||||
}
|
||||
|
||||
return graphiteTemplates, defaultTemplate, nil
|
||||
}
|
||||
|
||||
// SerializeBucketNameWithTags will take the given measurement name and tags and
|
||||
// produce a graphite bucket. It will use the Graphite11Serializer.
|
||||
// http://graphite.readthedocs.io/en/latest/tags.html
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -68,6 +68,9 @@ type Config struct {
|
||||
// only supports Graphite
|
||||
Template string `toml:"template"`
|
||||
|
||||
// Templates same Template, but multiple
|
||||
Templates []string `toml:"templates"`
|
||||
|
||||
// Timestamp units to use for JSON formatted output
|
||||
TimestampUnits time.Duration `toml:"timestamp_units"`
|
||||
|
||||
@@ -104,7 +107,7 @@ func NewSerializer(config *Config) (Serializer, error) {
|
||||
case "influx":
|
||||
serializer, err = NewInfluxSerializerConfig(config)
|
||||
case "graphite":
|
||||
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template, config.GraphiteTagSupport)
|
||||
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template, config.GraphiteTagSupport, config.Templates)
|
||||
case "json":
|
||||
serializer, err = NewJsonSerializer(config.TimestampUnits)
|
||||
case "splunkmetric":
|
||||
@@ -188,10 +191,21 @@ func NewInfluxSerializer() (Serializer, error) {
|
||||
return influx.NewSerializer(), nil
|
||||
}
|
||||
|
||||
func NewGraphiteSerializer(prefix, template string, tag_support bool) (Serializer, error) {
|
||||
func NewGraphiteSerializer(prefix, template string, tag_support bool, templates []string) (Serializer, error) {
|
||||
graphiteTemplates, defaultTemplate, err := graphite.InitGraphiteTemplates(templates)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if defaultTemplate != "" {
|
||||
template = defaultTemplate
|
||||
}
|
||||
|
||||
return &graphite.GraphiteSerializer{
|
||||
Prefix: prefix,
|
||||
Template: template,
|
||||
TagSupport: tag_support,
|
||||
Templates: graphiteTemplates,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user