Skip to content

Commit 2841613

Browse files
committed
Allow users to specify outputs as lists
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
1 parent bf8e0f4 commit 2841613

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

agent.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,12 @@ func (a *Agent) Close() error {
147147
func (a *Agent) LoadOutputs(filters []string, config *Config) ([]string, error) {
148148
var names []string
149149

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

156152
if sliceContains(name, filters) || len(filters) == 0 {
157153
if a.Debug {
158154
log.Println("Output Enabled: ", name)
159155
}
160-
output := creator()
161156

162157
err := config.ApplyOutput(name, output)
163158
if err != nil {

config.go

+28-20
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,8 @@ func (c *Config) PluginsDeclared() []string {
166166
}
167167

168168
// OutputsDeclared returns the name of all outputs declared in the config.
169-
func (c *Config) OutputsDeclared() []string {
170-
var names []string
171-
for name := range c.outputs {
172-
names = append(names, name)
173-
}
174-
sort.Strings(names)
175-
return names
169+
func (c *Config) OutputsDeclared() map[string]outputs.Output {
170+
return c.outputs
176171
}
177172

178173
// ListTags returns a string of tags specified in the config,
@@ -394,20 +389,24 @@ func findField(fieldName string, value reflect.Value) reflect.Value {
394389
return reflect.Value{}
395390
}
396391

397-
// A very limited merge. Merges the fields named in the fields parameter, replacing most values, but appending to arrays.
392+
// A very limited merge. Merges the fields named in the fields parameter,
393+
// replacing most values, but appending to arrays.
398394
func mergeStruct(base, overlay interface{}, fields []string) error {
399395
baseValue := reflect.ValueOf(base).Elem()
400396
overlayValue := reflect.ValueOf(overlay).Elem()
401397
if baseValue.Kind() != reflect.Struct {
402-
return fmt.Errorf("Tried to merge something that wasn't a struct: type %v was %v", baseValue.Type(), baseValue.Kind())
398+
return fmt.Errorf("Tried to merge something that wasn't a struct: type %v was %v",
399+
baseValue.Type(), baseValue.Kind())
403400
}
404401
if baseValue.Type() != overlayValue.Type() {
405-
return fmt.Errorf("Tried to merge two different types: %v and %v", baseValue.Type(), overlayValue.Type())
402+
return fmt.Errorf("Tried to merge two different types: %v and %v",
403+
baseValue.Type(), overlayValue.Type())
406404
}
407405
for _, field := range fields {
408406
overlayFieldValue := findField(field, overlayValue)
409407
if !overlayFieldValue.IsValid() {
410-
return fmt.Errorf("could not find field in %v matching %v", overlayValue.Type(), field)
408+
return fmt.Errorf("could not find field in %v matching %v",
409+
overlayValue.Type(), field)
411410
}
412411
baseFieldValue := findField(field, baseValue)
413412
if overlayFieldValue.Kind() == reflect.Slice {
@@ -536,13 +535,22 @@ func LoadConfig(path string) (*Config, error) {
536535
}
537536
case "outputs":
538537
for outputName, outputVal := range subtbl.Fields {
539-
outputSubtbl, ok := outputVal.(*ast.Table)
540-
if !ok {
541-
return nil, err
542-
}
543-
err = c.parseOutput(outputName, outputSubtbl)
544-
if err != nil {
545-
return nil, err
538+
switch outputSubtbl := outputVal.(type) {
539+
case *ast.Table:
540+
err = c.parseOutput(outputName, outputSubtbl, 0)
541+
if err != nil {
542+
return nil, err
543+
}
544+
case []*ast.Table:
545+
for id, t := range outputSubtbl {
546+
err = c.parseOutput(outputName, t, id)
547+
if err != nil {
548+
return nil, err
549+
}
550+
}
551+
default:
552+
return nil, fmt.Errorf("Unsupported config format: %s",
553+
outputName)
546554
}
547555
}
548556
default:
@@ -579,7 +587,7 @@ func (c *Config) parseAgent(agentAst *ast.Table) error {
579587
}
580588

581589
// Parse an output config out of the given *ast.Table.
582-
func (c *Config) parseOutput(name string, outputAst *ast.Table) error {
590+
func (c *Config) parseOutput(name string, outputAst *ast.Table, id int) error {
583591
c.outputFieldsSet[name] = extractFieldNames(outputAst)
584592
creator, ok := outputs.Outputs[name]
585593
if !ok {
@@ -590,7 +598,7 @@ func (c *Config) parseOutput(name string, outputAst *ast.Table) error {
590598
if err != nil {
591599
return err
592600
}
593-
c.outputs[name] = output
601+
c.outputs[fmt.Sprintf("%s-%d", name, id)] = output
594602
return nil
595603
}
596604

0 commit comments

Comments
 (0)