Skip to content

Commit

Permalink
Allow users to specify outputs as lists
Browse files Browse the repository at this point in the history
This will provide the ability to specify multiple outputs for a single
type of output.

In essence, allowing this:

[outputs]

[[outputs.influxdb]]
  urls = ["udp://localhost:8089"]
  database = "udp-telegraf"

[[outputs.influxdb]]
  urls = ["http://myhost:8086"]
  database = "telegraf"

[[outputs.kafka]]
  brokers = ["192.168.99.100:9092"]
  topic = "telegraf"

closes #335
  • Loading branch information
sparrc committed Nov 13, 2015
1 parent bf8e0f4 commit 3a1b15a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
17 changes: 2 additions & 15 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,11 @@ func (a *Agent) Close() error {
func (a *Agent) LoadOutputs(filters []string, config *Config) ([]string, error) {
var names []string

for _, name := range config.OutputsDeclared() {
creator, ok := outputs.Outputs[name]
if !ok {
return nil, fmt.Errorf("Undefined but requested output: %s", name)
}

for name, output := range config.OutputsDeclared() {
if sliceContains(name, filters) || len(filters) == 0 {
if a.Debug {
log.Println("Output Enabled: ", name)
}
output := creator()

err := config.ApplyOutput(name, output)
if err != nil {
Expand All @@ -178,15 +172,8 @@ func (a *Agent) LoadOutputs(filters []string, config *Config) ([]string, error)
func (a *Agent) LoadPlugins(filters []string, config *Config) ([]string, error) {
var names []string

for _, name := range config.PluginsDeclared() {
creator, ok := plugins.Plugins[name]
if !ok {
return nil, fmt.Errorf("Undefined but requested plugin: %s", name)
}

for name, plugin := range config.PluginsDeclared() {
if sliceContains(name, filters) || len(filters) == 0 {
plugin := creator()

config, err := config.ApplyPlugin(name, plugin)
if err != nil {
return nil, err
Expand Down
59 changes: 31 additions & 28 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,13 @@ func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, err
// Couldn't figure out how to get this to work with the declared function.

// PluginsDeclared returns the name of all plugins declared in the config.
func (c *Config) PluginsDeclared() []string {
var names []string
for name := range c.plugins {
names = append(names, name)
}
sort.Strings(names)
return names
func (c *Config) PluginsDeclared() map[string]plugins.Plugin {
return c.plugins
}

// OutputsDeclared returns the name of all outputs declared in the config.
func (c *Config) OutputsDeclared() []string {
var names []string
for name := range c.outputs {
names = append(names, name)
}
sort.Strings(names)
return names
func (c *Config) OutputsDeclared() map[string]outputs.Output {
return c.outputs
}

// ListTags returns a string of tags specified in the config,
Expand Down Expand Up @@ -283,7 +273,7 @@ func PrintSampleConfig(pluginFilters []string, outputFilters []string) {
creator := outputs.Outputs[oname]
output := creator()

fmt.Printf("\n# %s\n[outputs.%s]", output.Description(), oname)
fmt.Printf("\n# %s\n[[outputs.%s]]", output.Description(), oname)

config := output.SampleConfig()
if config == "" {
Expand Down Expand Up @@ -394,20 +384,24 @@ func findField(fieldName string, value reflect.Value) reflect.Value {
return reflect.Value{}
}

// A very limited merge. Merges the fields named in the fields parameter, replacing most values, but appending to arrays.
// A very limited merge. Merges the fields named in the fields parameter,
// replacing most values, but appending to arrays.
func mergeStruct(base, overlay interface{}, fields []string) error {
baseValue := reflect.ValueOf(base).Elem()
overlayValue := reflect.ValueOf(overlay).Elem()
if baseValue.Kind() != reflect.Struct {
return fmt.Errorf("Tried to merge something that wasn't a struct: type %v was %v", baseValue.Type(), baseValue.Kind())
return fmt.Errorf("Tried to merge something that wasn't a struct: type %v was %v",
baseValue.Type(), baseValue.Kind())
}
if baseValue.Type() != overlayValue.Type() {
return fmt.Errorf("Tried to merge two different types: %v and %v", baseValue.Type(), overlayValue.Type())
return fmt.Errorf("Tried to merge two different types: %v and %v",
baseValue.Type(), overlayValue.Type())
}
for _, field := range fields {
overlayFieldValue := findField(field, overlayValue)
if !overlayFieldValue.IsValid() {
return fmt.Errorf("could not find field in %v matching %v", overlayValue.Type(), field)
return fmt.Errorf("could not find field in %v matching %v",
overlayValue.Type(), field)
}
baseFieldValue := findField(field, baseValue)
if overlayFieldValue.Kind() == reflect.Slice {
Expand Down Expand Up @@ -536,13 +530,22 @@ func LoadConfig(path string) (*Config, error) {
}
case "outputs":
for outputName, outputVal := range subtbl.Fields {
outputSubtbl, ok := outputVal.(*ast.Table)
if !ok {
return nil, err
}
err = c.parseOutput(outputName, outputSubtbl)
if err != nil {
return nil, err
switch outputSubtbl := outputVal.(type) {
case *ast.Table:
err = c.parseOutput(outputName, outputSubtbl, 0)
if err != nil {
return nil, err
}
case []*ast.Table:
for id, t := range outputSubtbl {
err = c.parseOutput(outputName, t, id)
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("Unsupported config format: %s",
outputName)
}
}
default:
Expand Down Expand Up @@ -579,7 +582,7 @@ func (c *Config) parseAgent(agentAst *ast.Table) error {
}

// Parse an output config out of the given *ast.Table.
func (c *Config) parseOutput(name string, outputAst *ast.Table) error {
func (c *Config) parseOutput(name string, outputAst *ast.Table, id int) error {
c.outputFieldsSet[name] = extractFieldNames(outputAst)
creator, ok := outputs.Outputs[name]
if !ok {
Expand All @@ -590,7 +593,7 @@ func (c *Config) parseOutput(name string, outputAst *ast.Table) error {
if err != nil {
return err
}
c.outputs[name] = output
c.outputs[fmt.Sprintf("%s-%d", name, id)] = output
return nil
}

Expand Down

0 comments on commit 3a1b15a

Please sign in to comment.