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

Terminate exporter when file cannot be opened #28

Merged
merged 1 commit into from
Jan 5, 2018
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
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand All @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions tail/tailer.go
Original file line number Diff line number Diff line change
@@ -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
}