Move consistent NaN logic into the parser.

This commit is contained in:
Brian Brazil 2017-04-13 10:33:08 +01:00
parent 76acf7b9b1
commit beaa7d5a43
5 changed files with 13 additions and 17 deletions

View file

@ -18,6 +18,8 @@ import (
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
"github.com/prometheus/prometheus/pkg/value"
) )
@ -85,8 +87,9 @@ M [a-zA-Z_:]
l.offsets = append(l.offsets, l.i-1) l.offsets = append(l.offsets, l.i-1)
<lstateValue>[ \t]+ l.vstart = l.i <lstateValue>[ \t]+ l.vstart = l.i
<lstateValue>(NaN) l.val = math.NaN() <lstateValue>(NaN) l.val = math.Float64frombits(value.NormalNaN)
s = lstateTimestamp s = lstateTimestamp
<lstateValue>[^\n \t\r]+ // We don't parse strictly correct floats as the conversion <lstateValue>[^\n \t\r]+ // We don't parse strictly correct floats as the conversion
// repeats the effort anyway. // repeats the effort anyway.
l.val, l.err = strconv.ParseFloat(yoloString(l.b[l.vstart:l.i]), 64) l.val, l.err = strconv.ParseFloat(yoloString(l.b[l.vstart:l.i]), 64)

View file

@ -19,6 +19,8 @@ import (
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
"github.com/prometheus/prometheus/pkg/value"
) )
// Lex is called by the parser generated by "go tool yacc" to obtain each // Lex is called by the parser generated by "go tool yacc" to obtain each
@ -426,7 +428,7 @@ yyrule12: // [ \t]+
} }
yyrule13: // (NaN) yyrule13: // (NaN)
{ {
l.val = math.NaN() l.val = math.Float64frombits(value.NormalNaN)
s = lstateTimestamp s = lstateTimestamp
goto yystate0 goto yystate0
} }

View file

@ -14,6 +14,6 @@
package value package value
var ( var (
normalNaN uint64 = 0x7ff8000000000001 // A quiet NaN. This is also math.NaN(). NormalNaN uint64 = 0x7ff8000000000001 // A quiet NaN. This is also math.NaN().
staleNaN uint64 = 0x7ff4000000000000 // A signalling NaN, starting 01 to allow for expansion. StaleNaN uint64 = 0x7ff4000000000000 // A signalling NaN, starting 01 to allow for expansion.
) )

View file

@ -19,7 +19,6 @@ import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"math"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -47,11 +46,6 @@ const (
samplesPostRelabelMetricName = "scrape_samples_post_metric_relabeling" 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 ( var (
targetIntervalLength = prometheus.NewSummaryVec( targetIntervalLength = prometheus.NewSummaryVec(
prometheus.SummaryOpts{ prometheus.SummaryOpts{
@ -542,10 +536,6 @@ loop:
t := defTime t := defTime
met, tp, v := p.At() met, tp, v := p.At()
// Normalise actual NaNs to one bit representation.
if math.IsNaN(v) {
v = math.Float64frombits(normalNaN)
}
if tp != nil { if tp != nil {
t = *tp t = *tp
} }

View file

@ -18,10 +18,10 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"math"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
@ -35,6 +35,7 @@ import (
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/pkg/value"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
) )
@ -451,8 +452,8 @@ func TestScrapeLoopAppend(t *testing.T) {
} }
ingestedNaN := math.Float64bits(app.result[1].v) ingestedNaN := math.Float64bits(app.result[1].v)
if ingestedNaN != normalNaN { if ingestedNaN != value.NormalNaN {
t.Fatalf("Appended NaN samples wasn't as expected. Wanted: %x Got: %x", normalNaN, ingestedNaN) 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. // DeepEqual will report NaNs as being different, so replace with a different value.