From 33702da8a8f28a4738e92c5ae97b1f749ac8def2 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Fri, 27 Mar 2015 23:43:47 +0100 Subject: [PATCH] Use simple Now() func in API instead of utility.Time. --- main.go | 2 ++ web/api/api.go | 5 +++-- web/api/api_test.go | 16 +++------------- web/api/query.go | 11 +++++------ web/api/query_test.go | 11 +++++------ 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index cbc756d39..19e355744 100644 --- a/main.go +++ b/main.go @@ -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, } diff --git a/web/api/api.go b/web/api/api.go index 32c3c375a..0ab6576c9 100644 --- a/web/api/api.go +++ b/web/api/api.go @@ -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 } diff --git a/web/api/api_test.go b/web/api/api_test.go index bfcf598cf..d76f9f7f1 100644 --- a/web/api/api_test.go +++ b/web/api/api_test.go @@ -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() diff --git a/web/api/query.go b/web/api/query.go index 8cae6884c..c4e8f4c81 100644 --- a/web/api/query.go +++ b/web/api/query.go @@ -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) diff --git a/web/api/query_test.go b/web/api/query_test.go index ce691eda0..8ee334e58 100644 --- a/web/api/query_test.go +++ b/web/api/query_test.go @@ -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) }