Skip to content

Commit

Permalink
Fix datatype to match prom output (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunjgit authored Aug 30, 2018
1 parent 606981d commit 50bf110
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/query/api/v1/handler/prometheus/native/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ func renderResultsJSON(w io.Writer, series []*ts.Series, params models.RequestPa
}

jw.BeginArray()
jw.WriteFloat64(dp.Value)
jw.WriteInt(int(dp.Timestamp.Unix()))
jw.WriteString(valueToProm(dp.Value))
jw.EndArray()
}
jw.EndArray()
Expand All @@ -190,3 +190,7 @@ func renderResultsJSON(w io.Writer, series []*ts.Series, params models.RequestPa
jw.EndArray()
jw.Close()
}

func valueToProm(value float64) string {
return strconv.FormatFloat(value, 'f', -1, 64)
}
8 changes: 8 additions & 0 deletions src/query/api/v1/handler/prometheus/native/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package native

import (
"math"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -74,3 +75,10 @@ func TestInvalidTarget(t *testing.T) {
assert.NotNil(t, p.Start)
require.Equal(t, err.Code(), http.StatusBadRequest)
}

func TestValueToProm(t *testing.T) {
assert.Equal(t, valueToProm(1.0), "1")
assert.Equal(t, valueToProm(1.2), "1.2")
assert.Equal(t, valueToProm(math.NaN()), "NaN")
assert.Equal(t, valueToProm(0.0119311), "0.0119311")
}

0 comments on commit 50bf110

Please sign in to comment.