Skip to content

Commit

Permalink
#5 prepare linux file tailer for multiple log file support
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Jan 22, 2019
1 parent 64d41ad commit e12294f
Show file tree
Hide file tree
Showing 17 changed files with 956 additions and 513 deletions.
7 changes: 5 additions & 2 deletions grok_exporter.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/fstab/grok_exporter/oniguruma"
"github.com/fstab/grok_exporter/tailer"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -269,13 +270,15 @@ func startServer(cfg v2.ServerConfig, handler http.Handler) chan error {
}

func startTailer(cfg *v2.Config) (tailer.Tailer, error) {
logger := logrus.New()
logger.Level = logrus.WarnLevel
var tail tailer.Tailer
switch {
case cfg.Input.Type == "file":
if cfg.Input.PollInterval == 0 {
tail = tailer.RunFseventFileTailer(cfg.Input.Path, cfg.Input.Readall, cfg.Input.FailOnMissingLogfile, nil)
tail = tailer.RunFseventFileTailer(cfg.Input.Path, cfg.Input.Readall, cfg.Input.FailOnMissingLogfile, logger)
} else {
tail = tailer.RunPollingFileTailer(cfg.Input.Path, cfg.Input.Readall, cfg.Input.FailOnMissingLogfile, cfg.Input.PollInterval, nil)
tail = tailer.RunPollingFileTailer(cfg.Input.Path, cfg.Input.Readall, cfg.Input.FailOnMissingLogfile, cfg.Input.PollInterval, logger)
}
case cfg.Input.Type == "stdin":
tail = tailer.RunStdinTailer()
Expand Down
17 changes: 6 additions & 11 deletions tailer/fileTailer.go
Expand Up @@ -16,6 +16,7 @@ package tailer

import (
"fmt"
"github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -44,16 +45,18 @@ func (f *fileTailer) Errors() chan Error {
return f.errors
}

func RunPollingFileTailer(path string, readall bool, failOnMissingFile bool, pollIntervall time.Duration, logger simpleLogger) Tailer {
func RunPollingFileTailer(path string, readall bool, failOnMissingFile bool, pollIntervall time.Duration, logger logrus.FieldLogger) Tailer {
makeWatcher := func(abspath string, _ *File) (Watcher, error) {
return NewPollingWatcher(abspath, pollIntervall)
}
return runFileTailer(path, readall, failOnMissingFile, logger, makeWatcher)
}

func runFileTailer(path string, readall bool, failOnMissingFile bool, logger simpleLogger, makeWatcher func(string, *File) (Watcher, error)) Tailer {
func runFileTailer(path string, readall bool, failOnMissingFile bool, logger logrus.FieldLogger, makeWatcher func(string, *File) (Watcher, error)) Tailer {
if logger == nil {
logger = &nilLogger{}
log := logrus.New()
log.Level = logrus.WarnLevel
logger = log
}

lines := make(chan string)
Expand Down Expand Up @@ -199,11 +202,3 @@ func writeError(errors chan Error, done chan struct{}, cause error, format strin
case <-done:
}
}

type simpleLogger interface {
Debug(format string, a ...interface{})
}

type nilLogger struct{}

func (_ *nilLogger) Debug(_ string, _ ...interface{}) {}
Expand Up @@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !darwin
// +build !darwin,!linux

package tailer

import "github.com/sirupsen/logrus"

// old implementation, darwin is already switched to the new implementation, the other OSes will follow
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, logger simpleLogger) Tailer {
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, logger logrus.FieldLogger) Tailer {
return runFileTailer(path, readall, failOnMissingFile, logger, NewFseventWatcher)
}
Expand Up @@ -16,8 +16,12 @@ package tailer

import (
"github.com/fstab/grok_exporter/tailer/fswatcher"
"github.com/fstab/grok_exporter/tailer/glob"
"github.com/sirupsen/logrus"
)

// TODO: This wrapper will be removed when all OSs are migrated to the new fswatcher, supporting multiple log files.

type tailerWrapper struct {
lines chan string
errors chan Error
Expand All @@ -38,14 +42,21 @@ func (t *tailerWrapper) Errors() chan Error {

// Switch to the new file tailer implementation which supports watching multiple files.
// Once we switched for all supported operating systems, we can remove the old implementation and the wrapper.
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, _ interface{}) Tailer {
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, logger logrus.FieldLogger) Tailer {
result := &tailerWrapper{
lines: make(chan string),
errors: make(chan Error),
done: make(chan struct{}),
}

newTailer, err := fswatcher.Run([]string{path}, readall, failOnMissingFile)
pathAsGlob, err := glob.Parse(path)
if err != nil {
go func() {
result.errors <- newError("failed to initialize file system watcher", err)
}()
return result
}
newTailer, err := fswatcher.Run([]glob.Glob{pathAsGlob}, readall, failOnMissingFile, logger)
if err != nil {
go func() {
result.errors <- newError("failed to initialize file system watcher", err)
Expand Down
272 changes: 0 additions & 272 deletions tailer/fileTailer_linux.go

This file was deleted.

0 comments on commit e12294f

Please sign in to comment.