diff --git a/config/config_test.go b/config/config_test.go index 273e8fb3353af..9ee7aa21c903d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -475,6 +475,7 @@ func TestConfig_ParserInterfaceNewFormat(t *testing.T) { require.True(t, ok) // Get the parser set with 'SetParser()' if p, ok := input.Parser.(*models.RunningParser); ok { + require.NoError(t, p.Init()) actual = append(actual, p.Parser) } else { actual = append(actual, input.Parser) @@ -614,6 +615,7 @@ func TestConfig_ParserInterfaceOldFormat(t *testing.T) { require.True(t, ok) // Get the parser set with 'SetParser()' if p, ok := input.Parser.(*models.RunningParser); ok { + require.NoError(t, p.Init()) actual = append(actual, p.Parser) } else { actual = append(actual, input.Parser) diff --git a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go index b9e90d7c1983d..50987fb32cafd 100644 --- a/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go +++ b/plugins/inputs/kafka_consumer_legacy/kafka_consumer_legacy_test.go @@ -7,6 +7,7 @@ import ( "github.com/Shopify/sarama" "github.com/influxdata/telegraf/plugins/parsers" + "github.com/influxdata/telegraf/plugins/parsers/graphite" "github.com/influxdata/telegraf/plugins/parsers/json" "github.com/influxdata/telegraf/testutil" @@ -115,9 +116,9 @@ func TestRunParserAndGatherGraphite(t *testing.T) { k.acc = &acc defer close(k.done) - var err error - k.parser, err = parsers.NewGraphiteParser("_", []string{}, nil) - require.NoError(t, err) + p := graphite.Parser{Separator: "_", Templates: []string{}} + require.NoError(t, p.Init()) + k.parser = &p go k.receiver() in <- saramaMsg(testMsgGraphite) acc.Wait(1) diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index 6e5233b0de2a0..97dca4656062c 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -150,7 +150,7 @@ type Statsd struct { // Max duration for each metric to stay cached without being updated. MaxTTL config.Duration `toml:"max_ttl"` - graphiteParser *graphite.GraphiteParser + graphiteParser *graphite.Parser acc telegraf.Accumulator @@ -713,7 +713,8 @@ func (s *Statsd) parseName(bucket string) (name string, field string, tags map[s var err error if p == nil || s.graphiteParser.Separator != s.MetricSeparator { - p, err = graphite.NewGraphiteParser(s.MetricSeparator, s.Templates, nil) + p = &graphite.Parser{Separator: s.MetricSeparator, Templates: s.Templates} + err = p.Init() s.graphiteParser = p } diff --git a/plugins/inputs/tcp_listener/tcp_listener_test.go b/plugins/inputs/tcp_listener/tcp_listener_test.go index 0d2d958bed0da..a9f4707a19cd5 100644 --- a/plugins/inputs/tcp_listener/tcp_listener_test.go +++ b/plugins/inputs/tcp_listener/tcp_listener_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "github.com/influxdata/telegraf/plugins/parsers" + "github.com/influxdata/telegraf/plugins/parsers/graphite" "github.com/influxdata/telegraf/plugins/parsers/json" "github.com/influxdata/telegraf/testutil" ) @@ -293,9 +294,9 @@ func TestRunParserGraphiteMsg(t *testing.T) { listener.acc = &acc defer close(listener.done) - var err error - listener.parser, err = parsers.NewGraphiteParser("_", []string{}, nil) - require.NoError(t, err) + p := graphite.Parser{Separator: "_", Templates: []string{}} + require.NoError(t, p.Init()) + listener.parser = &p listener.wg.Add(1) go listener.tcpParser() diff --git a/plugins/parsers/all/all.go b/plugins/parsers/all/all.go index 0943ded6f5e01..b953730713bc3 100644 --- a/plugins/parsers/all/all.go +++ b/plugins/parsers/all/all.go @@ -5,6 +5,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/parsers/collectd" _ "github.com/influxdata/telegraf/plugins/parsers/csv" _ "github.com/influxdata/telegraf/plugins/parsers/form_urlencoded" + _ "github.com/influxdata/telegraf/plugins/parsers/graphite" _ "github.com/influxdata/telegraf/plugins/parsers/json" _ "github.com/influxdata/telegraf/plugins/parsers/json_v2" _ "github.com/influxdata/telegraf/plugins/parsers/value" diff --git a/plugins/parsers/graphite/parser.go b/plugins/parsers/graphite/parser.go index 954424cefcbc4..9490ea8c6f6af 100644 --- a/plugins/parsers/graphite/parser.go +++ b/plugins/parsers/graphite/parser.go @@ -12,6 +12,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal/templating" "github.com/influxdata/telegraf/metric" + "github.com/influxdata/telegraf/plugins/parsers" ) // Minimum and maximum supported dates for timestamps. @@ -20,45 +21,34 @@ var ( MaxDate = time.Date(2038, 1, 19, 0, 0, 0, 0, time.UTC) ) -type GraphiteParser struct { - Separator string - Templates []string - DefaultTags map[string]string - templateEngine *templating.Engine -} +type Parser struct { + Separator string `toml:"separator"` + Templates []string `toml:"templates"` + DefaultTags map[string]string ` toml:"-"` -func (p *GraphiteParser) SetDefaultTags(tags map[string]string) { - p.DefaultTags = tags + templateEngine *templating.Engine } -func NewGraphiteParser( - separator string, - templates []string, - defaultTags map[string]string, -) (*GraphiteParser, error) { - var err error - - if separator == "" { - separator = DefaultSeparator - } - p := &GraphiteParser{ - Separator: separator, - Templates: templates, +func (p *Parser) Init() error { + // Set defaults + if p.Separator == "" { + p.Separator = DefaultSeparator } - if defaultTags != nil { - p.DefaultTags = defaultTags + defaultTemplate, err := templating.NewDefaultTemplateWithPattern("measurement*") + if err != nil { + return fmt.Errorf("creating template failed: %w", err) } - defaultTemplate, _ := templating.NewDefaultTemplateWithPattern("measurement*") - p.templateEngine, err = templating.NewEngine(p.Separator, defaultTemplate, p.Templates) + p.templateEngine, err = templating.NewEngine(p.Separator, defaultTemplate, p.Templates) if err != nil { - return p, fmt.Errorf("exec input parser config is error: %s ", err.Error()) + return fmt.Errorf("creating template engine failed: %w ", err) } - return p, nil + + return nil } -func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) { +func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { // parse even if the buffer begins with a newline if len(buf) != 0 && buf[0] == '\n' { buf = buf[1:] @@ -95,7 +85,7 @@ func (p *GraphiteParser) Parse(buf []byte) ([]telegraf.Metric, error) { } // ParseLine performs Graphite parsing of a single line. -func (p *GraphiteParser) ParseLine(line string) (telegraf.Metric, error) { +func (p *Parser) ParseLine(line string) (telegraf.Metric, error) { // Break into 3 fields (name, value, timestamp). fields := strings.Fields(line) if len(fields) != 2 && len(fields) != 3 { @@ -178,7 +168,8 @@ func (p *GraphiteParser) ParseLine(line string) (telegraf.Metric, error) { // ApplyTemplate extracts the template fields from the given line and // returns the measurement name and tags. -func (p *GraphiteParser) ApplyTemplate(line string) (string, map[string]string, string, error) { +//nolint:revive // This function should be eliminated anyway +func (p *Parser) ApplyTemplate(line string) (string, map[string]string, string, error) { // Break line into fields (name, value, timestamp), only name is used fields := strings.Fields(line) if len(fields) == 0 { @@ -196,3 +187,19 @@ func (p *GraphiteParser) ApplyTemplate(line string) (string, map[string]string, return name, tags, field, err } + +func (p *Parser) SetDefaultTags(tags map[string]string) { + p.DefaultTags = tags +} + +func init() { + parsers.Add("graphite", func(_ string) telegraf.Parser { return &Parser{} }) +} + +func (p *Parser) InitFromConfig(config *parsers.Config) error { + p.Templates = append(p.Templates, config.Templates...) + p.Separator = config.Separator + p.DefaultTags = config.DefaultTags + + return p.Init() +} diff --git a/plugins/parsers/graphite/parser_test.go b/plugins/parsers/graphite/parser_test.go index b08f7cd0ec9c6..bb42ec937e675 100644 --- a/plugins/parsers/graphite/parser_test.go +++ b/plugins/parsers/graphite/parser_test.go @@ -14,20 +14,20 @@ import ( ) func BenchmarkParse(b *testing.B) { - p, err := NewGraphiteParser("_", []string{ - "*.* .wrong.measurement*", - "servers.* .host.measurement*", - "servers.localhost .host.measurement*", - "*.localhost .host.measurement*", - "*.*.cpu .host.measurement*", - "a.b.c .host.measurement*", - "influxd.*.foo .host.measurement*", - "prod.*.mem .host.measurement*", - }, nil) - - if err != nil { - b.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Separator: "_", + Templates: []string{ + "*.* .wrong.measurement*", + "servers.* .host.measurement*", + "servers.localhost .host.measurement*", + "*.localhost .host.measurement*", + "*.*.cpu .host.measurement*", + "a.b.c .host.measurement*", + "influxd.*.foo .host.measurement*", + "prod.*.mem .host.measurement*", + }, } + require.NoError(b, p.Init()) for i := 0; i < b.N; i++ { _, err := p.Parse([]byte("servers.localhost.cpu.load 11 1435077219")) @@ -122,34 +122,24 @@ func TestTemplateApply(t *testing.T) { for _, test := range tests { tmpl, err := templating.NewDefaultTemplateWithPattern(test.template) - if errstr(err) != test.err { - t.Fatalf("err does not match. expected %v, got %v", test.err, err) - } - if err != nil { - // If we erred out,it was intended and the following tests won't work + if test.err != "" { + require.EqualError(t, err, test.err) continue } + require.NoError(t, err) measurement, tags, _, _ := tmpl.Apply(test.input, DefaultSeparator) - if measurement != test.measurement { - t.Fatalf("name parse failer. expected %v, got %v", test.measurement, measurement) - } - if len(tags) != len(test.tags) { - t.Fatalf("unexpected number of tags. expected %v, got %v", test.tags, tags) - } + require.Equal(t, test.measurement, measurement) + require.Len(t, tags, len(test.tags)) for k, v := range test.tags { - if tags[k] != v { - t.Fatalf("unexpected tag value for tags[%s]. expected %q, got %q", k, v, tags[k]) - } + require.Equal(t, v, tags[k]) } } } func TestParseMissingMeasurement(t *testing.T) { - _, err := NewGraphiteParser("", []string{"a.b.c"}, nil) - if err == nil { - t.Fatalf("expected error creating parser, got nil") - } + p := Parser{Templates: []string{"a.b.c"}} + require.Error(t, p.Init()) } func TestParseLine(t *testing.T) { @@ -281,19 +271,16 @@ func TestParseLine(t *testing.T) { } for _, test := range tests { - p, err := NewGraphiteParser("", []string{test.template}, nil) - if err != nil { - t.Fatalf("unexpected error creating graphite parser: %v", err) - } + p := Parser{Templates: []string{test.template}} + require.NoError(t, p.Init()) m, err := p.ParseLine(test.input) - if errstr(err) != test.err { - t.Fatalf("err does not match. expected %v, got %v", test.err, err) - } - if err != nil { - // If we erred out,it was intended and the following tests won't work + if test.err != "" { + require.EqualError(t, err, test.err) continue } + require.NoError(t, err) + if m.Name() != test.measurement { t.Fatalf("name parse failer. expected %v, got %v", test.measurement, m.Name()) @@ -396,19 +383,16 @@ func TestParse(t *testing.T) { } for _, test := range tests { - p, err := NewGraphiteParser("", []string{test.template}, nil) - if err != nil { - t.Fatalf("unexpected error creating graphite parser: %v", err) - } + p := Parser{Templates: []string{test.template}} + require.NoError(t, p.Init()) metrics, err := p.Parse(test.input) - if errstr(err) != test.err { - t.Fatalf("err does not match. expected [%v], got [%v]", test.err, err) - } - if err != nil { - // If we erred out,it was intended and the following tests won't work + if test.err != "" { + require.EqualError(t, err, test.err) continue } + require.NoError(t, err) + if metrics[0].Name() != test.measurement { t.Fatalf("name parse failer. expected %v, got %v", test.measurement, metrics[0].Name()) @@ -430,8 +414,8 @@ func TestParse(t *testing.T) { } func TestParseNaN(t *testing.T) { - p, err := NewGraphiteParser("", []string{"measurement*"}, nil) - require.NoError(t, err) + p := Parser{Templates: []string{"measurement*"}} + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load NaN 1435077219") require.NoError(t, err) @@ -449,8 +433,8 @@ func TestParseNaN(t *testing.T) { } func TestParseInf(t *testing.T) { - p, err := NewGraphiteParser("", []string{"measurement*"}, nil) - require.NoError(t, err) + p := Parser{Templates: []string{"measurement*"}} + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load +Inf 1435077219") require.NoError(t, err) @@ -468,10 +452,8 @@ func TestParseInf(t *testing.T) { } func TestFilterMatchDefault(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement*"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + p := Parser{Templates: []string{"servers.localhost .host.measurement*"}} + require.NoError(t, p.Init()) exp := metric.New("miss.servers.localhost.cpu_load", map[string]string{}, @@ -485,10 +467,10 @@ func TestFilterMatchDefault(t *testing.T) { } func TestFilterMatchMultipleMeasurement(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement.measurement*"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Templates: []string{"servers.localhost .host.measurement.measurement*"}, } + require.NoError(t, p.Init()) exp := metric.New("cpu.cpu_load.10", map[string]string{"host": "localhost"}, @@ -502,11 +484,11 @@ func TestFilterMatchMultipleMeasurement(t *testing.T) { } func TestFilterMatchMultipleMeasurementSeparator(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"servers.localhost .host.measurement.measurement*"}, - nil, - ) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"servers.localhost .host.measurement.measurement*"}, + } + require.NoError(t, p.Init()) exp := metric.New("cpu_cpu_load_10", map[string]string{"host": "localhost"}, @@ -520,10 +502,8 @@ func TestFilterMatchMultipleMeasurementSeparator(t *testing.T) { } func TestFilterMatchSingle(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement*"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + p := Parser{Templates: []string{"servers.localhost .host.measurement*"}} + require.NoError(t, p.Init()) exp := metric.New("cpu_load", map[string]string{"host": "localhost"}, @@ -537,10 +517,10 @@ func TestFilterMatchSingle(t *testing.T) { } func TestParseNoMatch(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.*.cpu .host.measurement.cpu.measurement"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Templates: []string{"servers.*.cpu .host.measurement.cpu.measurement"}, } + require.NoError(t, p.Init()) exp := metric.New("servers.localhost.memory.VmallocChunk", map[string]string{}, @@ -554,10 +534,8 @@ func TestParseNoMatch(t *testing.T) { } func TestFilterMatchWildcard(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.* .host.measurement*"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + p := Parser{Templates: []string{"servers.* .host.measurement*"}} + require.NoError(t, p.Init()) exp := metric.New("cpu_load", map[string]string{"host": "localhost"}, @@ -571,12 +549,12 @@ func TestFilterMatchWildcard(t *testing.T) { } func TestFilterMatchExactBeforeWildcard(t *testing.T) { - p, err := NewGraphiteParser("", []string{ - "servers.* .wrong.measurement*", - "servers.localhost .host.measurement*"}, nil) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Templates: []string{ + "servers.* .wrong.measurement*", + "servers.localhost .host.measurement*"}, } + require.NoError(t, p.Init()) exp := metric.New("cpu_load", map[string]string{"host": "localhost"}, @@ -590,17 +568,16 @@ func TestFilterMatchExactBeforeWildcard(t *testing.T) { } func TestFilterMatchMostLongestFilter(t *testing.T) { - p, err := NewGraphiteParser("", []string{ - "*.* .wrong.measurement*", - "servers.* .wrong.measurement*", - "servers.localhost .wrong.measurement*", - "servers.localhost.cpu .host.resource.measurement*", // should match this - "*.localhost .wrong.measurement*", - }, nil) - - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Templates: []string{ + "*.* .wrong.measurement*", + "servers.* .wrong.measurement*", + "servers.localhost .wrong.measurement*", + "servers.localhost.cpu .host.resource.measurement*", // should match this + "*.localhost .wrong.measurement*", + }, } + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu.cpu_load 11 1435077219") require.NoError(t, err) @@ -615,16 +592,15 @@ func TestFilterMatchMostLongestFilter(t *testing.T) { } func TestFilterMatchMultipleWildcards(t *testing.T) { - p, err := NewGraphiteParser("", []string{ - "*.* .wrong.measurement*", - "servers.* .host.measurement*", // should match this - "servers.localhost .wrong.measurement*", - "*.localhost .wrong.measurement*", - }, nil) - - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Templates: []string{ + "*.* .wrong.measurement*", + "servers.* .host.measurement*", // should match this + "servers.localhost .wrong.measurement*", + "*.localhost .wrong.measurement*", + }, } + require.NoError(t, p.Init()) exp := metric.New("cpu_load", map[string]string{"host": "server01"}, @@ -638,14 +614,14 @@ func TestFilterMatchMultipleWildcards(t *testing.T) { } func TestParseDefaultTags(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement*"}, map[string]string{ - "region": "us-east", - "zone": "1c", - "host": "should not set", - }) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + p := Parser{Templates: []string{"servers.localhost .host.measurement*"}} + p.SetDefaultTags( + map[string]string{ + "region": "us-east", + "zone": "1c", + "host": "should not set", + }) + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load 11 1435077219") require.NoError(t, err) @@ -664,13 +640,13 @@ func TestParseDefaultTags(t *testing.T) { } func TestParseDefaultTemplateTags(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement* zone=1c"}, map[string]string{ - "region": "us-east", - "host": "should not set", - }) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + p := Parser{Templates: []string{"servers.localhost .host.measurement* zone=1c"}} + p.SetDefaultTags( + map[string]string{ + "region": "us-east", + "host": "should not set", + }) + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load 11 1435077219") require.NoError(t, err) @@ -689,16 +665,19 @@ func TestParseDefaultTemplateTags(t *testing.T) { } func TestParseDefaultTemplateTagsOverridGlobal(t *testing.T) { - p, err := NewGraphiteParser("", []string{"servers.localhost .host.measurement* zone=1c,region=us-east"}, map[string]string{ - "region": "shot not be set", - "host": "should not set", - }) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) + p := Parser{ + Separator: "", + Templates: []string{"servers.localhost .host.measurement* zone=1c,region=us-east"}, } + p.SetDefaultTags( + map[string]string{ + "region": "shot not be set", + "host": "should not set", + }, + ) + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load 11 1435077219") - _ = m require.NoError(t, err) value, ok := m.GetTag("host") @@ -715,15 +694,17 @@ func TestParseDefaultTemplateTagsOverridGlobal(t *testing.T) { } func TestParseTemplateWhitespace(t *testing.T) { - p, err := NewGraphiteParser("", - []string{"servers.localhost .host.measurement* zone=1c"}, + p := Parser{ + Templates: []string{ + "servers.localhost .host.measurement* zone=1c", + }, + } + p.SetDefaultTags( map[string]string{ "region": "us-east", "host": "should not set", }) - if err != nil { - t.Fatalf("unexpected error creating parser, got %v", err) - } + require.NoError(t, p.Init()) m, err := p.ParseLine("servers.localhost.cpu_load 11 1435077219") require.NoError(t, err) @@ -743,10 +724,11 @@ func TestParseTemplateWhitespace(t *testing.T) { // Test basic functionality of ApplyTemplate func TestApplyTemplate(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement"}, - nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement"}, + } + require.NoError(t, p.Init()) measurement, _, _, err := p.ApplyTemplate("current.users") require.NoError(t, err) @@ -755,10 +737,11 @@ func TestApplyTemplate(t *testing.T) { // Test basic functionality of ApplyTemplate func TestApplyTemplateNoMatch(t *testing.T) { - p, err := NewGraphiteParser(".", - []string{"foo.bar measurement.measurement"}, - nil) - require.NoError(t, err) + p := Parser{ + Separator: ".", + Templates: []string{"foo.bar measurement.measurement"}, + } + require.NoError(t, p.Init()) measurement, _, _, err := p.ApplyTemplate("current.users") require.NoError(t, err) @@ -767,12 +750,14 @@ func TestApplyTemplateNoMatch(t *testing.T) { // Test that most specific template is chosen func TestApplyTemplateSpecific(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{ + p := Parser{ + Separator: "_", + Templates: []string{ "current.* measurement.measurement", "current.*.* measurement.measurement.service", - }, nil) - require.NoError(t, err) + }, + } + require.NoError(t, p.Init()) measurement, tags, _, _ := p.ApplyTemplate("current.users.facebook") require.Equal(t, "current_users", measurement) @@ -787,9 +772,11 @@ func TestApplyTemplateSpecific(t *testing.T) { } func TestApplyTemplateTags(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement region=us-west"}, nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement region=us-west"}, + } + require.NoError(t, p.Init()) measurement, tags, _, _ := p.ApplyTemplate("current.users") require.Equal(t, "current_users", measurement) @@ -804,9 +791,11 @@ func TestApplyTemplateTags(t *testing.T) { } func TestApplyTemplateField(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement.field"}, nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement.field"}, + } + require.NoError(t, p.Init()) measurement, _, field, err := p.ApplyTemplate("current.users.logged_in") require.NoError(t, err) @@ -819,9 +808,11 @@ func TestApplyTemplateField(t *testing.T) { } func TestApplyTemplateMultipleFieldsTogether(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement.field.field"}, nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement.field.field"}, + } + require.NoError(t, p.Init()) measurement, _, field, err := p.ApplyTemplate("current.users.logged_in.ssh") require.NoError(t, err) @@ -834,9 +825,11 @@ func TestApplyTemplateMultipleFieldsTogether(t *testing.T) { } func TestApplyTemplateMultipleFieldsApart(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement.field.method.field"}, nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement.field.method.field"}, + } + require.NoError(t, p.Init()) measurement, _, field, err := p.ApplyTemplate("current.users.logged_in.ssh.total") require.NoError(t, err) @@ -849,9 +842,11 @@ func TestApplyTemplateMultipleFieldsApart(t *testing.T) { } func TestApplyTemplateGreedyField(t *testing.T) { - p, err := NewGraphiteParser("_", - []string{"current.* measurement.measurement.field*"}, nil) - require.NoError(t, err) + p := Parser{ + Separator: "_", + Templates: []string{"current.* measurement.measurement.field*"}, + } + require.NoError(t, p.Init()) measurement, _, field, err := p.ApplyTemplate("current.users.logged_in") require.NoError(t, err) @@ -864,54 +859,36 @@ func TestApplyTemplateGreedyField(t *testing.T) { } func TestApplyTemplateOverSpecific(t *testing.T) { - p, err := NewGraphiteParser( - ".", - []string{ - "measurement.host.metric.metric.metric", - }, - nil, - ) - require.NoError(t, err) + p := Parser{ + Separator: ".", + Templates: []string{"measurement.host.metric.metric.metric"}, + } + require.NoError(t, p.Init()) measurement, tags, _, err := p.ApplyTemplate("net.server001.a.b 2") require.NoError(t, err) require.Equal(t, "net", measurement) - require.Equal(t, - map[string]string{"host": "server001", "metric": "a.b"}, - tags) + require.Equal(t, map[string]string{"host": "server001", "metric": "a.b"}, tags) } func TestApplyTemplateMostSpecificTemplate(t *testing.T) { - p, err := NewGraphiteParser( - ".", - []string{ + p := Parser{ + Separator: ".", + Templates: []string{ "measurement.host.metric", "measurement.host.metric.metric.metric", "measurement.host.metric.metric", }, - nil, - ) - require.NoError(t, err) + } + require.NoError(t, p.Init()) measurement, tags, _, err := p.ApplyTemplate("net.server001.a.b.c 2") require.NoError(t, err) require.Equal(t, "net", measurement) - require.Equal(t, - map[string]string{"host": "server001", "metric": "a.b.c"}, - tags) + require.Equal(t, map[string]string{"host": "server001", "metric": "a.b.c"}, tags) measurement, tags, _, err = p.ApplyTemplate("net.server001.a.b 2") require.NoError(t, err) require.Equal(t, "net", measurement) - require.Equal(t, - map[string]string{"host": "server001", "metric": "a.b"}, - tags) -} - -// Test Helpers -func errstr(err error) string { - if err != nil { - return err.Error() - } - return "" + require.Equal(t, map[string]string{"host": "server001", "metric": "a.b"}, tags) } diff --git a/plugins/parsers/registry.go b/plugins/parsers/registry.go index fd2fc497d358a..59603bdbdc984 100644 --- a/plugins/parsers/registry.go +++ b/plugins/parsers/registry.go @@ -5,7 +5,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/parsers/dropwizard" - "github.com/influxdata/telegraf/plugins/parsers/graphite" "github.com/influxdata/telegraf/plugins/parsers/grok" "github.com/influxdata/telegraf/plugins/parsers/influx" "github.com/influxdata/telegraf/plugins/parsers/influx/influx_upstream" @@ -209,9 +208,6 @@ func NewParser(config *Config) (Parser, error) { } case "nagios": parser, err = NewNagiosParser() - case "graphite": - parser, err = NewGraphiteParser(config.Separator, - config.Templates, config.DefaultTags) case "dropwizard": parser, err = NewDropwizardParser( config.DropwizardMetricRegistryPath, @@ -289,14 +285,6 @@ func NewInfluxUpstreamParser() (Parser, error) { return influx_upstream.NewParser(), nil } -func NewGraphiteParser( - separator string, - templates []string, - defaultTags map[string]string, -) (Parser, error) { - return graphite.NewGraphiteParser(separator, templates, defaultTags) -} - func NewDropwizardParser( metricRegistryPath string, timePath string,