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