Skip to content

Commit 9aecc18

Browse files
authored
Remove loki support (#7)
Remove loki rule support
1 parent 5618f93 commit 9aecc18

File tree

268 files changed

+721
-54727
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+721
-54727
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
66

77
* [CHANGE] Rename to cortexproject/cortex-tools #2
88
* [CHANGE] Update go image to v1.16.15. #4
9+
* [CHANGE] Remove loki support #7
910
* [FEATURE] Support Arm64 on Darwin for all binaries (benchtool etc). #215
1011
* [BUGFIX] Fix `cortextool rules` legends displaying wrong symbols for updates and deletions. #226
1112

cmd/logtool/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ func main() {
6464
}
6565
case "msg":
6666
msg := strings.ReplaceAll(d[2], "\"", "")
67-
if !(strings.HasPrefix(msg, "GET /loki/api/") ||
68-
strings.HasPrefix(msg, "GET /api/prom")) {
67+
if !strings.HasPrefix(msg, "GET /api/prom") {
6968
continue
7069
}
7170

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/alecthomas/units v0.0.0-20210912230133-d1bdfacee922
1111
github.com/cortexproject/cortex v1.10.1-0.20211104100946-3f329a21cad4
1212
github.com/dlclark/regexp2 v1.2.0 // indirect
13-
github.com/go-kit/kit v0.12.0
13+
github.com/go-kit/log v0.2.0
1414
github.com/gocql/gocql v0.0.0-20200526081602-cd04bd7f22a7
1515
github.com/gogo/protobuf v1.3.2
1616
github.com/golang/snappy v0.0.4
@@ -25,7 +25,6 @@ require (
2525
github.com/gorilla/mux v1.8.0
2626
github.com/grafana-tools/sdk v0.0.0-20220203092117-edae16afa87b
2727
github.com/grafana/dskit v0.0.0-20211103155626-4e784973d341
28-
github.com/grafana/loki v1.6.2-0.20211108134117-5b9f5b9efaa5
2928
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
3029
github.com/oklog/ulid v1.3.1
3130
github.com/opentracing-contrib/go-stdlib v1.0.0

go.sum

Lines changed: 10 additions & 294 deletions
Large diffs are not rendered by default.

pkg/commands/analyse_rulefiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (cmd *RuleFileAnalyseCommand) run(k *kingpin.ParseContext) error {
1818
output := &analyse.MetricsInRuler{}
1919
output.OverallMetrics = make(map[string]struct{})
2020

21-
nss, err := rules.ParseFiles("cortex", cmd.RuleFilesList)
21+
nss, err := rules.ParseFiles(cmd.RuleFilesList)
2222
if err != nil {
2323
return errors.Wrap(err, "analyse operation unsuccessful, unable to parse rules files")
2424
}

pkg/commands/analyse_rules_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestParseMetricsInRuleFile(t *testing.T) {
4747
output := &analyse.MetricsInRuler{}
4848
output.OverallMetrics = make(map[string]struct{})
4949

50-
nss, err := rules.ParseFiles("cortex", []string{"testdata/prometheus_rules.yaml"})
50+
nss, err := rules.ParseFiles([]string{"testdata/prometheus_rules.yaml"})
5151
require.NoError(t, err)
5252

5353
for _, ns := range nss {

pkg/commands/rules.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ var (
3838
Help: "The timestamp of the last successful rule load.",
3939
})
4040

41-
backends = []string{rules.CortexBackend, rules.LokiBackend} // list of supported backend types
42-
formats = []string{"json", "yaml", "table"} // list of supported formats for the list command
41+
formats = []string{"json", "yaml", "table"} // list of supported formats for the list command
4342
)
4443

4544
// RuleCommand configures and executes rule related cortex operations
@@ -48,9 +47,6 @@ type RuleCommand struct {
4847

4948
cli *client.CortexClient
5049

51-
// Backend type (cortex | loki)
52-
Backend string
53-
5450
// Get Rule Groups Configs
5551
Namespace string
5652
RuleGroup string
@@ -93,7 +89,6 @@ func (r *RuleCommand) Register(app *kingpin.Application) {
9389
rulesCmd.Flag("authToken", "Authentication token for bearer token or JWT auth, alternatively set CORTEX_AUTH_TOKEN.").Default("").Envar("CORTEX_AUTH_TOKEN").StringVar(&r.ClientConfig.AuthToken)
9490
rulesCmd.Flag("user", "API user to use when contacting cortex, alternatively set CORTEX_API_USER. If empty, CORTEX_TENANT_ID will be used instead.").Default("").Envar("CORTEX_API_USER").StringVar(&r.ClientConfig.User)
9591
rulesCmd.Flag("key", "API key to use when contacting cortex, alternatively set CORTEX_API_KEY.").Default("").Envar("CORTEX_API_KEY").StringVar(&r.ClientConfig.Key)
96-
rulesCmd.Flag("backend", "Backend type to interact with: <cortex|loki>").Default("cortex").EnumVar(&r.Backend, backends...)
9792

9893
// Register rule commands
9994
listCmd := rulesCmd.
@@ -241,11 +236,6 @@ func (r *RuleCommand) setup(k *kingpin.ParseContext) error {
241236
ruleLoadSuccessTimestamp,
242237
)
243238

244-
// Loki's non-legacy route does not match Cortex, but the legacy one does.
245-
if r.Backend == rules.LokiBackend {
246-
r.ClientConfig.UseLegacyRoutes = true
247-
}
248-
249239
cli, err := client.New(r.ClientConfig)
250240
if err != nil {
251241
return err
@@ -378,7 +368,7 @@ func (r *RuleCommand) deleteRuleGroup(k *kingpin.ParseContext) error {
378368
}
379369

380370
func (r *RuleCommand) loadRules(k *kingpin.ParseContext) error {
381-
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
371+
nss, err := rules.ParseFiles(r.RuleFilesList)
382372
if err != nil {
383373
return errors.Wrap(err, "load operation unsuccessful, unable to parse rules files")
384374
}
@@ -440,7 +430,7 @@ func (r *RuleCommand) diffRules(k *kingpin.ParseContext) error {
440430
return errors.Wrap(err, "diff operation unsuccessful, unable to load rules files")
441431
}
442432

443-
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
433+
nss, err := rules.ParseFiles(r.RuleFilesList)
444434
if err != nil {
445435
return errors.Wrap(err, "diff operation unsuccessful, unable to parse rules files")
446436
}
@@ -503,7 +493,7 @@ func (r *RuleCommand) syncRules(k *kingpin.ParseContext) error {
503493
return errors.Wrap(err, "sync operation unsuccessful, unable to load rules files")
504494
}
505495

506-
nss, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
496+
nss, err := rules.ParseFiles(r.RuleFilesList)
507497
if err != nil {
508498
return errors.Wrap(err, "sync operation unsuccessful, unable to parse rules files")
509499
}
@@ -625,7 +615,7 @@ func (r *RuleCommand) prepare(k *kingpin.ParseContext) error {
625615
return errors.Wrap(err, "prepare operation unsuccessful, unable to load rules files")
626616
}
627617

628-
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
618+
namespaces, err := rules.ParseFiles(r.RuleFilesList)
629619
if err != nil {
630620
return errors.Wrap(err, "prepare operation unsuccessful, unable to parse rules files")
631621
}
@@ -663,14 +653,14 @@ func (r *RuleCommand) lint(k *kingpin.ParseContext) error {
663653
return errors.Wrap(err, "prepare operation unsuccessful, unable to load rules files")
664654
}
665655

666-
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
656+
namespaces, err := rules.ParseFiles(r.RuleFilesList)
667657
if err != nil {
668658
return errors.Wrap(err, "prepare operation unsuccessful, unable to parse rules files")
669659
}
670660

671661
var count, mod int
672662
for _, ruleNamespace := range namespaces {
673-
c, m, err := ruleNamespace.LintExpressions(r.Backend)
663+
c, m, err := ruleNamespace.LintExpressions()
674664
if err != nil {
675665
return err
676666
}
@@ -697,7 +687,7 @@ func (r *RuleCommand) checkRecordingRuleNames(k *kingpin.ParseContext) error {
697687
return errors.Wrap(err, "check operation unsuccessful, unable to load rules files")
698688
}
699689

700-
namespaces, err := rules.ParseFiles(r.Backend, r.RuleFilesList)
690+
namespaces, err := rules.ParseFiles(r.RuleFilesList)
701691
if err != nil {
702692
return errors.Wrap(err, "check operation unsuccessful, unable to parse rules files")
703693
}

pkg/rules/parser.go

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,20 @@ import (
88
"path/filepath"
99
"strings"
1010

11-
"github.com/grafana/loki/pkg/ruler"
12-
"github.com/prometheus/prometheus/pkg/rulefmt"
1311
log "github.com/sirupsen/logrus"
1412
yaml "gopkg.in/yaml.v3"
1513
)
1614

17-
const (
18-
CortexBackend = "cortex"
19-
LokiBackend = "loki"
20-
)
21-
2215
var (
23-
errFileReadError = errors.New("file read error")
24-
errInvalidBackend = errors.New("invalid backend type")
16+
errFileReadError = errors.New("file read error")
2517
)
2618

2719
// ParseFiles returns a formatted set of prometheus rule groups
28-
func ParseFiles(backend string, files []string) (map[string]RuleNamespace, error) {
20+
func ParseFiles(files []string) (map[string]RuleNamespace, error) {
2921
ruleSet := map[string]RuleNamespace{}
30-
var parseFn func(f string) ([]RuleNamespace, []error)
31-
switch backend {
32-
case CortexBackend:
33-
parseFn = Parse
34-
case LokiBackend:
35-
parseFn = ParseLoki
36-
default:
37-
return nil, errInvalidBackend
38-
}
3922

4023
for _, f := range files {
41-
nss, errs := parseFn(f)
24+
nss, errs := Parse(f)
4225
for _, err := range errs {
4326
log.WithError(err).WithField("file", f).Errorln("unable parse rules file")
4427
return nil, errFileReadError
@@ -104,44 +87,6 @@ func ParseBytes(content []byte) ([]RuleNamespace, []error) {
10487
return nss, nil
10588
}
10689

107-
func ParseLoki(f string) ([]RuleNamespace, []error) {
108-
content, err := loadFile(f)
109-
if err != nil {
110-
log.WithError(err).WithField("file", f).Errorln("unable load rules file")
111-
return nil, []error{errFileReadError}
112-
}
113-
114-
decoder := yaml.NewDecoder(bytes.NewReader(content))
115-
decoder.KnownFields(true)
116-
117-
var nss []RuleNamespace
118-
for {
119-
var ns RuleNamespace
120-
err := decoder.Decode(&ns)
121-
if err == io.EOF {
122-
break
123-
}
124-
if err != nil {
125-
return nil, []error{err}
126-
}
127-
128-
// the upstream loki validator only validates the rulefmt rule groups,
129-
// not the remote write configs this type attaches.
130-
var grps []rulefmt.RuleGroup
131-
for _, g := range ns.Groups {
132-
grps = append(grps, g.RuleGroup)
133-
}
134-
135-
if errs := ruler.ValidateGroups(grps...); len(errs) > 0 {
136-
return nil, errs
137-
}
138-
139-
nss = append(nss, ns)
140-
141-
}
142-
return nss, nil
143-
}
144-
14590
func loadFile(filename string) ([]byte, error) {
14691
file, err := os.Open(filename)
14792
if err != nil {

pkg/rules/parser_test.go

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ import (
1212
func TestParseFiles(t *testing.T) {
1313
tests := []struct {
1414
name string
15-
backend string
1615
files []string
1716
want map[string]RuleNamespace
1817
wantErr bool
1918
}{
2019
{
21-
name: "basic_file",
22-
backend: CortexBackend,
20+
name: "basic_file",
2321
files: []string{
2422
"testdata/basic_namespace.yaml",
2523
},
@@ -42,17 +40,15 @@ func TestParseFiles(t *testing.T) {
4240
},
4341
},
4442
{
45-
name: "file_namespace_overlap",
46-
backend: CortexBackend,
43+
name: "file_namespace_overlap",
4744
files: []string{
4845
"testdata/basic_namespace.yaml",
4946
"testdata/basic_namespace_repeated.yaml",
5047
},
5148
wantErr: true,
5249
},
5350
{
54-
name: "multiple_namespace_file",
55-
backend: CortexBackend,
51+
name: "multiple_namespace_file",
5652
files: []string{
5753
"testdata/multiple_namespace.yaml",
5854
},
@@ -89,106 +85,11 @@ func TestParseFiles(t *testing.T) {
8985
},
9086
},
9187
},
92-
{
93-
name: "basic_loki_file",
94-
backend: LokiBackend,
95-
files: []string{
96-
"testdata/loki_basic.yaml",
97-
},
98-
want: map[string]RuleNamespace{
99-
"loki_basic": {
100-
Namespace: "loki_basic",
101-
Groups: []rwrulefmt.RuleGroup{
102-
{
103-
RuleGroup: rulefmt.RuleGroup{
104-
Name: "testgrp2",
105-
Rules: []rulefmt.RuleNode{
106-
{
107-
// currently the tests only check length
108-
},
109-
},
110-
},
111-
},
112-
},
113-
},
114-
},
115-
},
116-
{
117-
name: "basic_loki_namespace",
118-
backend: LokiBackend,
119-
files: []string{
120-
"testdata/loki_basic_namespace.yaml",
121-
},
122-
want: map[string]RuleNamespace{
123-
"foo": {
124-
Namespace: "foo",
125-
Groups: []rwrulefmt.RuleGroup{
126-
{
127-
RuleGroup: rulefmt.RuleGroup{
128-
Name: "testgrp2",
129-
Rules: []rulefmt.RuleNode{
130-
{
131-
// currently the tests only check length
132-
},
133-
},
134-
},
135-
},
136-
},
137-
},
138-
},
139-
},
140-
{
141-
name: "basic_loki_failure",
142-
backend: LokiBackend,
143-
files: []string{
144-
"testdata/loki_basic_failure.yaml",
145-
},
146-
wantErr: true,
147-
},
148-
{
149-
name: "multiple_loki_namespace",
150-
backend: LokiBackend,
151-
files: []string{
152-
"testdata/loki_multiple_namespace.yaml",
153-
},
154-
want: map[string]RuleNamespace{
155-
"foo": {
156-
Namespace: "foo",
157-
Groups: []rwrulefmt.RuleGroup{
158-
{
159-
RuleGroup: rulefmt.RuleGroup{
160-
Name: "testgrp2",
161-
Rules: []rulefmt.RuleNode{
162-
{
163-
// currently the tests only check length
164-
},
165-
},
166-
},
167-
},
168-
},
169-
},
170-
"other_foo": {
171-
Namespace: "other_foo",
172-
Groups: []rwrulefmt.RuleGroup{
173-
{
174-
RuleGroup: rulefmt.RuleGroup{
175-
Name: "other_testgrp2",
176-
Rules: []rulefmt.RuleNode{
177-
{
178-
// currently the tests only check length
179-
},
180-
},
181-
},
182-
},
183-
},
184-
},
185-
},
186-
},
18788
}
18889

18990
for _, tt := range tests {
19091
t.Run(tt.name, func(t *testing.T) {
191-
got, err := ParseFiles(tt.backend, tt.files)
92+
got, err := ParseFiles(tt.files)
19293
if (err != nil) != tt.wantErr {
19394
t.Errorf("ParseFiles() error = %v, wantErr %v", err, tt.wantErr)
19495
return

0 commit comments

Comments
 (0)