-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathwatch.go
More file actions
79 lines (67 loc) · 2 KB
/
watch.go
File metadata and controls
79 lines (67 loc) · 2 KB
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
package file_match
import (
"os"
"path/filepath"
"time"
"github.com/go-kit/log"
"github.com/grafana/alloy/internal/component/discovery"
"github.com/grafana/alloy/internal/runtime/logging/level"
"github.com/grafana/alloy/internal/util"
"github.com/grafana/alloy/internal/util/glob"
)
// watch handles a single discovery.target for file watching.
type watch struct {
target discovery.Target
log log.Logger
ignoreOlderThan time.Duration
globber glob.Globber
}
func (w *watch) getPaths() ([]discovery.Target, error) {
allMatchingPaths := make([]discovery.Target, 0)
matches, err := w.globber.FilepathGlob(w.getPath())
if err != nil {
return nil, err
}
exclude := w.getExcludePath()
for _, m := range matches {
if exclude != "" {
if match, _ := w.globber.PathMatch(filepath.FromSlash(exclude), m); match {
continue
}
}
abs, err := filepath.Abs(m)
if err != nil {
level.Error(w.log).Log("msg", "error getting absolute path", "path", m, "err", err)
continue
}
fi, err := os.Stat(abs)
if err != nil {
// On some filesystems we can get errors accessing the discovered paths. Don't log these as errors.
// local.file_match will retry on the next sync period if the access is blocked temporarily only.
if util.IsEphemeralOrFileClosed(err) {
level.Debug(w.log).Log("msg", "I/O error when getting os stat", "path", abs, "err", err)
} else {
level.Error(w.log).Log("msg", "error getting os stat", "path", abs, "err", err)
}
continue
}
if fi.IsDir() {
continue
}
if w.ignoreOlderThan != 0 && fi.ModTime().Before(time.Now().Add(-w.ignoreOlderThan)) {
continue
}
tb := discovery.NewTargetBuilderFrom(w.target)
tb.Set("__path__", abs)
allMatchingPaths = append(allMatchingPaths, tb.Target())
}
return allMatchingPaths, nil
}
func (w *watch) getPath() string {
path, _ := w.target.Get("__path__")
return path
}
func (w *watch) getExcludePath() string {
excludePath, _ := w.target.Get("__path_exclude__")
return excludePath
}