fix: nan and inf values in formula result (#5733)

This commit is contained in:
Srikanth Chekuri 2024-08-21 17:55:16 +05:30 committed by GitHub
parent a20794040a
commit 072693d57d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 110 deletions

View File

@ -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,

View File

@ -1,7 +1,6 @@
package postprocess
import (
"math"
"reflect"
"testing"
@ -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",
@ -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)
}
@ -835,10 +891,6 @@ func TestFormula(t *testing.T) {
Timestamp: 5,
Value: 1,
},
{
Timestamp: 7,
Value: math.Inf(1),
},
},
},
{
@ -855,10 +907,6 @@ func TestFormula(t *testing.T) {
Timestamp: 2,
Value: 0.6923076923076923,
},
{
Timestamp: 3,
Value: math.Inf(1),
},
{
Timestamp: 4,
Value: 1,
@ -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",
@ -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),
},
},
},
{
@ -1537,10 +1496,6 @@ func TestFormula(t *testing.T) {
Timestamp: 5,
Value: 51,
},
{
Timestamp: 7,
Value: math.Inf(1),
},
},
},
{
@ -1558,10 +1513,6 @@ func TestFormula(t *testing.T) {
Timestamp: 2,
Value: 45.6923076923076923,
},
{
Timestamp: 3,
Value: math.Inf(1),
},
{
Timestamp: 4,
Value: 41,