Skip to content

Commit 8b8678d

Browse files
danielnelsonJean-Louis Dupond
authored and
Jean-Louis Dupond
committed
Fix open error handling in file output (influxdata#5540)
1 parent 41ec76c commit 8b8678d

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

agent/agent.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ func (a *Agent) Run(ctx context.Context) error {
129129
wg.Wait()
130130

131131
log.Printf("D! [agent] Closing outputs")
132-
err = a.closeOutputs()
133-
if err != nil {
134-
return err
135-
}
132+
a.closeOutputs()
136133

137134
log.Printf("D! [agent] Stopped Successfully")
138135
return nil
@@ -589,12 +586,10 @@ func (a *Agent) connectOutputs(ctx context.Context) error {
589586
}
590587

591588
// closeOutputs closes all outputs.
592-
func (a *Agent) closeOutputs() error {
593-
var err error
589+
func (a *Agent) closeOutputs() {
594590
for _, output := range a.Config.Outputs {
595-
err = output.Output.Close()
591+
output.Close()
596592
}
597-
return err
598593
}
599594

600595
// startServiceInputs starts all service inputs.

internal/models/running_output.go

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ func (ro *RunningOutput) WriteBatch() error {
180180
return nil
181181
}
182182

183+
func (ro *RunningOutput) Close() {
184+
err := ro.Output.Close()
185+
if err != nil {
186+
log.Printf("E! [outputs.%s] Error closing output: %v", ro.Name, err)
187+
}
188+
}
189+
183190
func (ro *RunningOutput) write(metrics []telegraf.Metric) error {
184191
start := time.Now()
185192
err := ro.Output.Write(metrics)

plugins/outputs/file/file.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,11 @@ func (f *File) Connect() error {
4343
if file == "stdout" {
4444
f.writers = append(f.writers, os.Stdout)
4545
} else {
46-
var of *os.File
47-
var err error
48-
if _, err := os.Stat(file); os.IsNotExist(err) {
49-
of, err = os.Create(file)
50-
} else {
51-
of, err = os.OpenFile(file, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
52-
}
53-
46+
of, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeAppend|0644)
5447
if err != nil {
5548
return err
5649
}
50+
5751
f.writers = append(f.writers, of)
5852
f.closers = append(f.closers, of)
5953
}
@@ -62,16 +56,14 @@ func (f *File) Connect() error {
6256
}
6357

6458
func (f *File) Close() error {
65-
var errS string
59+
var err error
6660
for _, c := range f.closers {
67-
if err := c.Close(); err != nil {
68-
errS += err.Error() + "\n"
61+
errClose := c.Close()
62+
if errClose != nil {
63+
err = errClose
6964
}
7065
}
71-
if errS != "" {
72-
return fmt.Errorf(errS)
73-
}
74-
return nil
66+
return err
7567
}
7668

7769
func (f *File) SampleConfig() string {

0 commit comments

Comments
 (0)