Skip to content

Commit 73552ac

Browse files
committed
feat: added thanos check rules command
Signed-off-by: Martin Chodur <[email protected]>
1 parent 8498d91 commit 73552ac

File tree

9 files changed

+301
-25
lines changed

9 files changed

+301
-25
lines changed

CHANGELOG.md

+27-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ We use *breaking* word for marking changes that are not backward compatible (rel
1111

1212
## Unreleased
1313

14+
### Added
15+
- [#1097](https://github.com/improbable-eng/thanos/pull/1097) Added `thanos check rules` linter for Thanos rule rules files.
16+
1417
## [v0.4.0-rc.1](https://github.com/improbable-eng/thanos/releases/tag/v0.4.0-rc.1) - 2019.04.26
1518

1619
:warning: **IMPORTANT** :warning: This is the last release that supports gossip. From Thanos v0.5.0, gossip will be completely removed.
@@ -31,28 +34,28 @@ See [this](docs/proposals/approved/201809_gossip-removal.md) for more details.
3134
New options:
3235

3336
New Store flags:
34-
37+
3538
* `--store.grpc.series-sample-limit` limits the amount of samples that might be retrieved on a single Series() call. By default it is 0. Consider enabling it by setting it to more than 0 if you are running on limited resources.
3639
* `--store.grpc.series-max-concurrency` limits the number of concurrent Series() calls in Thanos Store. By default it is 20. Considering making it lower or bigger depending on the scale of your deployment.
3740

3841
New Store metrics:
39-
42+
4043
* `thanos_bucket_store_queries_dropped_total` shows how many queries were dropped due to the samples limit;
4144
* `thanos_bucket_store_queries_concurrent_max` is a constant metric which shows how many Series() calls can concurrently be executed by Thanos Store;
4245
* `thanos_bucket_store_queries_in_flight` shows how many queries are currently "in flight" i.e. they are being executed;
4346
* `thanos_bucket_store_gate_duration_seconds` shows how many seconds it took for queries to pass through the gate in both cases - when that fails and when it does not.
44-
47+
4548
New Store tracing span:
4649
* `store_query_gate_ismyturn` shows how long it took for a query to pass (or not) through the gate.
47-
48-
- [#1016](https://github.com/improbable-eng/thanos/pull/1016) Added option for another DNS resolver (miekg/dns client).
50+
51+
- [#1016](https://github.com/improbable-eng/thanos/pull/1016) Added option for another DNS resolver (miekg/dns client).
4952
Note that this is required to have SRV resolution working on [Golang 1.11+ with KubeDNS below v1.14](https://github.com/golang/go/issues/27546)
5053

5154
New Querier and Ruler flag: `-- store.sd-dns-resolver` which allows to specify resolver to use. Either `golang` or `miekgdns`
52-
55+
5356
- [#986](https://github.com/improbable-eng/thanos/pull/986) Allow to save some startup & sync time in store gateway as it is no longer needed to compute index-cache from block index on its own for larger blocks.
54-
The store Gateway still can do it, but it first checks bucket if there is index-cached uploaded already.
55-
In the same time, compactor precomputes the index cache file on every compaction.
57+
The store Gateway still can do it, but it first checks bucket if there is index-cached uploaded already.
58+
In the same time, compactor precomputes the index cache file on every compaction.
5659

5760
New Compactor flag: `--index.generate-missing-cache-file` was added to allow quicker addition of index cache files. If enabled it precomputes missing files on compactor startup. Note that it will take time and it's only one-off step per bucket.
5861

@@ -69,60 +72,60 @@ Note that this is required to have SRV resolution working on [Golang 1.11+ with
6972
- [#1021](https://github.com/improbable-eng/thanos/pull/1021) Query API `series` now supports POST method.
7073
- [#939](https://github.com/improbable-eng/thanos/pull/939) Query API `query_range` now supports POST method.
7174

72-
### Changed
75+
### Changed
7376

7477
- [#970](https://github.com/improbable-eng/thanos/pull/970) Deprecated `partial_response_disabled` proto field. Added `partial_response_strategy` instead. Both in gRPC and Query API.
7578
No `PartialResponseStrategy` field for `RuleGroups` by default means `abort` strategy (old PartialResponse disabled) as this is recommended option for Rules and alerts.
7679

7780
Metrics:
78-
81+
7982
* Added `thanos_rule_evaluation_with_warnings_total` to Ruler.
8083
* DNS `thanos_ruler_query_apis*` are now `thanos_ruler_query_apis_*` for consistency.
8184
* DNS `thanos_querier_store_apis*` are now `thanos_querier_store_apis__*` for consistency.
8285
* Query Gate `thanos_bucket_store_series*` are now `thanos_bucket_store_series___*` for consistency.
8386
* Most of thanos ruler metris related to rule manager has `strategy` label.
84-
87+
8588
Ruler tracing spans:
86-
89+
8790
* `/rule_instant_query HTTP[client]` is now `/rule_instant_query_part_resp_abort HTTP[client]"` if request is for abort strategy.
88-
91+
8992
- [#1009](https://github.com/improbable-eng/thanos/pull/1009): Upgraded Prometheus (~v2.7.0-rc.0 to v2.8.1) and TSDB (`v0.4.0` to `v0.6.1`) deps.
90-
93+
9194
Changes that affects Thanos:
92-
* query:
93-
* [ENHANCEMENT] In histogram_quantile merge buckets with equivalent le values. #5158.
94-
* [ENHANCEMENT] Show list of offending labels in the error message in many-to-many scenarios. #5189
95+
* query:
96+
* [ENHANCEMENT] In histogram_quantile merge buckets with equivalent le values. #5158.
97+
* [ENHANCEMENT] Show list of offending labels in the error message in many-to-many scenarios. #5189
9598
* [BUGFIX] Fix panic when aggregator param is not a literal. #5290
96-
* ruler:
99+
* ruler:
97100
* [ENHANCEMENT] Reduce time that Alertmanagers are in flux when reloaded. #5126
98101
* [BUGFIX] prometheus_rule_group_last_evaluation_timestamp_seconds is now a unix timestamp. #5186
99102
* [BUGFIX] prometheus_rule_group_last_duration_seconds now reports seconds instead of nanoseconds. Fixes our [issue #1027](https://github.com/improbable-eng/thanos/issues/1027)
100103
* [BUGFIX] Fix sorting of rule groups. #5260
101104
* store: [ENHANCEMENT] Fast path for EmptyPostings cases in Merge, Intersect and Without.
102105
* tooling: [FEATURE] New dump command to tsdb tool to dump all samples.
103106
* compactor: [ENHANCEMENT] When closing the db any running compaction will be cancelled so it doesn't block.
104-
107+
105108
For ruler essentially whole TSDB CHANGELOG applies beween v0.4.0-v0.6.1: https://github.com/prometheus/tsdb/blob/master/CHANGELOG.md
106-
109+
107110
Note that this was added on TSDB and Prometheus: [FEATURE] Time-ovelapping blocks are now allowed. #370
108111
Whoever due to nature of Thanos compaction (distributed systems), for safety reason this is disabled for Thanos compactor for now.
109-
112+
110113
- [#868](https://github.com/improbable-eng/thanos/pull/868) Go has been updated to 1.12.
111-
- [#1055](https://github.com/improbable-eng/thanos/pull/1055) Gossip flags are now disabled by default and deprecated.
114+
- [#1055](https://github.com/improbable-eng/thanos/pull/1055) Gossip flags are now disabled by default and deprecated.
112115
- [#964](https://github.com/improbable-eng/thanos/pull/964) repair: Repair process now sorts the series and labels within block.
113116
- [#1073](https://github.com/improbable-eng/thanos/pull/1073) Store: index cache for requests. It now calculates the size properly (includes slice header), has anti-deadlock safeguard and reports more metrics.
114117

115118
### Fixed
116119

117120
- [#921](https://github.com/improbable-eng/thanos/pull/921) `thanos_objstore_bucket_last_successful_upload_time` now does not appear when no blocks have been uploaded so far.
118-
- [#966](https://github.com/improbable-eng/thanos/pull/966) Bucket: verify no longer warns about overlapping blocks, that overlap `0s`
121+
- [#966](https://github.com/improbable-eng/thanos/pull/966) Bucket: verify no longer warns about overlapping blocks, that overlap `0s`
119122
- [#848](https://github.com/improbable-eng/thanos/pull/848) Compact: now correctly works with time series with duplicate labels.
120123
- [#894](https://github.com/improbable-eng/thanos/pull/894) Thanos Rule: UI now correctly shows evaluation time.
121124
- [#865](https://github.com/improbable-eng/thanos/pull/865) Query: now properly parses DNS SRV Service Discovery.
122125
- [#889](https://github.com/improbable-eng/thanos/pull/889) Store: added safeguard against merging posting groups segfault
123126
- [#941](https://github.com/improbable-eng/thanos/pull/941) Sidecar: added better handling of intermediate restarts.
124127
- [#933](https://github.com/improbable-eng/thanos/pull/933) Query: Fixed 30 seconds lag of adding new store to query.
125-
- [#962](https://github.com/improbable-eng/thanos/pull/962) Sidecar: Make config reloader file writes atomic.
128+
- [#962](https://github.com/improbable-eng/thanos/pull/962) Sidecar: Make config reloader file writes atomic.
126129
- [#982](https://github.com/improbable-eng/thanos/pull/982) Query: now advertises Min & Max Time accordingly to the nodes.
127130
- [#1041](https://github.com/improbable-eng/thanos/issues/1038) Ruler is now able to return long time range queries.
128131
- [#904](https://github.com/improbable-eng/thanos/pull/904) Compact: Skip compaction for blocks with no samples.

cmd/thanos/check.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
6+
"github.com/go-kit/kit/log"
7+
"github.com/go-kit/kit/log/level"
8+
thanosrule "github.com/improbable-eng/thanos/pkg/rule"
9+
"github.com/oklog/run"
10+
"github.com/opentracing/opentracing-go"
11+
"github.com/pkg/errors"
12+
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/prometheus/prometheus/pkg/rulefmt"
14+
"github.com/prometheus/tsdb"
15+
"gopkg.in/alecthomas/kingpin.v2"
16+
"gopkg.in/yaml.v2"
17+
)
18+
19+
func registerChecks(m map[string]setupFunc, app *kingpin.Application, name string) {
20+
cmd := app.Command(name, "Linting tools for Thanos")
21+
registerRulesCheck(m, cmd, name)
22+
return
23+
}
24+
25+
func registerRulesCheck(m map[string]setupFunc, root *kingpin.CmdClause, name string) {
26+
checkRulesCmd := root.Command("rules", "Check if the rule files are valid or not.")
27+
ruleFiles := checkRulesCmd.Arg(
28+
"rule-files",
29+
"The rule files to check.",
30+
).Required().ExistingFiles()
31+
32+
m[name+" rules"] = func(g *run.Group, logger log.Logger, reg *prometheus.Registry, _ opentracing.Tracer, _ bool) error {
33+
// Dummy actor to immediately kill the group after the run function returns.
34+
g.Add(func() error { return nil }, func(error) {})
35+
return checkRulesFiles(logger, ruleFiles)
36+
}
37+
}
38+
39+
func checkRulesFiles(logger log.Logger, files *[]string) error {
40+
failed := tsdb.MultiError{}
41+
42+
for _, f := range *files {
43+
n, errs := checkRules(logger, f)
44+
if errs.Err() != nil {
45+
level.Error(logger).Log("result", "FAILED")
46+
for _, e := range errs {
47+
level.Error(logger).Log("error", e.Error())
48+
failed.Add(e)
49+
}
50+
level.Info(logger).Log()
51+
continue
52+
}
53+
level.Info(logger).Log("result", "SUCCESS", "rules found", n)
54+
}
55+
if failed.Err() != nil {
56+
return failed
57+
}
58+
return nil
59+
}
60+
61+
func checkRules(logger log.Logger, filename string) (int, tsdb.MultiError) {
62+
level.Info(logger).Log("msg", "Checking", "filename", filename)
63+
checkErrors := tsdb.MultiError{}
64+
65+
b, err := ioutil.ReadFile(filename)
66+
if err != nil {
67+
checkErrors.Add(err)
68+
return 0, checkErrors
69+
}
70+
71+
var rgs thanosrule.RuleGroups
72+
if err := yaml.Unmarshal(b, &rgs); err != nil {
73+
checkErrors.Add(err)
74+
return 0, checkErrors
75+
}
76+
77+
// We need to convert Thanos rules to Prometheus rules so we can use their validation.
78+
promRgs := thanosRuleGroupsToPromRuleGroups(rgs)
79+
if errs := promRgs.Validate(); errs != nil {
80+
for _, e := range errs {
81+
checkErrors.Add(e)
82+
}
83+
return 0, checkErrors
84+
}
85+
86+
numRules := 0
87+
for _, rg := range rgs.Groups {
88+
numRules += len(rg.Rules)
89+
}
90+
91+
return numRules, checkErrors
92+
}
93+
94+
func thanosRuleGroupsToPromRuleGroups(ruleGroups thanosrule.RuleGroups) rulefmt.RuleGroups {
95+
promRuleGroups := rulefmt.RuleGroups{Groups: []rulefmt.RuleGroup{}}
96+
for _, g := range ruleGroups.Groups {
97+
group := rulefmt.RuleGroup{
98+
Name: g.Name,
99+
Interval: g.Interval,
100+
Rules: []rulefmt.Rule{},
101+
}
102+
for _, r := range g.Rules {
103+
group.Rules = append(
104+
group.Rules,
105+
rulefmt.Rule{
106+
Record: r.Record,
107+
Alert: r.Alert,
108+
Expr: r.Expr,
109+
For: r.For,
110+
Labels: r.Labels,
111+
Annotations: r.Annotations,
112+
},
113+
)
114+
}
115+
promRuleGroups.Groups = append(
116+
promRuleGroups.Groups,
117+
group,
118+
)
119+
}
120+
return promRuleGroups
121+
}

cmd/thanos/check_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-kit/kit/log"
7+
"github.com/improbable-eng/thanos/pkg/testutil"
8+
)
9+
10+
func Test_checkRules(t *testing.T) {
11+
12+
validFiles := []string{
13+
"./testdata/rules-files/valid.yaml",
14+
}
15+
16+
invalidFiles := [][]string{
17+
[]string{"./testdata/rules-files/non-existing-file.yaml"},
18+
[]string{"./testdata/rules-files/invalid-yaml-format.yaml"},
19+
[]string{"./testdata/rules-files/invalid-rules-data.yaml"},
20+
}
21+
22+
logger := log.NewNopLogger()
23+
24+
testutil.Ok(t, checkRulesFiles(logger, &validFiles))
25+
26+
for _, fn := range invalidFiles {
27+
testutil.NotOk(t, checkRulesFiles(logger, &fn))
28+
}
29+
}

cmd/thanos/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func main() {
7979
registerBucket(cmds, app, "bucket")
8080
registerDownsample(cmds, app, "downsample")
8181
registerReceive(cmds, app, "receive")
82+
registerChecks(cmds, app, "check")
8283

8384
cmd, err := app.Parse(os.Args[1:])
8485
if err != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
groups:
2+
- name: null
3+
partial_response_strategy: "warn"
4+
interval: 2m
5+
rules:
6+
- alert: TestAlert
7+
partial_response_strategy: "warn"
8+
expr: 1
9+
labels:
10+
key: value
11+
annotations:
12+
key: value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
groups:
2+
- name: test
3+
invalid_yaml_reason
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
groups:
2+
- name: test-alert-group
3+
partial_response_strategy: "warn"
4+
interval: 2m
5+
rules:
6+
- alert: TestAlert
7+
partial_response_strategy: "warn"
8+
expr: 1
9+
labels:
10+
key: value
11+
annotations:
12+
key: value
13+
14+
- name: test-rule-group
15+
partial_response_strategy: "warn"
16+
interval: 2m
17+
rules:
18+
- record: test_metric
19+
expr: 1
20+
partial_response_strategy: "warn"

0 commit comments

Comments
 (0)