mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 06:04:05 -08:00
Use simple Now() func in API instead of utility.Time.
This commit is contained in:
parent
a5a553f1da
commit
33702da8a8
2
main.go
2
main.go
|
@ -24,6 +24,7 @@ import (
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
|
||||||
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
registry "github.com/prometheus/client_golang/prometheus"
|
registry "github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
|
@ -167,6 +168,7 @@ func NewPrometheus() *prometheus {
|
||||||
}
|
}
|
||||||
|
|
||||||
metricsService := &api.MetricsService{
|
metricsService := &api.MetricsService{
|
||||||
|
Now: clientmodel.Now,
|
||||||
Storage: memStorage,
|
Storage: memStorage,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,15 @@ import (
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/storage/local"
|
"github.com/prometheus/prometheus/storage/local"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
"github.com/prometheus/prometheus/web/httputils"
|
"github.com/prometheus/prometheus/web/httputils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MetricsService manages the /api HTTP endpoint.
|
// MetricsService manages the /api HTTP endpoint.
|
||||||
type MetricsService struct {
|
type MetricsService struct {
|
||||||
nower utility.Time
|
Now func() clientmodel.Timestamp
|
||||||
Storage local.Storage
|
Storage local.Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,17 +24,8 @@ import (
|
||||||
clientmodel "github.com/prometheus/client_golang/model"
|
clientmodel "github.com/prometheus/client_golang/model"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/storage/local"
|
"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
|
// 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
|
// 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
|
// 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.
|
// parsing/re-formatting.
|
||||||
var testTimestamp = clientmodel.TimestampFromTime(time.Now().Round(time.Second)).Add(124 * time.Millisecond)
|
var testTimestamp = clientmodel.TimestampFromTime(time.Now().Round(time.Second)).Add(124 * time.Millisecond)
|
||||||
|
|
||||||
var testNower = utility.Time{
|
func testNow() clientmodel.Timestamp {
|
||||||
Provider: testInstantProvider{
|
return testTimestamp
|
||||||
now: testTimestamp.Time(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQuery(t *testing.T) {
|
func TestQuery(t *testing.T) {
|
||||||
|
@ -103,6 +92,7 @@ func TestQuery(t *testing.T) {
|
||||||
storage.WaitForIndexing()
|
storage.WaitForIndexing()
|
||||||
|
|
||||||
api := MetricsService{
|
api := MetricsService{
|
||||||
|
Now: testNow,
|
||||||
Storage: storage,
|
Storage: storage,
|
||||||
}
|
}
|
||||||
api.RegisterHandler()
|
api.RegisterHandler()
|
||||||
|
|
|
@ -29,7 +29,6 @@ import (
|
||||||
"github.com/prometheus/prometheus/rules"
|
"github.com/prometheus/prometheus/rules"
|
||||||
"github.com/prometheus/prometheus/rules/ast"
|
"github.com/prometheus/prometheus/rules/ast"
|
||||||
"github.com/prometheus/prometheus/stats"
|
"github.com/prometheus/prometheus/stats"
|
||||||
"github.com/prometheus/prometheus/utility"
|
|
||||||
"github.com/prometheus/prometheus/web/httputils"
|
"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))
|
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 == "" {
|
if t == "" {
|
||||||
return clientmodel.TimestampFromTime(nower.Now()), nil
|
return now, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tFloat, err := strconv.ParseFloat(t, 64)
|
tFloat, err := strconv.ParseFloat(t, 64)
|
||||||
|
@ -75,7 +74,7 @@ func (serv MetricsService) Query(w http.ResponseWriter, r *http.Request) {
|
||||||
params := httputils.GetQueryParams(r)
|
params := httputils.GetQueryParams(r)
|
||||||
expr := params.Get("expr")
|
expr := params.Get("expr")
|
||||||
|
|
||||||
timestamp, err := parseTimestampOrNow(params.Get("timestamp"), serv.nower)
|
timestamp, err := parseTimestampOrNow(params.Get("timestamp"), serv.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpJSONError(w, fmt.Errorf("invalid query timestamp %s", err), http.StatusBadRequest)
|
httpJSONError(w, fmt.Errorf("invalid query timestamp %s", err), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
@ -113,7 +112,7 @@ func (serv MetricsService) QueryRange(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
end, err := parseTimestampOrNow(params.Get("end"), serv.nower)
|
end, err := parseTimestampOrNow(params.Get("end"), serv.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpJSONError(w, fmt.Errorf("invalid query timestamp: %s", err), http.StatusBadRequest)
|
httpJSONError(w, fmt.Errorf("invalid query timestamp: %s", err), http.StatusBadRequest)
|
||||||
return
|
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
|
// the current time as the end time. Instead, the "end" parameter should
|
||||||
// simply be omitted or set to an empty string for that case.
|
// simply be omitted or set to an empty string for that case.
|
||||||
if end == 0 {
|
if end == 0 {
|
||||||
end = clientmodel.TimestampFromTime(serv.nower.Now())
|
end = serv.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
exprNode, err := rules.LoadExprFromString(expr)
|
exprNode, err := rules.LoadExprFromString(expr)
|
||||||
|
|
|
@ -21,16 +21,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseTimestampOrNow(t *testing.T) {
|
func TestParseTimestampOrNow(t *testing.T) {
|
||||||
ts, err := parseTimestampOrNow("", testNower)
|
ts, err := parseTimestampOrNow("", testNow())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err = %s; want nil", err)
|
t.Fatalf("err = %s; want nil", err)
|
||||||
}
|
}
|
||||||
now := clientmodel.TimestampFromTime(testNower.Now())
|
if !testNow().Equal(ts) {
|
||||||
if !now.Equal(ts) {
|
t.Fatalf("ts = %v; want ts = %v", ts, testNow)
|
||||||
t.Fatalf("ts = %v; want ts = %v", ts, now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ts, err = parseTimestampOrNow("1426956073.12345", testNower)
|
ts, err = parseTimestampOrNow("1426956073.12345", testNow())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err = %s; want nil", err)
|
t.Fatalf("err = %s; want nil", err)
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,7 @@ func TestParseTimestampOrNow(t *testing.T) {
|
||||||
t.Fatalf("ts = %v; want %v", ts, expTS)
|
t.Fatalf("ts = %v; want %v", ts, expTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = parseTimestampOrNow("123.45foo", testNower)
|
_, err = parseTimestampOrNow("123.45foo", testNow())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("err = nil; want %s", err)
|
t.Fatalf("err = nil; want %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue