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

pkg/rule: support identical rule filenames in different directories #1791

Merged
merged 1 commit into from
Nov 26, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel
- [#1773](https://github.com/thanos-io/thanos/pull/1773) Thanos Ruler: fixed the /api/v1/rules endpoint that returned 500 status code with `failed to assert type of rule ...` message.
- [#1770](https://github.com/thanos-io/thanos/pull/1770) Fix `--web.external-prefix` 404s for static resources.
- [#1785](https://github.com/thanos-io/thanos/pull/1785) Thanos Ruler: the /api/v1/rules endpoints now returns the original rule filenames.
- [#1791](https://github.com/thanos-io/thanos/pull/1791) Thanos Ruler now supports identical rule filenames in different directories.

### Changed

Expand Down
3 changes: 2 additions & 1 deletion pkg/rule/rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package thanosrule

import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error {
continue
}

newFn := filepath.Join(m.workDir, filepath.Base(fn)+"."+s.String())
newFn := filepath.Join(m.workDir, fmt.Sprintf("%s.%x.%s", filepath.Base(fn), sha256.Sum256([]byte(fn)), s.String()))
if err := ioutil.WriteFile(newFn, b, os.ModePerm); err != nil {
errs = append(errs, errors.Wrap(err, newFn))
continue
Expand Down
39 changes: 27 additions & 12 deletions pkg/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package thanosrule
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
Expand All @@ -22,39 +21,41 @@ func TestUpdate(t *testing.T) {
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
err = os.MkdirAll(filepath.Join(dir, "subdir"), 0775)
testutil.Ok(t, err)

testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "no_strategy.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "no_strategy.yaml"), []byte(`
groups:
- name: "something1"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "abort.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "abort.yaml"), []byte(`
groups:
- name: "something2"
partial_response_strategy: "abort"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "warn.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "warn.yaml"), []byte(`
groups:
- name: "something3"
partial_response_strategy: "warn"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "wrong.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "wrong.yaml"), []byte(`
groups:
- name: "something4"
partial_response_strategy: "afafsdgsdgs" # Err 1
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "combined.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "combined.yaml"), []byte(`
groups:
- name: "something5"
partial_response_strategy: "warn"
Expand All @@ -71,6 +72,14 @@ groups:
- alert: "some"
expr: "up"
`), os.ModePerm))
// Same filename as the first rule file but different path.
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "subdir", "no_strategy.yaml"), []byte(`
groups:
- name: "something8"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))

opts := rules.ManagerOptions{
Logger: log.NewLogfmtLogger(os.Stderr),
Expand All @@ -80,12 +89,13 @@ groups:
m.SetRuleManager(storepb.PartialResponseStrategy_WARN, rules.NewManager(&opts))

err = m.Update(10*time.Second, []string{
path.Join(dir, "no_strategy.yaml"),
path.Join(dir, "abort.yaml"),
path.Join(dir, "warn.yaml"),
path.Join(dir, "wrong.yaml"),
path.Join(dir, "combined.yaml"),
path.Join(dir, "non_existing.yaml"),
filepath.Join(dir, "no_strategy.yaml"),
filepath.Join(dir, "abort.yaml"),
filepath.Join(dir, "warn.yaml"),
filepath.Join(dir, "wrong.yaml"),
filepath.Join(dir, "combined.yaml"),
filepath.Join(dir, "non_existing.yaml"),
filepath.Join(dir, "subdir", "no_strategy.yaml"),
})

testutil.NotOk(t, err)
Expand Down Expand Up @@ -132,6 +142,11 @@ groups:
file: filepath.Join(dir, "combined.yaml"),
strategy: storepb.PartialResponseStrategy_ABORT,
},
{
name: "something8",
file: filepath.Join(dir, "subdir", "no_strategy.yaml"),
strategy: storepb.PartialResponseStrategy_ABORT,
},
}
testutil.Equals(t, len(exp), len(g))
for i := range exp {
Expand Down