diff --git a/main.go b/main.go index 13f6ad0..f1f524d 100644 --- a/main.go +++ b/main.go @@ -24,12 +24,12 @@ import ( "os/signal" "syscall" - "github.com/hpcloud/tail" "github.com/martin-helmich/prometheus-nginxlog-exporter/config" "github.com/martin-helmich/prometheus-nginxlog-exporter/discovery" "github.com/prometheus/client_golang/prometheus" "github.com/satyrius/gonx" "github.com/martin-helmich/prometheus-nginxlog-exporter/relabeling" + "github.com/martin-helmich/prometheus-nginxlog-exporter/tail" ) // Metrics is a struct containing pointers to all metrics that should be @@ -169,15 +169,15 @@ func main() { metrics.Init(&nsCfg) for _, f := range nsCfg.SourceFiles { - t, err := tail.TailFile(f, tail.Config{ - Follow: true, - ReOpen: true, - Poll: true, - }) + t, err := tail.NewFollower(f) if err != nil { panic(err) } + t.OnError(func (err error) { + panic(err) + }) + go func(nsCfg config.NamespaceConfig) { relabelings := relabeling.NewRelabelings(nsCfg.RelabelConfigs) relabelings = append(relabeling.DefaultRelabelings, relabelings...) @@ -192,7 +192,7 @@ func main() { labelValues[i] = staticLabelValues[i] } - for line := range t.Lines { + for line := range t.Lines() { entry, err := parser.ParseString(line.Text) if err != nil { fmt.Printf("error while parsing line '%s': %s\n", line.Text, err) diff --git a/tail/tailer.go b/tail/tailer.go new file mode 100644 index 0000000..985455d --- /dev/null +++ b/tail/tailer.go @@ -0,0 +1,55 @@ +package tail + +import ( + "github.com/hpcloud/tail" +) + +type Follower interface { + Lines() chan *tail.Line + OnError(func (error)) +} + +type followerImpl struct { + filename string + t *tail.Tail +} + +func NewFollower(filename string) (Follower, error) { + f := &followerImpl{ + filename: filename, + } + + if err := f.start(); err != nil { + return nil, err + } + + return f, nil +} + +func (f *followerImpl) start() error { + t, err := tail.TailFile(f.filename, tail.Config{ + Follow: true, + ReOpen: true, + Poll: true, + }) + + if err != nil { + return err + } + + f.t = t + return nil +} + +func (f *followerImpl) OnError(cb func(error)) { + go func() { + err := f.t.Wait() + if err != nil { + cb(err) + } + }() +} + +func (f *followerImpl) Lines() chan *tail.Line { + return f.t.Lines +} \ No newline at end of file