Support finer time resolutions than 1 second.

Change-Id: I4c5f1d6d2361e841999b23283d1961b1bd0c2859
This commit is contained in:
Julius Volz 2014-10-23 11:47:15 +02:00 committed by Bjoern Rabenstein
parent 0712d738d1
commit c3fcea45e3
3 changed files with 16 additions and 15 deletions

View File

@ -389,7 +389,7 @@ func createRandomSamples(r *rand.Rand) clientmodel.Samples {
result = append(result, &clientmodel.Sample{ result = append(result, &clientmodel.Sample{
Metric: metric, Metric: metric,
Value: createValue(), Value: createValue(),
Timestamp: clientmodel.Timestamp(timestamp), Timestamp: clientmodel.TimestampFromUnix(timestamp),
}) })
incTimestamp() incTimestamp()
case 1: // A streak of random sample values. case 1: // A streak of random sample values.
@ -397,7 +397,7 @@ func createRandomSamples(r *rand.Rand) clientmodel.Samples {
result = append(result, &clientmodel.Sample{ result = append(result, &clientmodel.Sample{
Metric: metric, Metric: metric,
Value: createValue(), Value: createValue(),
Timestamp: clientmodel.Timestamp(timestamp), Timestamp: clientmodel.TimestampFromUnix(timestamp),
}) })
incTimestamp() incTimestamp()
} }
@ -407,7 +407,7 @@ func createRandomSamples(r *rand.Rand) clientmodel.Samples {
result = append(result, &clientmodel.Sample{ result = append(result, &clientmodel.Sample{
Metric: metric, Metric: metric,
Value: value, Value: value,
Timestamp: clientmodel.Timestamp(timestamp), Timestamp: clientmodel.TimestampFromUnix(timestamp),
}) })
incTimestamp() incTimestamp()
value = applyDelta(value) value = applyDelta(value)
@ -418,7 +418,7 @@ func createRandomSamples(r *rand.Rand) clientmodel.Samples {
result = append(result, &clientmodel.Sample{ result = append(result, &clientmodel.Sample{
Metric: metric, Metric: metric,
Value: value, Value: value,
Timestamp: clientmodel.Timestamp(timestamp), Timestamp: clientmodel.TimestampFromUnix(timestamp),
}) })
incTimestamp() incTimestamp()
} }

View File

@ -21,7 +21,7 @@ import (
// MarshalJSON implements json.Marshaler. // MarshalJSON implements json.Marshaler.
func (s SamplePair) MarshalJSON() ([]byte, error) { func (s SamplePair) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("{\"Value\": \"%f\", \"Timestamp\": %d}", s.Value, s.Timestamp)), nil return []byte(fmt.Sprintf("{\"Value\": \"%f\", \"Timestamp\": %s}", s.Value, s.Timestamp.String())), nil
} }
// SamplePair pairs a SampleValue with a Timestamp. // SamplePair pairs a SampleValue with a Timestamp.

View File

@ -78,13 +78,14 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
params := http_utils.GetQueryParams(r) params := http_utils.GetQueryParams(r)
expr := params.Get("expr") expr := params.Get("expr")
// Gracefully handle decimal input, by truncating it. // Input times and durations are in seconds and get converted to nanoseconds.
endFloat, _ := strconv.ParseFloat(params.Get("end"), 64) endFloat, _ := strconv.ParseFloat(params.Get("end"), 64)
durationFloat, _ := strconv.ParseFloat(params.Get("range"), 64) durationFloat, _ := strconv.ParseFloat(params.Get("range"), 64)
stepFloat, _ := strconv.ParseFloat(params.Get("step"), 64) stepFloat, _ := strconv.ParseFloat(params.Get("step"), 64)
end := int64(endFloat) nanosPerSecond := int64(time.Second / time.Nanosecond)
duration := int64(durationFloat) end := int64(endFloat) * nanosPerSecond
step := int64(stepFloat) duration := int64(durationFloat) * nanosPerSecond
step := int64(stepFloat) * nanosPerSecond
exprNode, err := rules.LoadExprFromString(expr) exprNode, err := rules.LoadExprFromString(expr)
if err != nil { if err != nil {
@ -97,11 +98,11 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
} }
if end == 0 { if end == 0 {
end = clientmodel.Now().Unix() end = clientmodel.Now().UnixNano()
} }
if step < 1 { if step <= 0 {
step = 1 step = nanosPerSecond
} }
if end-duration < 0 { if end-duration < 0 {
@ -123,9 +124,9 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
evalTimer := queryStats.GetTimer(stats.TotalEvalTime).Start() evalTimer := queryStats.GetTimer(stats.TotalEvalTime).Start()
matrix, err := ast.EvalVectorRange( matrix, err := ast.EvalVectorRange(
exprNode.(ast.VectorNode), exprNode.(ast.VectorNode),
clientmodel.TimestampFromUnix(end-duration), clientmodel.TimestampFromUnixNano(end-duration),
clientmodel.TimestampFromUnix(end), clientmodel.TimestampFromUnixNano(end),
time.Duration(step)*time.Second, time.Duration(step),
serv.Storage, serv.Storage,
queryStats) queryStats)
if err != nil { if err != nil {