From 544f65f7fc2ab8d5c7b0ec8f3409c8e95efd1868 Mon Sep 17 00:00:00 2001 From: David Nesting Date: Mon, 9 Nov 2015 17:14:07 -0500 Subject: [PATCH] metricSorter stops relying on time.Now and respects nil timestamps --- prometheus/registry.go | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/prometheus/registry.go b/prometheus/registry.go index 9000caa..65c534a 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -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 -}