Skip to content

Commit 55358f5

Browse files
committed
Re-order diagnostics to make it more readable
1 parent c65a3c6 commit 55358f5

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

internal/checks/promql_impossible_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,31 @@ func TestImpossibleCheck(t *testing.T) {
417417
sum by (job) (
418418
up{env="prod", job="foo"} * on () group_left(job) services_enabled{job=""}
419419
)
420+
`,
421+
checker: newImpossibleCheck,
422+
prometheus: newSimpleProm,
423+
problems: true,
424+
},
425+
{
426+
description: "join on possible label",
427+
content: `
428+
- record: foo
429+
expr: |
430+
sum by (job) (
431+
up{env="prod"} * on () group_left(job) services_enabled{job!=""}
432+
)
433+
`,
434+
checker: newImpossibleCheck,
435+
prometheus: newSimpleProm,
436+
},
437+
{
438+
description: "join on possible but redundant label",
439+
content: `
440+
- record: foo
441+
expr: |
442+
sum by (job) (
443+
up{env="prod", job="foo"} * on () group_left(job) services_enabled{job!=""}
444+
)
420445
`,
421446
checker: newImpossibleCheck,
422447
prometheus: newSimpleProm,

internal/checks/promql_impossible_test.snap

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,8 +1640,8 @@
16401640
expr: foo / on(instance) group_left(cluster) sum(bar)
16411641
output: |
16421642
3 | expr: foo / on(instance) group_left(cluster) sum(bar)
1643-
^^^^^^^ You can't use `cluster` because this label is not possible here.
16441643
^^^ Query is using aggregation that removes all labels.
1644+
^^^^^^^ You can't use `cluster` because this label is not possible here.
16451645
problem:
16461646
reporter: promql/impossible
16471647
summary: impossible label
@@ -2363,8 +2363,8 @@
23632363
output: |
23642364
4 | sum by (job) (
23652365
5 | up{env="prod", job="foo"} * on () group_left(job) services_enabled{job=""}
2366-
^^^ You can't use `job` because this label is not possible here.
23672366
^^^^^^^^^^^^^^^^^^^^^^^^ Query uses `{job=""}` selector which will filter out any time series with the `job` label set.
2367+
^^^ You can't use `job` because this label is not possible here.
23682368
6 | )
23692369
problem:
23702370
reporter: promql/impossible
@@ -2387,6 +2387,47 @@
23872387

23882388
---
23892389

2390+
[TestImpossibleCheck/join_on_possible_but_redundant_label - 1]
2391+
- description: join on possible but redundant label
2392+
content: |4
2393+
2394+
- record: foo
2395+
expr: |
2396+
sum by (job) (
2397+
up{env="prod", job="foo"} * on () group_left(job) services_enabled{job!=""}
2398+
)
2399+
output: |
2400+
4 | sum by (job) (
2401+
5 | up{env="prod", job="foo"} * on () group_left(job) services_enabled{job!=""}
2402+
^^^ Query is trying to join the `job` label that is already present on the other side of the query.
2403+
^^^^^^^^^^^^^^^^^^^^^^^^^ Query will only return series where these labels are present.
2404+
6 | )
2405+
problem:
2406+
reporter: promql/impossible
2407+
summary: redundant label
2408+
details: ""
2409+
diagnostics:
2410+
- message: Query is trying to join the `job` label that is already present on the other side of the query.
2411+
firstcolumn: 63
2412+
lastcolumn: 65
2413+
kind: 0
2414+
- message: Query will only return series where these labels are present.
2415+
firstcolumn: 18
2416+
lastcolumn: 42
2417+
kind: 1
2418+
lines:
2419+
first: 4
2420+
last: 6
2421+
severity: 1
2422+
anchor: 0
2423+
2424+
---
2425+
2426+
[TestImpossibleCheck/join_on_possible_label - 1]
2427+
[]
2428+
2429+
---
2430+
23902431
[TestImpossibleCheck/joined_label_NOT_ignored_via_ignoring() - 1]
23912432
[]
23922433

@@ -2403,8 +2444,8 @@
24032444
output: |
24042445
4 | (prometheus_ready * on(instance) group_left(version) prometheus_build_info)
24052446
5 | * ignoring(version) prometheus_config_last_reload_successful
2406-
^ This binary operation prevents previously joined label `version` from being added to the results.
24072447
^^^^^^^ Query is using one-to-one vector matching with `ignoring(version)`, all labels included inside `ignoring(...)` will be removed on the results.
2448+
^ This binary operation prevents previously joined label `version` from being added to the results.
24082449
problem:
24092450
reporter: promql/impossible
24102451
summary: orphaned label

internal/diags/problems.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package diags
22

33
import (
4+
"cmp"
45
"fmt"
56
"slices"
67
"strings"
@@ -43,10 +44,17 @@ func lineCoverage(diags []Diagnostic) (lines []int) {
4344
return lines
4445
}
4546

46-
func InjectDiagnostics(content string, diags []Diagnostic, color output.Color) string {
47+
func InjectDiagnostics(content string, d []Diagnostic, color output.Color) string {
48+
diags := make([]Diagnostic, len(d))
49+
copy(diags, d)
50+
4751
lines := lineCoverage(diags)
4852
lastLine := slices.Max(lines)
4953

54+
slices.SortStableFunc(diags, func(a, b Diagnostic) int {
55+
return cmp.Compare(b.FirstColumn, a.FirstColumn)
56+
})
57+
5058
diagPositions := make([]PositionRanges, len(diags))
5159
for i, diag := range diags {
5260
dl := diag.Pos.Len()

internal/diags/problems_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ expr: sum(foo{job="bar"})
4747
},
4848
output: `2 | expr: sum(foo{job="bar"})
4949
3 | / on(a,b)
50-
^^^^^^^ abc
5150
^^^ efg
51+
^^^^^^^ abc
5252
4 | sum(foo)
5353
`,
5454
},
@@ -191,3 +191,22 @@ func TestInjectDiagnosticsKind(t *testing.T) {
191191
`
192192
require.Equal(t, expected, out)
193193
}
194+
195+
func TestInjectDiagnosticsOrder(t *testing.T) {
196+
input := "expr: foo(bar) by()"
197+
diags := []Diagnostic{
198+
{FirstColumn: 1, LastColumn: 13, Message: "this is bad", Kind: Issue},
199+
{FirstColumn: 10, LastColumn: 13, Message: "this is context", Kind: Context},
200+
}
201+
key, val := parseYaml(input)
202+
pos := NewPositionRange(strings.Split(input, "\n"), val, key.Column+2)
203+
for i := range diags {
204+
diags[i].Pos = pos
205+
}
206+
out := InjectDiagnostics(input, diags, output.None)
207+
expected := `1 | expr: foo(bar) by()
208+
^^^^ this is context
209+
^^^^^^^^^^^^^ this is bad
210+
`
211+
require.Equal(t, expected, out)
212+
}

0 commit comments

Comments
 (0)