-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtailer.go
152 lines (136 loc) · 3.51 KB
/
tailer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"sync"
"github.com/fstab/grok_exporter/tailer/fswatcher"
"github.com/fstab/grok_exporter/tailer/glob"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type SafeTailers struct {
sync.Mutex
Tailers []fswatcher.FileTailer
}
func NewSafeTailers() *SafeTailers {
return &SafeTailers{}
}
func (tg *SafeTailers) start(lcs []LogConfig) error {
tg.Lock()
defer tg.Unlock()
for _, lc := range lcs {
log.WithFields(log.Fields{"path": lc.Path}).Info("Registering relevant metrics")
err := registerMetrics(lc.Patterns)
if err != nil {
return fmt.Errorf("failed to register metrics: %s", err)
}
log.WithFields(log.Fields{"path": lc.Path}).Info("Starting tailer")
readall := false
failOnMissing := false
t, err := fswatcher.RunFileTailer([]glob.Glob{glob.Glob(lc.Path)}, readall, failOnMissing, log.StandardLogger())
if err != nil {
return fmt.Errorf("failed to start file tailer for path %s: %s", lc.Path, err)
}
tg.Tailers = append(tg.Tailers, t)
go waitForTailerLines(t, lc)
}
return nil
}
func (tg *SafeTailers) stop() {
tg.Lock()
defer tg.Unlock()
for _, tailer := range tg.Tailers {
tailer.Close()
}
tg.Tailers = nil
}
func runTailer(config *Config, configPath string, tailers *SafeTailers) error {
if tailers.Tailers != nil {
log.WithFields(log.Fields{"configFile": configPath}).Debug("Stopping current tailers...")
tailers.stop()
}
err := tailers.start(config.Logs)
if err != nil {
return err
}
return nil
}
func waitForTailerLines(t fswatcher.FileTailer, lc LogConfig) {
for {
line, open := <-t.Lines()
if !open {
break
}
log.WithFields(log.Fields{"line": line.Line, "path": lc.Path}).Debug("new line")
handleLine(line.Line, lc.Patterns)
}
}
func handleLine(line string, patterns []*PatternConfig) error {
for _, pc := range patterns {
matches := pc.MatchCompiled.FindStringSubmatch(line)
if matches == nil {
continue
}
err := callLineSubHandler(pc, matches)
if err != nil {
log.WithFields(log.Fields{"err": err}).Warn("could not evaluate")
}
if !pc.Continue {
return nil
}
}
return nil
}
func callLineSubHandler(pc *PatternConfig, matches []string) error {
val, err := pc.eval(matches)
if err != nil {
return err
}
if pc.CounterVec != nil {
return handleLineCounter(pc, matches, val)
}
if pc.GaugeVec != nil {
return handleLineGauge(pc, matches, val)
}
return fmt.Errorf("unknown type")
}
func buildLabels(pc *PatternConfig, matches []string) prometheus.Labels {
labels := prometheus.Labels{}
// we are ignoring errors here as the only error case is checked during startup:
evalLabels, _ := pc.getEvaluatedLabels(matches)
for label, value := range evalLabels {
labels[label] = value
}
return labels
}
func handleLineCounter(pc *PatternConfig, matches []string, val float64) error {
labels := buildLabels(pc, matches)
c, err := pc.CounterVec.GetMetricWith(labels)
if err != nil {
return fmt.Errorf("failed to get metric: %s", err)
}
if pc.Action == "inc" {
c.Add(val)
return nil
}
return fmt.Errorf("unknown action '%s'", pc.Action)
}
func handleLineGauge(pc *PatternConfig, matches []string, val float64) error {
labels := buildLabels(pc, matches)
c, err := pc.GaugeVec.GetMetricWith(labels)
if err != nil {
return fmt.Errorf("failed to get metric: %s", err)
}
if pc.Action == "inc" {
c.Add(val)
return nil
}
if pc.Action == "dec" {
c.Sub(val)
return nil
}
if pc.Action == "set" {
c.Set(val)
return nil
}
return fmt.Errorf("unknown action '%s'", pc.Action)
}