From b6a71563008f92db7ffb2457172aa7d011ae5033 Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Wed, 21 Jul 2021 10:04:40 -0400 Subject: [PATCH] Alerting: Handle marshaling Inf values (#36947) * SSE: change EvalMatch json value encoding to string * update MarshalJSON on null.Float to handle inf as null fixes #36424 --- pkg/components/null/float.go | 4 ++-- pkg/expr/classic/classic.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/components/null/float.go b/pkg/components/null/float.go index 8dd87f03f13..64bcf43781f 100644 --- a/pkg/components/null/float.go +++ b/pkg/components/null/float.go @@ -98,9 +98,9 @@ func (f *Float) UnmarshalText(text []byte) error { } // MarshalJSON implements json.Marshaler. -// It will encode null if this Float is null. +// It will encode null if this Float is null, NaN, of Inf. func (f Float) MarshalJSON() ([]byte, error) { - if !f.Valid || math.IsNaN(f.Float64) { + if !f.Valid || math.IsNaN(f.Float64) || math.IsInf(f.Float64, 0) { return []byte(nullString), nil } return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil diff --git a/pkg/expr/classic/classic.go b/pkg/expr/classic/classic.go index 66b22d9014b..830d1ba1f1c 100644 --- a/pkg/expr/classic/classic.go +++ b/pkg/expr/classic/classic.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strconv" "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana/pkg/expr/mathexp" @@ -61,12 +62,29 @@ func (ccc *ConditionsCmd) NeedsVars() []string { } // EvalMatch represents the series violating the threshold. +// It goes into the metadata of data frames so it can be extracted. type EvalMatch struct { Value *float64 `json:"value"` Metric string `json:"metric"` Labels data.Labels `json:"labels"` } +func (em EvalMatch) MarshalJSON() ([]byte, error) { + fs := "" + if em.Value != nil { + fs = strconv.FormatFloat(*em.Value, 'f', -1, 64) + } + return json.Marshal(struct { + Value string `json:"value"` + Metric string `json:"metric"` + Labels data.Labels `json:"labels"` + }{ + fs, + em.Metric, + em.Labels, + }) +} + // Execute runs the command and returns the results or an error if the command // failed to execute. func (ccc *ConditionsCmd) Execute(ctx context.Context, vars mathexp.Vars) (mathexp.Results, error) {