Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for remaining file rotation options #5944

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ type AgentConfig struct {
// Log file name, the empty string means to log to stderr.
Logfile string `toml:"logfile"`

// The logfile will be rotated when it becomes larger than the specified
// size. When set to 0 no size based rotation is performed.
// The file will be rotated after the time interval specified. When set
// to 0 no time based rotation is performed.
LogfileRotationInterval internal.Duration `toml:"logfile_rotation_interval"`

// Maximum number of rotated archives to keep, any older logs are deleted.
// If set to -1, no archives are removed.
// The logfile will be rotated when it becomes larger than the specified
// size. When set to 0 no size based rotation is performed.
LogfileRotationMaxSize internal.Size `toml:"logfile_rotation_max_size"`

// Maximum number of rotated archives to keep, any older logs are deleted.
Expand Down
18 changes: 14 additions & 4 deletions plugins/outputs/file/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# file Output Plugin
# File Output Plugin

This plugin writes telegraf metrics to files

### Configuration
```

```toml
[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
files = ["stdout", "/tmp/metrics.out"]

## If this is defined, files will be rotated by the time.Duration specified
# rotate_max_age = "1m"
## The file will be rotated after the time interval specified. When set
## 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.
## Each data format has its own unique set of configuration options, read
Expand Down
45 changes: 22 additions & 23 deletions plugins/outputs/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,40 @@ import (
"fmt"
"io"
"os"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/rotate"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers"
)

type File struct {
Files []string
RotateMaxAge string

writer io.Writer
closers []io.Closer
Files []string `toml:"files"`
RotationInterval internal.Duration `toml:"rotation_interval"`
RotationMaxSize internal.Size `toml:"rotation_max_size"`
RotationMaxArchives int `toml:"rotation_max_archives"`

writer io.Writer
closers []io.Closer
serializer serializers.Serializer
}

var sampleConfig = `
## Files to write to, "stdout" is a specially handled file.
files = ["stdout", "/tmp/metrics.out"]

## If this is defined, files will be rotated by the time.Duration specified
# rotate_max_age = "1m"
## The file will be rotated after the time interval specified. When set
## 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.
## Each data format has its own unique set of configuration options, read
Expand All @@ -51,23 +61,12 @@ func (f *File) Connect() error {
if file == "stdout" {
writers = append(writers, os.Stdout)
} else {
var of io.WriteCloser
var err error
if f.RotateMaxAge != "" {
maxAge, err := time.ParseDuration(f.RotateMaxAge)
if err != nil {
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)
}
of, err := rotate.NewFileWriter(
file, f.RotationInterval.Duration, f.RotationMaxSize.Size, f.RotationMaxArchives)
if err != nil {
return err
}

writers = append(writers, of)
f.closers = append(f.closers, of)
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func (f *File) Write(metrics []telegraf.Metric) error {

_, err = f.writer.Write(b)
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)
}
}

Expand Down