From beaa7d5a431d7b1f282059896d4019bfdcb7d346 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Thu, 13 Apr 2017 10:33:08 +0100 Subject: [PATCH] Move consistent NaN logic into the parser. --- pkg/textparse/lex.l | 5 ++++- pkg/textparse/lex.l.go | 4 +++- pkg/value/value.go | 4 ++-- retrieval/scrape.go | 10 ---------- retrieval/scrape_test.go | 7 ++++--- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pkg/textparse/lex.l b/pkg/textparse/lex.l index 4a0b45e10..8cd009cea 100644 --- a/pkg/textparse/lex.l +++ b/pkg/textparse/lex.l @@ -18,6 +18,8 @@ import ( "fmt" "math" "strconv" + + "github.com/prometheus/prometheus/pkg/value" ) @@ -85,8 +87,9 @@ M [a-zA-Z_:] l.offsets = append(l.offsets, l.i-1) [ \t]+ l.vstart = l.i -(NaN) l.val = math.NaN() +(NaN) l.val = math.Float64frombits(value.NormalNaN) s = lstateTimestamp + [^\n \t\r]+ // We don't parse strictly correct floats as the conversion // repeats the effort anyway. l.val, l.err = strconv.ParseFloat(yoloString(l.b[l.vstart:l.i]), 64) diff --git a/pkg/textparse/lex.l.go b/pkg/textparse/lex.l.go index 9b9e731b0..836b3bbdb 100644 --- a/pkg/textparse/lex.l.go +++ b/pkg/textparse/lex.l.go @@ -19,6 +19,8 @@ import ( "fmt" "math" "strconv" + + "github.com/prometheus/prometheus/pkg/value" ) // Lex is called by the parser generated by "go tool yacc" to obtain each @@ -426,7 +428,7 @@ yyrule12: // [ \t]+ } yyrule13: // (NaN) { - l.val = math.NaN() + l.val = math.Float64frombits(value.NormalNaN) s = lstateTimestamp goto yystate0 } diff --git a/pkg/value/value.go b/pkg/value/value.go index 2f43ecdf2..df847db6d 100644 --- a/pkg/value/value.go +++ b/pkg/value/value.go @@ -14,6 +14,6 @@ package value var ( - normalNaN uint64 = 0x7ff8000000000001 // A quiet NaN. This is also math.NaN(). - staleNaN uint64 = 0x7ff4000000000000 // A signalling NaN, starting 01 to allow for expansion. + NormalNaN uint64 = 0x7ff8000000000001 // A quiet NaN. This is also math.NaN(). + StaleNaN uint64 = 0x7ff4000000000000 // A signalling NaN, starting 01 to allow for expansion. ) diff --git a/retrieval/scrape.go b/retrieval/scrape.go index 0d152df85..8258892b8 100644 --- a/retrieval/scrape.go +++ b/retrieval/scrape.go @@ -19,7 +19,6 @@ import ( "compress/gzip" "fmt" "io" - "math" "net/http" "sync" "time" @@ -47,11 +46,6 @@ const ( samplesPostRelabelMetricName = "scrape_samples_post_metric_relabeling" ) -var ( - normalNaN uint64 = 0x7ff8000000000001 // A quiet NaN. This is also math.NaN(). - staleNaN uint64 = 0x7ff4000000000000 // A signalling NaN, starting 01 to allow for expansion. -) - var ( targetIntervalLength = prometheus.NewSummaryVec( prometheus.SummaryOpts{ @@ -542,10 +536,6 @@ loop: t := defTime met, tp, v := p.At() - // Normalise actual NaNs to one bit representation. - if math.IsNaN(v) { - v = math.Float64frombits(normalNaN) - } if tp != nil { t = *tp } diff --git a/retrieval/scrape_test.go b/retrieval/scrape_test.go index 0c06e4b2e..23097215b 100644 --- a/retrieval/scrape_test.go +++ b/retrieval/scrape_test.go @@ -18,10 +18,10 @@ import ( "fmt" "io" "io/ioutil" - "math" "net/http" "net/http/httptest" "net/url" + "math" "reflect" "strings" "sync" @@ -35,6 +35,7 @@ import ( "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" + "github.com/prometheus/prometheus/pkg/value" "github.com/prometheus/prometheus/storage" ) @@ -451,8 +452,8 @@ func TestScrapeLoopAppend(t *testing.T) { } ingestedNaN := math.Float64bits(app.result[1].v) - if ingestedNaN != normalNaN { - t.Fatalf("Appended NaN samples wasn't as expected. Wanted: %x Got: %x", normalNaN, ingestedNaN) + if ingestedNaN != value.NormalNaN { + t.Fatalf("Appended NaN samples wasn't as expected. Wanted: %x Got: %x", value.NormalNaN, ingestedNaN) } // DeepEqual will report NaNs as being different, so replace with a different value.