From aa148f8fdb281a9ea4c1e5a7ee2e1336d8459c4f Mon Sep 17 00:00:00 2001 From: Matej Gera <38492574+matej-g@users.noreply.github.com> Date: Thu, 29 Jul 2021 17:44:40 +0200 Subject: [PATCH] Rule: Fix temporary rule filename composition issue (#4468) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adjust rule filename composition Signed-off-by: Matej Gera * Update CHANGELOG.md Signed-off-by: Matej Gera * Update CHANGELOG.md Signed-off-by: Giedrius Statkevičius Co-authored-by: Giedrius Statkevičius Co-authored-by: Giedrius Statkevičius --- CHANGELOG.md | 1 + pkg/rules/manager.go | 2 +- pkg/rules/manager_test.go | 23 ++++++++++++++--------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85450122a9..e6b49139e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Fixed +- [#4468](https://github.com/thanos-io/thanos/pull/4468) Rule: Fix temporary rule filename composition issue. - [#4476](https://github.com/thanos-io/thanos/pull/4476) UI: fix incorrect html escape sequence used for '>' symbol. ### Changed diff --git a/pkg/rules/manager.go b/pkg/rules/manager.go index b671abb207..d2500205bc 100644 --- a/pkg/rules/manager.go +++ b/pkg/rules/manager.go @@ -356,7 +356,7 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error { // Use full file name appending to work dir, so we can differentiate between different dirs and same filenames(!). // This will be also used as key for file group name. - newFn := filepath.Join(m.workDir, s.String(), strings.TrimLeft(fn, m.workDir)) + newFn := filepath.Join(m.workDir, s.String(), fn) if err := os.MkdirAll(filepath.Dir(newFn), os.ModePerm); err != nil { errs.Add(errors.Wrapf(err, "create %s", filepath.Dir(newFn))) continue diff --git a/pkg/rules/manager_test.go b/pkg/rules/manager_test.go index d4230da74c..288abe729a 100644 --- a/pkg/rules/manager_test.go +++ b/pkg/rules/manager_test.go @@ -106,7 +106,12 @@ groups: func TestUpdate_Error_UpdatePartial(t *testing.T) { dir, err := ioutil.TempDir("", "test_rule_rule_groups") testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(dir)) }() + dataDir, err := ioutil.TempDir("", "test_rule_data") + testutil.Ok(t, err) + defer func() { + testutil.Ok(t, os.RemoveAll(dir)) + testutil.Ok(t, os.RemoveAll(dataDir)) + }() err = os.MkdirAll(filepath.Join(dir, "subdir"), 0775) testutil.Ok(t, err) @@ -171,7 +176,7 @@ groups: thanosRuleMgr := NewManager( context.Background(), reg, - dir, + dataDir, rules.ManagerOptions{ Logger: log.NewLogfmtLogger(os.Stderr), Queryable: nopQueryable{}, @@ -200,13 +205,13 @@ groups: // Also, check metrics: Regression test: https://github.com/thanos-io/thanos/issues/3083 testutil.Equals(t, map[string]float64{ - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT/abort.yaml;something2,strategy=abort}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT/bdir/no_strategy.yaml;something8,strategy=abort}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT/combined.yaml;something6,strategy=abort}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT/combined.yaml;something7,strategy=abort}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT/no_strategy.yaml;something1,strategy=abort}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/WARN/combined.yaml;something5,strategy=warn}", dir): 1, - fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/WARN/warn.yaml;something3,strategy=warn}", dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT%s/abort.yaml;something2,strategy=abort}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT%s/subdir/no_strategy.yaml;something8,strategy=abort}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT%s/combined.yaml;something6,strategy=abort}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT%s/combined.yaml;something7,strategy=abort}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/ABORT%s/no_strategy.yaml;something1,strategy=abort}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/WARN%s/combined.yaml;something5,strategy=warn}", dataDir, dir): 1, + fmt.Sprintf("prometheus_rule_group_rules{rule_group=%s/.tmp-rules/WARN%s/warn.yaml;something3,strategy=warn}", dataDir, dir): 1, }, extprom.CurrentGaugeValuesFor(t, reg, "prometheus_rule_group_rules"), )