Document and add support to input plugins for logging alias (#6357)

This commit is contained in:
Greg
2019-09-23 16:39:50 -06:00
committed by Daniel Nelson
parent e42d2e39c6
commit 817c9a69a9
111 changed files with 961 additions and 659 deletions

View File

@@ -3,8 +3,6 @@
package tail
import (
"fmt"
"log"
"strings"
"sync"
@@ -31,6 +29,8 @@ type Tail struct {
Pipe bool
WatchMethod string
Log telegraf.Logger
tailers map[string]*tail.Tail
offsets map[string]int64
parserFunc parsers.ParserFunc
@@ -124,7 +124,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
for _, filepath := range t.Files {
g, err := globpath.Compile(filepath)
if err != nil {
t.acc.AddError(fmt.Errorf("glob %s failed to compile, %s", filepath, err))
t.Log.Errorf("Glob %q failed to compile: %s", filepath, err.Error())
}
for _, file := range g.Match() {
if _, ok := t.tailers[file]; ok {
@@ -135,7 +135,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
var seek *tail.SeekInfo
if !t.Pipe && !fromBeginning {
if offset, ok := t.offsets[file]; ok {
log.Printf("D! [inputs.tail] using offset %d for file: %v", offset, file)
t.Log.Debugf("Using offset %d for %q", offset, file)
seek = &tail.SeekInfo{
Whence: 0,
Offset: offset,
@@ -163,11 +163,11 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
continue
}
log.Printf("D! [inputs.tail] tail added for file: %v", file)
t.Log.Debugf("Tail added for %q", file)
parser, err := t.parserFunc()
if err != nil {
t.acc.AddError(fmt.Errorf("error creating parser: %v", err))
t.Log.Errorf("Creating parser: %s", err.Error())
}
// create a goroutine for each "tailer"
@@ -213,7 +213,7 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
var firstLine = true
for line := range tailer.Lines {
if line.Err != nil {
t.acc.AddError(fmt.Errorf("error tailing file %s, Error: %s", tailer.Filename, line.Err))
t.Log.Errorf("Tailing %q: %s", tailer.Filename, line.Err.Error())
continue
}
// Fix up files with Windows line endings.
@@ -221,8 +221,8 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
metrics, err := parseLine(parser, text, firstLine)
if err != nil {
t.acc.AddError(fmt.Errorf("malformed log line in %s: [%s], Error: %s",
tailer.Filename, line.Text, err))
t.Log.Errorf("Malformed log line in %q: [%q]: %s",
tailer.Filename, line.Text, err.Error())
continue
}
firstLine = false
@@ -233,10 +233,10 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
}
}
log.Printf("D! [inputs.tail] tail removed for file: %v", tailer.Filename)
t.Log.Debugf("Tail removed for %q", tailer.Filename)
if err := tailer.Err(); err != nil {
t.acc.AddError(fmt.Errorf("error tailing file %s, Error: %s", tailer.Filename, err))
t.Log.Errorf("Tailing %q: %s", tailer.Filename, err.Error())
}
}
@@ -249,14 +249,14 @@ func (t *Tail) Stop() {
// store offset for resume
offset, err := tailer.Tell()
if err == nil {
log.Printf("D! [inputs.tail] recording offset %d for file: %v", offset, tailer.Filename)
t.Log.Debugf("Recording offset %d for %q", offset, tailer.Filename)
} else {
t.acc.AddError(fmt.Errorf("error recording offset for file %s", tailer.Filename))
t.Log.Errorf("Recording offset for %q: %s", tailer.Filename, err.Error())
}
}
err := tailer.Stop()
if err != nil {
t.acc.AddError(fmt.Errorf("error stopping tail on file %s", tailer.Filename))
t.Log.Errorf("Stopping tail on %q: %s", tailer.Filename, err.Error())
}
}

View File

@@ -1,7 +1,9 @@
package tail
import (
"bytes"
"io/ioutil"
"log"
"os"
"runtime"
"testing"
@@ -28,6 +30,7 @@ func TestTailFromBeginning(t *testing.T) {
require.NoError(t, err)
tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser)
@@ -61,6 +64,7 @@ func TestTailFromEnd(t *testing.T) {
require.NoError(t, err)
tt := NewTail()
tt.Log = testutil.Logger{}
tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser)
defer tt.Stop()
@@ -97,6 +101,7 @@ func TestTailBadLine(t *testing.T) {
defer os.Remove(tmpfile.Name())
tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser)
@@ -105,13 +110,17 @@ func TestTailBadLine(t *testing.T) {
acc := testutil.Accumulator{}
require.NoError(t, tt.Start(&acc))
buf := &bytes.Buffer{}
log.SetOutput(buf)
require.NoError(t, acc.GatherError(tt.Gather))
_, err = tmpfile.WriteString("cpu mytag= foo usage_idle= 100\n")
require.NoError(t, err)
acc.WaitError(1)
assert.Contains(t, acc.Errors[0].Error(), "malformed log line")
time.Sleep(500 * time.Millisecond)
assert.Contains(t, buf.String(), "Malformed log line")
}
func TestTailDosLineendings(t *testing.T) {
@@ -122,6 +131,7 @@ func TestTailDosLineendings(t *testing.T) {
require.NoError(t, err)
tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser)
@@ -160,6 +170,7 @@ cpu,42
require.NoError(t, err)
plugin := NewTail()
plugin.Log = testutil.Logger{}
plugin.FromBeginning = true
plugin.Files = []string{tmpfile.Name()}
plugin.SetParserFunc(func() (parsers.Parser, error) {
@@ -217,6 +228,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
require.NoError(t, err)
plugin := NewTail()
plugin.Log = testutil.Logger{}
plugin.FromBeginning = true
plugin.Files = []string{tmpfile.Name()}
plugin.SetParserFunc(func() (parsers.Parser, error) {