package graphite import ( "fmt" "strings" "github.com/influxdata/influxdb/models" ) 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 { 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 } func (c *Config) validateTemplates() error { // map to keep track of filters we see filters := map[string]struct{}{} for i, t := range c.Templates { parts := strings.Fields(t) // Ensure template string is non-empty if len(parts) == 0 { return fmt.Errorf("missing template at position: %d", i) } if len(parts) == 1 && parts[0] == "" { return fmt.Errorf("missing template at position: %d", i) } if len(parts) > 3 { return fmt.Errorf("invalid template format: '%s'", t) } template := t filter := "" tags := "" if len(parts) >= 2 { // We could have