-
Notifications
You must be signed in to change notification settings - Fork 7
/
collector.go
163 lines (131 loc) · 3.26 KB
/
collector.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
153
154
155
156
157
158
159
160
161
162
163
package logscollector
import (
"context"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/nxadm/tail"
"golang.org/x/sync/errgroup"
)
const timeFormat = time.RFC3339
type Collector struct {
logsDir string
outputDir string
filesToCollect chan string
}
func New(logsDir, outputDir string) *Collector {
return &Collector{
filesToCollect: make(chan string, 20),
logsDir: logsDir,
outputDir: outputDir,
}
}
func (lc Collector) watchForCreatedFiles(ctx context.Context) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
if err := watcher.Add(lc.logsDir); err != nil {
return err
}
defer close(lc.filesToCollect)
log.Printf("starting files watcher in %q", lc.logsDir)
for {
select {
case <-ctx.Done():
return ctx.Err()
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if event.Op&fsnotify.Create == fsnotify.Create {
lc.filesToCollect <- event.Name
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
return err
}
}
}
func (lc Collector) collectLogs(ctx context.Context, logFilePath string) error {
log.Printf("Setting up file listener for %q", logFilePath)
defer log.Printf("Shutting down file listener for %q", logFilePath)
t, err := tail.TailFile(logFilePath, tail.Config{Follow: true, CompleteLines: true})
if err != nil {
return fmt.Errorf("failed to setup file listener %q: %q", logFilePath, err)
}
defer t.Cleanup()
logFileNameBase := path.Base(logFilePath)
logFileName := strings.TrimSuffix(logFileNameBase, filepath.Ext(logFileNameBase))
destLogFile := path.Join(lc.outputDir, fmt.Sprintf("%s-%s.log", logFileName, time.Now().Format(timeFormat)))
f, err := os.Create(destLogFile)
if err != nil {
return fmt.Errorf("failed to create log file %q: %q", destLogFile, err)
}
defer f.Close()
stopChan := make(chan struct{}, 1)
defer close(stopChan)
go func() {
<-ctx.Done()
if err := t.StopAtEOF(); err != nil {
t.Stop() //nolint
}
time.Sleep(time.Second * 5)
stopChan <- struct{}{}
}()
for {
select {
case l := <-t.Lines:
if l == nil {
return nil
}
if l.Err != nil {
return err
}
if _, err := fmt.Fprintln(f, l.Text); err != nil {
return fmt.Errorf("failed to write to log file %q: %q", f.Name(), err)
}
case <-stopChan:
return nil
}
}
}
func (lc Collector) Run(ctx context.Context) error {
match := fmt.Sprintf("%s/*.[stderr|stdout]*[0-9]", lc.logsDir)
log.Printf("Looking for log files with %q", match)
logsPaths, err := filepath.Glob(match)
if err != nil {
return fmt.Errorf("failed to look for files: %w", err)
}
for _, logPath := range logsPaths {
lc.filesToCollect <- logPath
}
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
if err := lc.watchForCreatedFiles(ctx); err != nil {
return fmt.Errorf("failed to watch for created files: %w", err)
}
return nil
})
eg.Go(func() error {
for logFilePath := range lc.filesToCollect {
logFilePath := logFilePath
eg.Go(func() error {
if err := lc.collectLogs(ctx, logFilePath); err != nil {
return fmt.Errorf("failec to collect logs for %q: %w", logFilePath, err)
}
return nil
})
}
return nil
})
return eg.Wait()
}