metricSorter stops relying on time.Now and respects nil timestamps

This commit is contained in:
David Nesting 2015-11-09 17:14:07 -05:00
parent 5fb1b89678
commit 544f65f7fc
1 changed files with 16 additions and 21 deletions

View File

@ -32,7 +32,6 @@ import (
"sort"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/prometheus/common/expfmt"
@ -723,26 +722,22 @@ func (s metricSorter) Less(i, j int) bool {
return vi < vj
}
}
if s[i].GetTimestampMs() != s[j].GetTimestampMs() {
// While this is not strictly supported, until we have
// an import API some people are having some success
// getting data imported by supplying the same metric
// with different timestamps. At the very least we
// shouldn't make things more difficult, so ensure that
// metrics get ordered by timestamp to make it possible
// for Prometheus to import them.
ti, tj := s[i].GetTimestampMs(), s[j].GetTimestampMs()
if ti == 0 {
ti = nowMillis()
} else if tj == 0 {
tj = nowMillis()
}
return ti < tj
// While this is not strictly supported, until we have an
// import API some people are having some success getting
// data imported by supplying the same metric with different
// timestamps. At the very least we shouldn't make things
// more difficult, so ensure that metrics get ordered by
// timestamp so that Prometheus doesn't complain about
// out-of-order metrics. A missing timestamp (implies "now")
// will be ordered last.
if s[i].TimestampMs == nil && s[j].TimestampMs != nil {
return false
} else if s[i].TimestampMs != nil && s[j].TimestampMs == nil {
return true
} else if s[i].GetTimestampMs() != s[j].GetTimestampMs() {
return s[i].GetTimestampMs() < s[j].GetTimestampMs()
}
return false
}
func nowMillis() int64 {
now := time.Now()
return now.Unix()*1000 + int64(now.Nanosecond())/1000000
}