Fix open error handling in file output (#5540)

This commit is contained in:
Daniel Nelson 2019-03-22 13:59:30 -07:00 committed by GitHub
parent fa65a82ef3
commit 99a390b8e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 23 deletions

View File

@ -129,10 +129,7 @@ func (a *Agent) Run(ctx context.Context) error {
wg.Wait() wg.Wait()
log.Printf("D! [agent] Closing outputs") log.Printf("D! [agent] Closing outputs")
err = a.closeOutputs() a.closeOutputs()
if err != nil {
return err
}
log.Printf("D! [agent] Stopped Successfully") log.Printf("D! [agent] Stopped Successfully")
return nil return nil
@ -589,12 +586,10 @@ func (a *Agent) connectOutputs(ctx context.Context) error {
} }
// closeOutputs closes all outputs. // closeOutputs closes all outputs.
func (a *Agent) closeOutputs() error { func (a *Agent) closeOutputs() {
var err error
for _, output := range a.Config.Outputs { for _, output := range a.Config.Outputs {
err = output.Output.Close() output.Close()
} }
return err
} }
// startServiceInputs starts all service inputs. // startServiceInputs starts all service inputs.

View File

@ -180,6 +180,13 @@ func (ro *RunningOutput) WriteBatch() error {
return nil return nil
} }
func (ro *RunningOutput) Close() {
err := ro.Output.Close()
if err != nil {
log.Printf("E! [outputs.%s] Error closing output: %v", ro.Name, err)
}
}
func (ro *RunningOutput) write(metrics []telegraf.Metric) error { func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
start := time.Now() start := time.Now()
err := ro.Output.Write(metrics) err := ro.Output.Write(metrics)

View File

@ -43,17 +43,11 @@ func (f *File) Connect() error {
if file == "stdout" { if file == "stdout" {
f.writers = append(f.writers, os.Stdout) f.writers = append(f.writers, os.Stdout)
} else { } else {
var of *os.File of, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeAppend|0644)
var err error
if _, err := os.Stat(file); os.IsNotExist(err) {
of, err = os.Create(file)
} else {
of, err = os.OpenFile(file, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
}
if err != nil { if err != nil {
return err return err
} }
f.writers = append(f.writers, of) f.writers = append(f.writers, of)
f.closers = append(f.closers, of) f.closers = append(f.closers, of)
} }
@ -62,16 +56,14 @@ func (f *File) Connect() error {
} }
func (f *File) Close() error { func (f *File) Close() error {
var errS string var err error
for _, c := range f.closers { for _, c := range f.closers {
if err := c.Close(); err != nil { errClose := c.Close()
errS += err.Error() + "\n" if errClose != nil {
err = errClose
} }
} }
if errS != "" { return err
return fmt.Errorf(errS)
}
return nil
} }
func (f *File) SampleConfig() string { func (f *File) SampleConfig() string {