From 11503777a12f1964d9d2fcc8ce9202548d93e842 Mon Sep 17 00:00:00 2001 From: Mickael Carl Date: Sat, 23 Sep 2023 19:10:13 +0100 Subject: [PATCH] file sd: only glob on actual patterns The previous implementation can be confusing. It will glob even regular file paths, which means passing in a non-existing filepath will not result in an error. This is counter-intuitive, as examplified in #12356. As a result of this change, the following can be seen in logs: ``` ts=2023-09-23T17:53:57.332Z caller=file.go:352 level=error component="discovery manager scrape" discovery=file config=testFile msg="Error reading file" path=banana.yml err="open banana.yml: no such file or directory" ``` And the file SD error count is also incremented now as a consequence. I've also documented this behaviour, as I believe the documentation wasn't necessarily clear on the fact that pattern would be globbed. Fixes #12356. Signed-off-by: Mickael Carl --- discovery/file/file.go | 14 +++++++++----- docs/configuration/configuration.md | 5 +++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/discovery/file/file.go b/discovery/file/file.go index 60b63350f..6e78b72ea 100644 --- a/discovery/file/file.go +++ b/discovery/file/file.go @@ -209,12 +209,16 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) *Discovery { func (d *Discovery) listFiles() []string { var paths []string for _, p := range d.paths { - files, err := filepath.Glob(p) - if err != nil { - level.Error(d.logger).Log("msg", "Error expanding glob", "glob", p, "err", err) - continue + if strings.ContainsAny(p, "^?*[") { + files, err := filepath.Glob(p) + if err != nil { + level.Error(d.logger).Log("msg", "Error expanding glob", "glob", p, "err", err) + continue + } + paths = append(paths, files...) + } else { + paths = append(paths, p) } - paths = append(paths, files...) } return paths } diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index c9e941549..a2b70a2fc 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -1578,8 +1578,9 @@ files: [ refresh_interval: | default = 5m ] ``` -Where `` may be a path ending in `.json`, `.yml` or `.yaml`. The last path segment -may contain a single `*` that matches any character sequence, e.g. `my/path/tg_*.json`. +Where `` may be a path ending in `.json`, `.yml` or `.yaml`. +If the pattern contains any of `?`, `*`, `^` or `[`, it will be treated as a +glob and expanded. ### ``