Skip to content

Commit

Permalink
Improve error message formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Korn <charles.korn@grafana.com>
  • Loading branch information
charleskorn committed Mar 22, 2024
1 parent 165b7c3 commit f3e4657
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
27 changes: 13 additions & 14 deletions promql/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,30 +535,30 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
}

if len(expectedFloats) != len(s.Floats) || len(expectedHistograms) != len(s.Histograms) {
return fmt.Errorf("expected %v float points and %v histogram points for %s, but got %v float points %v and %v histogram points %v", len(expectedFloats), len(expectedHistograms), ev.metrics[hash], len(s.Floats), s.Floats, len(s.Histograms), s.Histograms)
return fmt.Errorf("expected %v float points and %v histogram points for %s, but got %s", len(expectedFloats), len(expectedHistograms), ev.metrics[hash], formatSeriesResult(s))
}

for i, expected := range expectedFloats {
actual := s.Floats[i]

if expected.T != actual.T {
return fmt.Errorf("expected float value at index %v for %s to have timestamp %v, but it had timestamp %v in result with %v float points %v and %v histogram points %v", i, ev.metrics[hash], expected.T, actual.T, len(s.Floats), s.Floats, len(s.Histograms), s.Histograms)
return fmt.Errorf("expected float value at index %v for %s to have timestamp %v, but it had timestamp %v (result has %s)", i, ev.metrics[hash], expected.T, actual.T, formatSeriesResult(s))
}

if !almostEqual(actual.F, expected.F, defaultEpsilon) {
return fmt.Errorf("expected float value at index %v (t=%v) for %s to be %v, but got %v in result with %v float points %v and %v histogram points %v", i, actual.T, ev.metrics[hash], expected.F, actual.F, len(s.Floats), s.Floats, len(s.Histograms), s.Histograms)
return fmt.Errorf("expected float value at index %v (t=%v) for %s to be %v, but got %v (result has %s)", i, actual.T, ev.metrics[hash], expected.F, actual.F, formatSeriesResult(s))
}
}

for i, expected := range expectedHistograms {
actual := s.Histograms[i]

if expected.T != actual.T {
return fmt.Errorf("expected histogram value at index %v for %s to have timestamp %v, but it had timestamp %v in result with %v float points %v and %v histogram points %v", i, ev.metrics[hash], expected.T, actual.T, len(s.Floats), s.Floats, len(s.Histograms), s.Histograms)
return fmt.Errorf("expected histogram value at index %v for %s to have timestamp %v, but it had timestamp %v (result has %s)", i, ev.metrics[hash], expected.T, actual.T, formatSeriesResult(s))
}

if !actual.H.Equals(expected.H) {
return fmt.Errorf("expected histogram value at index %v (t=%v) for %s to be %v, but got %v in result with %v float points %v and %v histogram points %v", i, actual.T, ev.metrics[hash], expected.H, actual.H, len(s.Floats), s.Floats, len(s.Histograms), s.Histograms)
return fmt.Errorf("expected histogram value at index %v (t=%v) for %s to be %v, but got %v (result has %s)", i, actual.T, ev.metrics[hash], expected.H, actual.H, formatSeriesResult(s))
}
}

Expand Down Expand Up @@ -630,20 +630,19 @@ func (ev *evalCmd) compareResult(result parser.Value) error {
return nil
}

func formatFloatAndHistogramPoints(floats []FPoint, histograms []HPoint) string {
if len(histograms) == 0 && len(floats) == 0 {
return "[]"
}
func formatSeriesResult(s Series) string {
floatPlural := "s"
histogramPlural := "s"

if len(histograms) != 0 && len(floats) != 0 {
return fmt.Sprintf("floats: %v, histograms: %v", floats, histograms)
if len(s.Floats) == 1 {
floatPlural = ""
}

if len(histograms) != 0 {
return fmt.Sprintf("%v", histograms)
if len(s.Histograms) == 1 {
histogramPlural = ""
}

return fmt.Sprintf("%v", floats)
return fmt.Sprintf("%v float point%s %v and %v histogram point%s %v", len(s.Floats), floatPlural, s.Floats, len(s.Histograms), histogramPlural, s.Histograms)
}

// HistogramTestExpression returns TestExpression() for the given histogram or "" if the histogram is nil.
Expand Down
8 changes: 4 additions & 4 deletions promql/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ eval range from 0 to 10m step 5m sum by (group) (http_requests)
{group="production"} 0 30 60
{group="canary"} 0 80 140
`,
expectedError: `error in eval sum by (group) (http_requests) (line 8): expected float value at index 1 (t=300000) for {group="canary"} to be 80, but got 70 in result with 3 float points [0 @[0] 70 @[300000] 140 @[600000]] and 0 histogram points []`,
expectedError: `error in eval sum by (group) (http_requests) (line 8): expected float value at index 1 (t=300000) for {group="canary"} to be 80, but got 70 (result has 3 float points [0 @[0] 70 @[300000] 140 @[600000]] and 0 histogram points [])`,
},
"range query with expected histogram values": {
input: `
Expand All @@ -314,7 +314,7 @@ load 5m
eval range from 0 to 10m step 5m testmetric
testmetric {{schema:-1 sum:4 count:1 buckets:[1] offset:1}} {{schema:-1 sum:7 count:1 buckets:[1] offset:1}} {{schema:-1 sum:8 count:1 buckets:[1] offset:1}}
`,
expectedError: `error in eval testmetric (line 5): expected histogram value at index 1 (t=300000) for {__name__="testmetric"} to be {count:1, sum:7, (1,4]:1}, but got {count:1, sum:5, (1,4]:1} in result with 0 float points [] and 3 histogram points [{count:1, sum:4, (1,4]:1} @[0] {count:1, sum:5, (1,4]:1} @[300000] {count:1, sum:6, (1,4]:1} @[600000]]`,
expectedError: `error in eval testmetric (line 5): expected histogram value at index 1 (t=300000) for {__name__="testmetric"} to be {count:1, sum:7, (1,4]:1}, but got {count:1, sum:5, (1,4]:1} (result has 0 float points [] and 3 histogram points [{count:1, sum:4, (1,4]:1} @[0] {count:1, sum:5, (1,4]:1} @[300000] {count:1, sum:6, (1,4]:1} @[600000]])`,
},
"range query with too many points for query time range": {
input: testData + `
Expand All @@ -332,7 +332,7 @@ load 5m
eval range from 0 to 6m step 6m testmetric
testmetric 5 10
`,
expectedError: `error in eval testmetric (line 5): expected 2 float points and 0 histogram points for {__name__="testmetric"}, but got 1 float points [5 @[0]] and 0 histogram points []`,
expectedError: `error in eval testmetric (line 5): expected 2 float points and 0 histogram points for {__name__="testmetric"}, but got 1 float point [5 @[0]] and 0 histogram points []`,
},
"range query with extra point in result": {
input: testData + `
Expand Down Expand Up @@ -432,7 +432,7 @@ load 5m
eval range from 0 to 5m step 5m testmetric
testmetric {{}} 3
`,
expectedError: `error in eval testmetric (line 5): expected float value at index 0 for {__name__="testmetric"} to have timestamp 300000, but it had timestamp 0 in result with 1 float points [3 @[0]] and 1 histogram points [{count:0, sum:0} @[300000]]`,
expectedError: `error in eval testmetric (line 5): expected float value at index 0 for {__name__="testmetric"} to have timestamp 300000, but it had timestamp 0 (result has 1 float point [3 @[0]] and 1 histogram point [{count:0, sum:0} @[300000]])`,
},
}

Expand Down

0 comments on commit f3e4657

Please sign in to comment.