Skip to content

Commit

Permalink
graphite parser, handle multiple templates empty filter
Browse files Browse the repository at this point in the history
Previously, the graphite parser would simply overwrite any template that
had an identical filter to a previous template. This included the empty
filter.

This change automatically creates a "*." filter for any template
specified with an empty filter. This allows users to specify only a
template, and Telegraf will auto-match based on the most specific
template.

closes #1731
  • Loading branch information
sparrc committed Oct 10, 2016
1 parent d627bdb commit b0edff4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugins/parsers/graphite/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ func NewGraphiteParser(
}
}

// if the template did not have a filter defined for it, then create a
// dummy filter of all '*'
// ie,
// "measurement.host.metric" -> *.*.*
// "measurement.host.metric.metric.metric" -> *.*.*.*.*
// "measurement.host.metric.metric" -> *.*.*.*
if filter == "" {
stars := []string{}
for _, _ = range strings.Split(template, ".") {
stars = append(stars, "*")
}
filter = strings.Join(stars, ".")
}

// Parse out the default tags specific to this template
tags := map[string]string{}
if strings.Contains(parts[len(parts)-1], "=") {
Expand Down
25 changes: 25 additions & 0 deletions plugins/parsers/graphite/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,31 @@ func TestApplyTemplateGreedyField(t *testing.T) {
}
}

func TestApplyTemplateMostSpecificTemplate(t *testing.T) {
p, err := NewGraphiteParser(
".",
[]string{
"measurement.host.metric",
"measurement.host.metric.metric.metric",
"measurement.host.metric.metric",
},
nil,
)
assert.NoError(t, err)

measurement, tags, _, err := p.ApplyTemplate("net.server001.a.b.c 2")
assert.Equal(t, "net", measurement)
assert.Equal(t,
map[string]string{"host": "server001", "metric": "a.b.c"},
tags)

measurement, tags, _, err = p.ApplyTemplate("net.server001.a.b 2")
assert.Equal(t, "net", measurement)
assert.Equal(t,
map[string]string{"host": "server001", "metric": "a.b"},
tags)
}

// Test Helpers
func errstr(err error) string {
if err != nil {
Expand Down

0 comments on commit b0edff4

Please sign in to comment.