Add support for remaining file rotation options (#5944)
This commit is contained in:
parent
4197426a73
commit
6c3534a66e
|
@ -149,12 +149,12 @@ type AgentConfig struct {
|
||||||
// Log file name, the empty string means to log to stderr.
|
// Log file name, the empty string means to log to stderr.
|
||||||
Logfile string `toml:"logfile"`
|
Logfile string `toml:"logfile"`
|
||||||
|
|
||||||
// The logfile will be rotated when it becomes larger than the specified
|
// The file will be rotated after the time interval specified. When set
|
||||||
// size. When set to 0 no size based rotation is performed.
|
// to 0 no time based rotation is performed.
|
||||||
LogfileRotationInterval internal.Duration `toml:"logfile_rotation_interval"`
|
LogfileRotationInterval internal.Duration `toml:"logfile_rotation_interval"`
|
||||||
|
|
||||||
// Maximum number of rotated archives to keep, any older logs are deleted.
|
// The logfile will be rotated when it becomes larger than the specified
|
||||||
// If set to -1, no archives are removed.
|
// size. When set to 0 no size based rotation is performed.
|
||||||
LogfileRotationMaxSize internal.Size `toml:"logfile_rotation_max_size"`
|
LogfileRotationMaxSize internal.Size `toml:"logfile_rotation_max_size"`
|
||||||
|
|
||||||
// Maximum number of rotated archives to keep, any older logs are deleted.
|
// Maximum number of rotated archives to keep, any older logs are deleted.
|
||||||
|
|
|
@ -1,15 +1,25 @@
|
||||||
# file Output Plugin
|
# File Output Plugin
|
||||||
|
|
||||||
This plugin writes telegraf metrics to files
|
This plugin writes telegraf metrics to files
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
```
|
|
||||||
|
```toml
|
||||||
[[outputs.file]]
|
[[outputs.file]]
|
||||||
## Files to write to, "stdout" is a specially handled file.
|
## Files to write to, "stdout" is a specially handled file.
|
||||||
files = ["stdout", "/tmp/metrics.out"]
|
files = ["stdout", "/tmp/metrics.out"]
|
||||||
|
|
||||||
## If this is defined, files will be rotated by the time.Duration specified
|
## The file will be rotated after the time interval specified. When set
|
||||||
# rotate_max_age = "1m"
|
## to 0 no time based rotation is performed.
|
||||||
|
# rotation_interval = "0h"
|
||||||
|
|
||||||
|
## The logfile will be rotated when it becomes larger than the specified
|
||||||
|
## size. When set to 0 no size based rotation is performed.
|
||||||
|
# rotation_max_size = "0MB"
|
||||||
|
|
||||||
|
## Maximum number of rotated archives to keep, any older logs are deleted.
|
||||||
|
## If set to -1, no archives are removed.
|
||||||
|
# rotation_max_archives = 5
|
||||||
|
|
||||||
## Data format to output.
|
## Data format to output.
|
||||||
## Each data format has its own unique set of configuration options, read
|
## Each data format has its own unique set of configuration options, read
|
||||||
|
|
|
@ -5,21 +5,22 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/internal"
|
||||||
"github.com/influxdata/telegraf/internal/rotate"
|
"github.com/influxdata/telegraf/internal/rotate"
|
||||||
"github.com/influxdata/telegraf/plugins/outputs"
|
"github.com/influxdata/telegraf/plugins/outputs"
|
||||||
"github.com/influxdata/telegraf/plugins/serializers"
|
"github.com/influxdata/telegraf/plugins/serializers"
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Files []string
|
Files []string `toml:"files"`
|
||||||
RotateMaxAge string
|
RotationInterval internal.Duration `toml:"rotation_interval"`
|
||||||
|
RotationMaxSize internal.Size `toml:"rotation_max_size"`
|
||||||
|
RotationMaxArchives int `toml:"rotation_max_archives"`
|
||||||
|
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
closers []io.Closer
|
closers []io.Closer
|
||||||
|
|
||||||
serializer serializers.Serializer
|
serializer serializers.Serializer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,8 +28,17 @@ var sampleConfig = `
|
||||||
## Files to write to, "stdout" is a specially handled file.
|
## Files to write to, "stdout" is a specially handled file.
|
||||||
files = ["stdout", "/tmp/metrics.out"]
|
files = ["stdout", "/tmp/metrics.out"]
|
||||||
|
|
||||||
## If this is defined, files will be rotated by the time.Duration specified
|
## The file will be rotated after the time interval specified. When set
|
||||||
# rotate_max_age = "1m"
|
## to 0 no time based rotation is performed.
|
||||||
|
# rotation_interval = "0d"
|
||||||
|
|
||||||
|
## The logfile will be rotated when it becomes larger than the specified
|
||||||
|
## size. When set to 0 no size based rotation is performed.
|
||||||
|
# rotation_max_size = "0MB"
|
||||||
|
|
||||||
|
## Maximum number of rotated archives to keep, any older logs are deleted.
|
||||||
|
## If set to -1, no archives are removed.
|
||||||
|
# rotation_max_archives = 5
|
||||||
|
|
||||||
## Data format to output.
|
## Data format to output.
|
||||||
## Each data format has its own unique set of configuration options, read
|
## Each data format has its own unique set of configuration options, read
|
||||||
|
@ -52,23 +62,12 @@ func (f *File) Connect() error {
|
||||||
if file == "stdout" {
|
if file == "stdout" {
|
||||||
writers = append(writers, os.Stdout)
|
writers = append(writers, os.Stdout)
|
||||||
} else {
|
} else {
|
||||||
var of io.WriteCloser
|
of, err := rotate.NewFileWriter(
|
||||||
var err error
|
file, f.RotationInterval.Duration, f.RotationMaxSize.Size, f.RotationMaxArchives)
|
||||||
if f.RotateMaxAge != "" {
|
|
||||||
maxAge, err := time.ParseDuration(f.RotateMaxAge)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only rotate by file age for now, keep no archives.
|
|
||||||
of, err = rotate.NewFileWriter(file, maxAge, 0, -1)
|
|
||||||
} else {
|
|
||||||
// Just open a normal file
|
|
||||||
of, err = rotate.NewFileWriter(file, 0, 0, -1)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
writers = append(writers, of)
|
writers = append(writers, of)
|
||||||
f.closers = append(f.closers, of)
|
f.closers = append(f.closers, of)
|
||||||
}
|
}
|
||||||
|
@ -107,7 +106,7 @@ func (f *File) Write(metrics []telegraf.Metric) error {
|
||||||
|
|
||||||
_, err = f.writer.Write(b)
|
_, err = f.writer.Write(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeErr = fmt.Errorf("E! failed to write message: %s, %s", b, err)
|
writeErr = fmt.Errorf("E! [outputs.file] failed to write message: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue