Use simple Now() func in API instead of utility.Time.

This commit is contained in:
Julius Volz 2015-03-27 23:43:47 +01:00
parent a5a553f1da
commit 33702da8a8
5 changed files with 18 additions and 27 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/golang/glog"
clientmodel "github.com/prometheus/client_golang/model"
registry "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/config"
@ -167,6 +168,7 @@ func NewPrometheus() *prometheus {
}
metricsService := &api.MetricsService{
Now: clientmodel.Now,
Storage: memStorage,
}

View File

@ -18,14 +18,15 @@ import (
"github.com/prometheus/client_golang/prometheus"
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/prometheus/storage/local"
"github.com/prometheus/prometheus/utility"
"github.com/prometheus/prometheus/web/httputils"
)
// MetricsService manages the /api HTTP endpoint.
type MetricsService struct {
nower utility.Time
Now func() clientmodel.Timestamp
Storage local.Storage
}

View File

@ -24,17 +24,8 @@ import (
clientmodel "github.com/prometheus/client_golang/model"
"github.com/prometheus/prometheus/storage/local"
"github.com/prometheus/prometheus/utility"
)
type testInstantProvider struct {
now time.Time
}
func (p testInstantProvider) Now() time.Time {
return p.now
}
// This is a bit annoying. On one hand, we have to choose a current timestamp
// because the storage doesn't have a mocked-out time yet and would otherwise
// immediately throw away "old" samples. On the other hand, we have to make
@ -44,10 +35,8 @@ func (p testInstantProvider) Now() time.Time {
// parsing/re-formatting.
var testTimestamp = clientmodel.TimestampFromTime(time.Now().Round(time.Second)).Add(124 * time.Millisecond)
var testNower = utility.Time{
Provider: testInstantProvider{
now: testTimestamp.Time(),
},
func testNow() clientmodel.Timestamp {
return testTimestamp
}
func TestQuery(t *testing.T) {
@ -103,6 +92,7 @@ func TestQuery(t *testing.T) {
storage.WaitForIndexing()
api := MetricsService{
Now: testNow,
Storage: storage,
}
api.RegisterHandler()

View File

@ -29,7 +29,6 @@ import (
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/rules/ast"
"github.com/prometheus/prometheus/stats"
"github.com/prometheus/prometheus/utility"
"github.com/prometheus/prometheus/web/httputils"
)
@ -47,9 +46,9 @@ func httpJSONError(w http.ResponseWriter, err error, code int) {
fmt.Fprintln(w, ast.ErrorToJSON(err))
}
func parseTimestampOrNow(t string, nower utility.Time) (clientmodel.Timestamp, error) {
func parseTimestampOrNow(t string, now clientmodel.Timestamp) (clientmodel.Timestamp, error) {
if t == "" {
return clientmodel.TimestampFromTime(nower.Now()), nil
return now, nil
}
tFloat, err := strconv.ParseFloat(t, 64)
@ -75,7 +74,7 @@ func (serv MetricsService) Query(w http.ResponseWriter, r *http.Request) {
params := httputils.GetQueryParams(r)
expr := params.Get("expr")
timestamp, err := parseTimestampOrNow(params.Get("timestamp"), serv.nower)
timestamp, err := parseTimestampOrNow(params.Get("timestamp"), serv.Now())
if err != nil {
httpJSONError(w, fmt.Errorf("invalid query timestamp %s", err), http.StatusBadRequest)
return
@ -113,7 +112,7 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
return
}
end, err := parseTimestampOrNow(params.Get("end"), serv.nower)
end, err := parseTimestampOrNow(params.Get("end"), serv.Now())
if err != nil {
httpJSONError(w, fmt.Errorf("invalid query timestamp: %s", err), http.StatusBadRequest)
return
@ -123,7 +122,7 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
// the current time as the end time. Instead, the "end" parameter should
// simply be omitted or set to an empty string for that case.
if end == 0 {
end = clientmodel.TimestampFromTime(serv.nower.Now())
end = serv.Now()
}
exprNode, err := rules.LoadExprFromString(expr)

View File

@ -21,16 +21,15 @@ import (
)
func TestParseTimestampOrNow(t *testing.T) {
ts, err := parseTimestampOrNow("", testNower)
ts, err := parseTimestampOrNow("", testNow())
if err != nil {
t.Fatalf("err = %s; want nil", err)
}
now := clientmodel.TimestampFromTime(testNower.Now())
if !now.Equal(ts) {
t.Fatalf("ts = %v; want ts = %v", ts, now)
if !testNow().Equal(ts) {
t.Fatalf("ts = %v; want ts = %v", ts, testNow)
}
ts, err = parseTimestampOrNow("1426956073.12345", testNower)
ts, err = parseTimestampOrNow("1426956073.12345", testNow())
if err != nil {
t.Fatalf("err = %s; want nil", err)
}
@ -39,7 +38,7 @@ func TestParseTimestampOrNow(t *testing.T) {
t.Fatalf("ts = %v; want %v", ts, expTS)
}
_, err = parseTimestampOrNow("123.45foo", testNower)
_, err = parseTimestampOrNow("123.45foo", testNow())
if err == nil {
t.Fatalf("err = nil; want %s", err)
}