From 3e9cf6a3ea54c59b96df0a4f0ed66bb2cf1ae1a5 Mon Sep 17 00:00:00 2001 From: Henry Hu Date: Thu, 4 Feb 2016 14:14:34 +0800 Subject: [PATCH] Removed the Tags capability from the graphite parser and modified the exe description... --- internal/encoding/graphite/config.go | 26 -------------------------- internal/encoding/graphite/parser.go | 28 +++++++--------------------- plugins/inputs/exec/README.md | 24 ++++++++---------------- plugins/inputs/exec/config.go | 24 ++++++------------------ plugins/inputs/exec/exec.go | 23 ++++++++--------------- 5 files changed, 29 insertions(+), 96 deletions(-) diff --git a/internal/encoding/graphite/config.go b/internal/encoding/graphite/config.go index 283448e86..7a5c759e7 100644 --- a/internal/encoding/graphite/config.go +++ b/internal/encoding/graphite/config.go @@ -3,8 +3,6 @@ package graphite import ( "fmt" "strings" - - "github.com/influxdata/influxdb/models" ) const ( @@ -16,30 +14,15 @@ const ( // Config represents the configuration for Graphite endpoints. type Config struct { Separator string - Tags []string Templates []string } -// DefaultTags returns the config's tags. -func (c *Config) DefaultTags() models.Tags { - tags := models.Tags{} - for _, t := range c.Tags { - parts := strings.Split(t, "=") - tags[parts[0]] = parts[1] - } - return tags -} - // Validate validates the config's templates and tags. func (c *Config) Validate() error { if err := c.validateTemplates(); err != nil { return err } - if err := c.validateTags(); err != nil { - return err - } - return nil } @@ -110,15 +93,6 @@ func (c *Config) validateTemplates() error { return nil } -func (c *Config) validateTags() error { - for _, t := range c.Tags { - if err := c.validateTag(t); err != nil { - return err - } - } - return nil -} - func (c *Config) validateTemplate(template string) error { hasMeasurement := false for _, p := range strings.Split(template, ".") { diff --git a/internal/encoding/graphite/parser.go b/internal/encoding/graphite/parser.go index 23670e2e7..a86397b80 100644 --- a/internal/encoding/graphite/parser.go +++ b/internal/encoding/graphite/parser.go @@ -35,14 +35,12 @@ func init() { // Parser encapsulates a Graphite Parser. type Parser struct { matcher *matcher - tags models.Tags } // Options are configurable values that can be provided to a Parser type Options struct { - Separator string - Templates []string - DefaultTags models.Tags + Separator string + Templates []string } // NewParserWithOptions returns a graphite parser using the given options @@ -84,16 +82,15 @@ func NewParserWithOptions(options Options) (*Parser, error) { } matcher.Add(filter, tmpl) } - return &Parser{matcher: matcher, tags: options.DefaultTags}, nil + return &Parser{matcher: matcher}, nil } // NewParser returns a GraphiteParser instance. -func NewParser(templates []string, defaultTags models.Tags) (*Parser, error) { +func NewParser(templates []string) (*Parser, error) { return NewParserWithOptions( Options{ - Templates: templates, - DefaultTags: defaultTags, - Separator: DefaultSeparator, + Templates: templates, + Separator: DefaultSeparator, }) } @@ -182,12 +179,6 @@ func (p *Parser) Parse(line string) (telegraf.Metric, error) { } } - // Set the default tags on the point if they are not already set - for k, v := range p.tags { - if _, ok := tags[k]; !ok { - tags[k] = v - } - } return telegraf.NewMetric(measurement, tags, fieldValues, timestamp) } @@ -202,12 +193,7 @@ func (p *Parser) ApplyTemplate(line string) (string, map[string]string, string, // decode the name and tags template := p.matcher.Match(fields[0]) name, tags, field, err := template.Apply(fields[0]) - // Set the default tags on the point if they are not already set - for k, v := range p.tags { - if _, ok := tags[k]; !ok { - tags[k] = v - } - } + return name, tags, field, err } diff --git a/plugins/inputs/exec/README.md b/plugins/inputs/exec/README.md index 8f67a480a..a58b3dde6 100644 --- a/plugins/inputs/exec/README.md +++ b/plugins/inputs/exec/README.md @@ -44,19 +44,15 @@ and strings will be ignored. ### If matching multiple measurement files, this string will be used to join the matched values. #separator = "." - ### Default tags that will be added to all metrics. These can be overridden at the template level - ### or by tags extracted from metric - #tags = ["region=north-east", "zone=1c"] - ### Each template line requires a template pattern. It can have an optional ### filter before the template and separated by spaces. It can also have optional extra ### tags following the template. Multiple tags should be separated by commas and no spaces ### similar to the line protocol format. The can be only one default template. ### Templates support below format: - ### filter + template - ### filter + template + extra tag - ### filter + template with field key - ### default template. Ignore the first graphite component "servers" + ### 1. filter + template + ### 2. filter + template + extra tag + ### 3. filter + template with field key + ### 4. default template #templates = [ # "*.app env.service.resource.measurement", # "stats.* .host.measurement* region=us-west,agent=sensu", @@ -149,19 +145,15 @@ We can also change the data_format to "graphite" to use the metrics collecting s ### If matching multiple measurement files, this string will be used to join the matched values. separator = "." - ### Default tags that will be added to all metrics. These can be overridden at the template level - ### or by tags extracted from metric - tags = ["region=north-east", "zone=1c"] - ### Each template line requires a template pattern. It can have an optional ### filter before the template and separated by spaces. It can also have optional extra ### tags following the template. Multiple tags should be separated by commas and no spaces ### similar to the line protocol format. The can be only one default template. ### Templates support below format: - ### filter + template - ### filter + template + extra tag - ### filter + template with field key - ### default template. Ignore the first graphite component "servers" + ### 1. filter + template + ### 2. filter + template + extra tag + ### 3. filter + template with field key + ### 4. default template templates = [ "*.app env.service.resource.measurement", "stats.* .host.measurement* region=us-west,agent=sensu", diff --git a/plugins/inputs/exec/config.go b/plugins/inputs/exec/config.go index d2239b0c8..c4ad5d47f 100644 --- a/plugins/inputs/exec/config.go +++ b/plugins/inputs/exec/config.go @@ -4,12 +4,6 @@ import ( "github.com/influxdata/telegraf/internal/encoding/graphite" ) -const ( - // DefaultSeparator is the default join character to use when joining multiple - // measurment parts in a template. - DefaultSeparator = "." -) - // Config represents the configuration for Graphite endpoints. type Config struct { Commands []string @@ -17,21 +11,15 @@ type Config struct { } // New Config instance. -func NewConfig(commands, tags, templates []string, separator string) *Config { +func NewConfig(commands, templates []string, separator string) *Config { c := &Config{} + if separator == "" { + separator = graphite.DefaultSeparator + } + c.Commands = commands - c.Tags = tags c.Templates = templates c.Separator = separator + return c } - -// WithDefaults takes the given config and returns a new config with any required -// default values set. -func (c *Config) WithDefaults() *Config { - d := *c - if d.Separator == "" { - d.Separator = DefaultSeparator - } - return &d -} diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 6f17da932..efccc7033 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -32,19 +32,15 @@ const sampleConfig = ` ### If matching multiple measurement files, this string will be used to join the matched values. separator = "." - ### Default tags that will be added to all metrics. These can be overridden at the template level - ### or by tags extracted from metric - tags = ["region=north-east", "zone=1c"] - ### Each template line requires a template pattern. It can have an optional ### filter before the template and separated by spaces. It can also have optional extra ### tags following the template. Multiple tags should be separated by commas and no spaces ### similar to the line protocol format. The can be only one default template. ### Templates support below format: - ### filter + template - ### filter + template + extra tag - ### filter + template with field key - ### default template. Ignore the first graphite component "servers" + ### 1. filter + template + ### 2. filter + template + extra tag + ### 3. filter + template with field key + ### 4. default template templates = [ "*.app env.service.resource.measurement", "stats.* .host.measurement* region=us-west,agent=sensu", @@ -59,7 +55,6 @@ type Exec struct { DataFormat string Separator string - Tags []string Templates []string encodingParser *encoding.Parser @@ -125,8 +120,7 @@ func (e *Exec) initConfig() error { e.Commands = []string{e.Command} } - c := NewConfig(e.Commands, e.Tags, e.Templates, e.Separator) - c.WithDefaults() + c := NewConfig(e.Commands, e.Templates, e.Separator) if err := c.Validate(); err != nil { return fmt.Errorf("exec configuration is error: %s ", err.Error()) @@ -134,9 +128,8 @@ func (e *Exec) initConfig() error { e.config = c graphiteParser, err := graphite.NewParserWithOptions(graphite.Options{ - Templates: e.config.Templates, - DefaultTags: e.config.DefaultTags(), - Separator: e.config.Separator}) + Templates: e.config.Templates, + Separator: e.config.Separator}) if err != nil { return fmt.Errorf("exec input parser config is error: %s ", err.Error()) @@ -152,7 +145,7 @@ func (e *Exec) SampleConfig() string { } func (e *Exec) Description() string { - return "Read metrics from one or more commands that output graphite line protocol to stdout" + return "Read metrics from one or more commands that can output JSON, influx or graphite line protocol to stdout" } func (e *Exec) Gather(acc telegraf.Accumulator) error {