Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nan and inf values in formula result #5733

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/query-service/postprocess/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func joinAndCalculate(
return nil, fmt.Errorf("expected float64, got %T", newValue)
}

if math.IsNaN(val) || math.IsInf(val, 0) {
continue
}

resultSeries.Points = append(resultSeries.Points, v3.Point{
Timestamp: timestamp,
Value: val,
Expand Down
171 changes: 61 additions & 110 deletions pkg/query-service/postprocess/formula_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package postprocess

import (
"math"
"reflect"
"testing"

Expand Down Expand Up @@ -204,9 +203,10 @@ func TestFindUniqueLabelSets(t *testing.T) {

func TestProcessResults(t *testing.T) {
tests := []struct {
name string
results []*v3.Result
want *v3.Result
name string
results []*v3.Result
want *v3.Result
expression string
}{
{
name: "test1",
Expand Down Expand Up @@ -288,12 +288,68 @@ func TestProcessResults(t *testing.T) {
},
},
},
expression: "A + B",
},
{
name: "test2",
results: []*v3.Result{
{
QueryName: "A",
Series: []*v3.Series{
{
Labels: map[string]string{},
Points: []v3.Point{
{
Timestamp: 1,
Value: 10,
},
{
Timestamp: 2,
Value: 0,
},
},
},
},
},
{
QueryName: "B",
Series: []*v3.Series{
{
Labels: map[string]string{},
Points: []v3.Point{
{
Timestamp: 1,
Value: 0,
},
{
Timestamp: 3,
Value: 10,
},
},
},
},
},
},
want: &v3.Result{
Series: []*v3.Series{
{
Labels: map[string]string{},
Points: []v3.Point{
{
Timestamp: 3,
Value: 0,
},
},
},
},
},
expression: "A/B",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expression, err := govaluate.NewEvaluableExpression("A + B")
expression, err := govaluate.NewEvaluableExpression(tt.expression)
if err != nil {
t.Errorf("Error parsing expression: %v", err)
}
Expand Down Expand Up @@ -835,10 +891,6 @@ func TestFormula(t *testing.T) {
Timestamp: 5,
Value: 1,
},
{
Timestamp: 7,
Value: math.Inf(1),
},
},
},
{
Expand All @@ -855,10 +907,6 @@ func TestFormula(t *testing.T) {
Timestamp: 2,
Value: 0.6923076923076923,
},
{
Timestamp: 3,
Value: math.Inf(1),
},
{
Timestamp: 4,
Value: 1,
Expand Down Expand Up @@ -997,62 +1045,6 @@ func TestFormula(t *testing.T) {
},
want: &v3.Result{
Series: []*v3.Series{
{
Labels: map[string]string{
"host_name": "ip-10-420-69-1",
"state": "running",
},
Points: []v3.Point{
{
Timestamp: 1,
Value: math.Inf(0),
},
{
Timestamp: 2,
Value: math.Inf(0),
},
{
Timestamp: 4,
Value: math.Inf(0),
},
{
Timestamp: 5,
Value: math.Inf(0),
},
{
Timestamp: 7,
Value: math.Inf(0),
},
},
},
{
Labels: map[string]string{
"host_name": "ip-10-420-69-2",
"state": "idle",
},
Points: []v3.Point{
{
Timestamp: 1,
Value: math.Inf(0),
},
{
Timestamp: 2,
Value: math.Inf(0),
},
{
Timestamp: 3,
Value: math.Inf(0),
},
{
Timestamp: 4,
Value: math.Inf(0),
},
{
Timestamp: 5,
Value: math.Inf(0),
},
},
},
{
Labels: map[string]string{
"host_name": "ip-10-420-69-1",
Expand Down Expand Up @@ -1262,39 +1254,6 @@ func TestFormula(t *testing.T) {
Timestamp: 5,
Value: 1,
},
{
Timestamp: 7,
Value: math.Inf(1),
},
},
},
{
Labels: map[string]string{
"host_name": "ip-10-420-69-2",
"state": "idle",
"os.type": "linux",
},
Points: []v3.Point{
{
Timestamp: 1,
Value: math.Inf(0),
},
{
Timestamp: 2,
Value: math.Inf(0),
},
{
Timestamp: 3,
Value: math.Inf(0),
},
{
Timestamp: 4,
Value: math.Inf(0),
},
{
Timestamp: 5,
Value: math.Inf(0),
},
},
},
{
Expand Down Expand Up @@ -1537,10 +1496,6 @@ func TestFormula(t *testing.T) {
Timestamp: 5,
Value: 51,
},
{
Timestamp: 7,
Value: math.Inf(1),
},
},
},
{
Expand All @@ -1558,10 +1513,6 @@ func TestFormula(t *testing.T) {
Timestamp: 2,
Value: 45.6923076923076923,
},
{
Timestamp: 3,
Value: math.Inf(1),
},
{
Timestamp: 4,
Value: 41,
Expand Down
Loading