created docker-prometheus compose file and edited the Prometheus yml file
This commit is contained in:
48
prom/web/api/legacy/api.go
Normal file
48
prom/web/api/legacy/api.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/local"
|
||||
"github.com/prometheus/prometheus/util/httputil"
|
||||
"github.com/prometheus/prometheus/util/route"
|
||||
)
|
||||
|
||||
// API manages the /api HTTP endpoint.
|
||||
type API struct {
|
||||
Now func() clientmodel.Timestamp
|
||||
Storage local.Storage
|
||||
QueryEngine *promql.Engine
|
||||
}
|
||||
|
||||
// RegisterHandler registers the handler for the various endpoints below /api.
|
||||
func (api *API) Register(router *route.Router) {
|
||||
router.Get("/query", handle("query", api.Query))
|
||||
router.Get("/query_range", handle("query_range", api.QueryRange))
|
||||
router.Get("/metrics", handle("metrics", api.Metrics))
|
||||
}
|
||||
|
||||
func handle(name string, f http.HandlerFunc) http.HandlerFunc {
|
||||
h := httputil.CompressionHandler{
|
||||
Handler: f,
|
||||
}
|
||||
return prometheus.InstrumentHandler(name, h)
|
||||
}
|
143
prom/web/api/legacy/api_test.go
Normal file
143
prom/web/api/legacy/api_test.go
Normal file
@ -0,0 +1,143 @@
|
||||
// Copyright 2015 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/local"
|
||||
"github.com/prometheus/prometheus/util/route"
|
||||
)
|
||||
|
||||
// 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
|
||||
// sure that the float value survives the parsing and re-formatting in the
|
||||
// query layer precisely without any change. Thus we round to seconds and then
|
||||
// add known-good digits after the decimal point which behave well in
|
||||
// parsing/re-formatting.
|
||||
var testTimestamp = clientmodel.TimestampFromTime(time.Now().Round(time.Second)).Add(124 * time.Millisecond)
|
||||
|
||||
func testNow() clientmodel.Timestamp {
|
||||
return testTimestamp
|
||||
}
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
// URL query string.
|
||||
queryStr string
|
||||
// Expected HTTP response status code.
|
||||
status int
|
||||
// Regex to match against response body.
|
||||
bodyRe string
|
||||
}{
|
||||
{
|
||||
queryStr: "",
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"error","value":"Parse error at char 1: no expression found in input","version":1}`,
|
||||
},
|
||||
{
|
||||
queryStr: "expr=1.4",
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"scalar","value":"1.4","version":1}`,
|
||||
},
|
||||
{
|
||||
queryStr: "expr=testmetric",
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":\d+\.\d+}\],"version":1}`,
|
||||
},
|
||||
{
|
||||
queryStr: "expr=testmetric×tamp=" + testTimestamp.String(),
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":` + testTimestamp.String() + `}\],"version":1}`,
|
||||
},
|
||||
{
|
||||
queryStr: "expr=testmetric×tamp=" + testTimestamp.Add(-time.Hour).String(),
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"vector","value":\[\],"version":1}`,
|
||||
},
|
||||
{
|
||||
queryStr: "timestamp=invalid",
|
||||
status: http.StatusBadRequest,
|
||||
bodyRe: "invalid query timestamp",
|
||||
},
|
||||
{
|
||||
queryStr: "expr=(badexpression",
|
||||
status: http.StatusOK,
|
||||
bodyRe: `{"type":"error","value":"Parse error at char 15: unclosed left parenthesis","version":1}`,
|
||||
},
|
||||
}
|
||||
|
||||
storage, closer := local.NewTestStorage(t, 1)
|
||||
defer closer.Close()
|
||||
storage.Append(&clientmodel.Sample{
|
||||
Metric: clientmodel.Metric{
|
||||
clientmodel.MetricNameLabel: "testmetric",
|
||||
},
|
||||
Timestamp: testTimestamp,
|
||||
Value: 0,
|
||||
})
|
||||
storage.WaitForIndexing()
|
||||
|
||||
api := &API{
|
||||
Now: testNow,
|
||||
Storage: storage,
|
||||
QueryEngine: promql.NewEngine(storage, nil),
|
||||
}
|
||||
rtr := route.New()
|
||||
api.Register(rtr.WithPrefix("/api"))
|
||||
|
||||
server := httptest.NewServer(rtr)
|
||||
defer server.Close()
|
||||
|
||||
for i, s := range scenarios {
|
||||
// Do query.
|
||||
resp, err := http.Get(server.URL + "/api/query?" + s.queryStr)
|
||||
if err != nil {
|
||||
t.Fatalf("%d. Error querying API: %s", i, err)
|
||||
}
|
||||
|
||||
// Check status code.
|
||||
if resp.StatusCode != s.status {
|
||||
t.Fatalf("%d. Unexpected status code; got %d, want %d", i, resp.StatusCode, s.status)
|
||||
}
|
||||
|
||||
// Check response headers.
|
||||
ct := resp.Header["Content-Type"]
|
||||
if len(ct) != 1 {
|
||||
t.Fatalf("%d. Unexpected number of 'Content-Type' headers; got %d, want 1", i, len(ct))
|
||||
}
|
||||
if ct[0] != "application/json" {
|
||||
t.Fatalf("%d. Unexpected 'Content-Type' header; got %s; want %s", i, ct[0], "application/json")
|
||||
}
|
||||
|
||||
// Check body.
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("%d. Error reading response body: %s", i, err)
|
||||
}
|
||||
re := regexp.MustCompile(s.bodyRe)
|
||||
if !re.Match(b) {
|
||||
t.Fatalf("%d. Body didn't match '%s'. Body: %s", i, s.bodyRe, string(b))
|
||||
}
|
||||
}
|
||||
}
|
277
prom/web/api/legacy/query.go
Normal file
277
prom/web/api/legacy/query.go
Normal file
@ -0,0 +1,277 @@
|
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/log"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
)
|
||||
|
||||
// Enables cross-site script calls.
|
||||
func setAccessControlHeaders(w http.ResponseWriter) {
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Origin")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Expose-Headers", "Date")
|
||||
}
|
||||
|
||||
func httpJSONError(w http.ResponseWriter, err error, code int) {
|
||||
w.WriteHeader(code)
|
||||
errorJSON(w, err)
|
||||
}
|
||||
|
||||
func parseTimestampOrNow(t string, now clientmodel.Timestamp) (clientmodel.Timestamp, error) {
|
||||
if t == "" {
|
||||
return now, nil
|
||||
}
|
||||
|
||||
tFloat, err := strconv.ParseFloat(t, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return clientmodel.TimestampFromUnixNano(int64(tFloat * float64(time.Second/time.Nanosecond))), nil
|
||||
}
|
||||
|
||||
func parseDuration(d string) (time.Duration, error) {
|
||||
dFloat, err := strconv.ParseFloat(d, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time.Duration(dFloat * float64(time.Second/time.Nanosecond)), nil
|
||||
}
|
||||
|
||||
// Query handles the /api/query endpoint.
|
||||
func (api *API) Query(w http.ResponseWriter, r *http.Request) {
|
||||
setAccessControlHeaders(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
params := getQueryParams(r)
|
||||
expr := params.Get("expr")
|
||||
|
||||
timestamp, err := parseTimestampOrNow(params.Get("timestamp"), api.Now())
|
||||
if err != nil {
|
||||
httpJSONError(w, fmt.Errorf("invalid query timestamp %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
query, err := api.QueryEngine.NewInstantQuery(expr, timestamp)
|
||||
if err != nil {
|
||||
httpJSONError(w, err, http.StatusOK)
|
||||
return
|
||||
}
|
||||
res := query.Exec()
|
||||
if res.Err != nil {
|
||||
httpJSONError(w, res.Err, http.StatusOK)
|
||||
return
|
||||
}
|
||||
log.Debugf("Instant query: %s\nQuery stats:\n%s\n", expr, query.Stats())
|
||||
|
||||
if vec, ok := res.Value.(promql.Vector); ok {
|
||||
respondJSON(w, plainVec(vec))
|
||||
return
|
||||
}
|
||||
if sca, ok := res.Value.(*promql.Scalar); ok {
|
||||
respondJSON(w, (*plainScalar)(sca))
|
||||
return
|
||||
}
|
||||
if str, ok := res.Value.(*promql.String); ok {
|
||||
respondJSON(w, (*plainString)(str))
|
||||
return
|
||||
}
|
||||
|
||||
respondJSON(w, res.Value)
|
||||
}
|
||||
|
||||
// plainVec is an indirection that hides the original MarshalJSON method
|
||||
// which does not fit the response format for the legacy API.
|
||||
type plainVec promql.Vector
|
||||
|
||||
func (pv plainVec) MarshalJSON() ([]byte, error) {
|
||||
type plainSmpl promql.Sample
|
||||
|
||||
v := make([]*plainSmpl, len(pv))
|
||||
for i, sv := range pv {
|
||||
v[i] = (*plainSmpl)(sv)
|
||||
}
|
||||
|
||||
return json.Marshal(&v)
|
||||
}
|
||||
|
||||
func (pv plainVec) Type() promql.ExprType {
|
||||
return promql.ExprVector
|
||||
}
|
||||
|
||||
func (pv plainVec) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// plainScalar is an indirection that hides the original MarshalJSON method
|
||||
// which does not fit the response format for the legacy API.
|
||||
type plainScalar promql.Scalar
|
||||
|
||||
func (ps plainScalar) MarshalJSON() ([]byte, error) {
|
||||
s := strconv.FormatFloat(float64(ps.Value), 'f', -1, 64)
|
||||
return json.Marshal(&s)
|
||||
}
|
||||
|
||||
func (plainScalar) Type() promql.ExprType {
|
||||
return promql.ExprScalar
|
||||
}
|
||||
|
||||
func (plainScalar) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// plainString is an indirection that hides the original MarshalJSON method
|
||||
// which does not fit the response format for the legacy API.
|
||||
type plainString promql.String
|
||||
|
||||
func (pv plainString) Type() promql.ExprType {
|
||||
return promql.ExprString
|
||||
}
|
||||
|
||||
func (pv plainString) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// QueryRange handles the /api/query_range endpoint.
|
||||
func (api *API) QueryRange(w http.ResponseWriter, r *http.Request) {
|
||||
setAccessControlHeaders(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
params := getQueryParams(r)
|
||||
expr := params.Get("expr")
|
||||
|
||||
duration, err := parseDuration(params.Get("range"))
|
||||
if err != nil {
|
||||
httpJSONError(w, fmt.Errorf("invalid query range: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
step, err := parseDuration(params.Get("step"))
|
||||
if err != nil {
|
||||
httpJSONError(w, fmt.Errorf("invalid query resolution: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
end, err := parseTimestampOrNow(params.Get("end"), api.Now())
|
||||
if err != nil {
|
||||
httpJSONError(w, fmt.Errorf("invalid query timestamp: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// TODO(julius): Remove this special-case handling a while after PromDash and
|
||||
// other API consumers have been changed to no longer set "end=0" for setting
|
||||
// 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 = api.Now()
|
||||
}
|
||||
|
||||
// For safety, limit the number of returned points per timeseries.
|
||||
// This is sufficient for 60s resolution for a week or 1h resolution for a year.
|
||||
if duration/step > 11000 {
|
||||
err := errors.New("exceeded maximum resolution of 11,000 points per timeseries. Try decreasing the query resolution (?step=XX)")
|
||||
httpJSONError(w, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Align the start to step "tick" boundary.
|
||||
end = end.Add(-time.Duration(end.UnixNano() % int64(step)))
|
||||
start := end.Add(-duration)
|
||||
|
||||
query, err := api.QueryEngine.NewRangeQuery(expr, start, end, step)
|
||||
if err != nil {
|
||||
httpJSONError(w, err, http.StatusOK)
|
||||
return
|
||||
}
|
||||
matrix, err := query.Exec().Matrix()
|
||||
if err != nil {
|
||||
httpJSONError(w, err, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("Range query: %s\nQuery stats:\n%s\n", expr, query.Stats())
|
||||
respondJSON(w, matrix)
|
||||
}
|
||||
|
||||
// Metrics handles the /api/metrics endpoint.
|
||||
func (api *API) Metrics(w http.ResponseWriter, r *http.Request) {
|
||||
setAccessControlHeaders(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
metricNames := api.Storage.LabelValuesForLabelName(clientmodel.MetricNameLabel)
|
||||
sort.Sort(metricNames)
|
||||
resultBytes, err := json.Marshal(metricNames)
|
||||
if err != nil {
|
||||
log.Error("Error marshalling metric names: ", err)
|
||||
httpJSONError(w, fmt.Errorf("Error marshalling metric names: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Write(resultBytes)
|
||||
}
|
||||
|
||||
// GetQueryParams calls r.ParseForm and returns r.Form.
|
||||
func getQueryParams(r *http.Request) url.Values {
|
||||
r.ParseForm()
|
||||
return r.Form
|
||||
}
|
||||
|
||||
var jsonFormatVersion = 1
|
||||
|
||||
// ErrorJSON writes the given error JSON-formatted to w.
|
||||
func errorJSON(w io.Writer, err error) error {
|
||||
data := struct {
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
Version int `json:"version"`
|
||||
}{
|
||||
Type: "error",
|
||||
Value: err.Error(),
|
||||
Version: jsonFormatVersion,
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(data)
|
||||
}
|
||||
|
||||
// RespondJSON converts the given data value to JSON and writes it to w.
|
||||
func respondJSON(w io.Writer, val promql.Value) error {
|
||||
data := struct {
|
||||
Type string `json:"type"`
|
||||
Value interface{} `json:"value"`
|
||||
Version int `json:"version"`
|
||||
}{
|
||||
Type: val.Type().String(),
|
||||
Value: val,
|
||||
Version: jsonFormatVersion,
|
||||
}
|
||||
// TODO(fabxc): Adding MarshalJSON to promql.Values might be a good idea.
|
||||
if sc, ok := val.(*promql.Scalar); ok {
|
||||
data.Value = sc.Value
|
||||
}
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(data)
|
||||
}
|
66
prom/web/api/legacy/query_test.go
Normal file
66
prom/web/api/legacy/query_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright 2015 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
)
|
||||
|
||||
func TestParseTimestampOrNow(t *testing.T) {
|
||||
ts, err := parseTimestampOrNow("", testNow())
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
if !testNow().Equal(ts) {
|
||||
t.Fatalf("ts = %v; want ts = %v", ts, testNow)
|
||||
}
|
||||
|
||||
ts, err = parseTimestampOrNow("1426956073.12345", testNow())
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
expTS := clientmodel.TimestampFromUnixNano(1426956073123000000)
|
||||
if !ts.Equal(expTS) {
|
||||
t.Fatalf("ts = %v; want %v", ts, expTS)
|
||||
}
|
||||
|
||||
_, err = parseTimestampOrNow("123.45foo", testNow())
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDuration(t *testing.T) {
|
||||
_, err := parseDuration("")
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
|
||||
_, err = parseDuration("1234.56foo")
|
||||
if err == nil {
|
||||
t.Fatalf("err = nil; want %s", err)
|
||||
}
|
||||
|
||||
d, err := parseDuration("1234.56789")
|
||||
if err != nil {
|
||||
t.Fatalf("err = %s; want nil", err)
|
||||
}
|
||||
expD := time.Duration(1234.56789 * float64(time.Second))
|
||||
if d != expD {
|
||||
t.Fatalf("d = %v; want %v", d, expD)
|
||||
}
|
||||
}
|
287
prom/web/api/v1/api.go
Normal file
287
prom/web/api/v1/api.go
Normal file
@ -0,0 +1,287 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/local"
|
||||
"github.com/prometheus/prometheus/util/route"
|
||||
"github.com/prometheus/prometheus/util/strutil"
|
||||
)
|
||||
|
||||
type status string
|
||||
|
||||
const (
|
||||
statusSuccess status = "success"
|
||||
statusError = "error"
|
||||
)
|
||||
|
||||
type errorType string
|
||||
|
||||
const (
|
||||
errorNone errorType = ""
|
||||
errorTimeout = "timeout"
|
||||
errorCanceled = "canceled"
|
||||
errorExec = "execution"
|
||||
errorBadData = "bad_data"
|
||||
)
|
||||
|
||||
type apiError struct {
|
||||
typ errorType
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *apiError) Error() string {
|
||||
return fmt.Sprintf("%s: %s", e.typ, e.err)
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Status status `json:"status"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
ErrorType errorType `json:"errorType,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// API can register a set of endpoints in a router and handle
|
||||
// them using the provided storage and query engine.
|
||||
type API struct {
|
||||
Storage local.Storage
|
||||
QueryEngine *promql.Engine
|
||||
|
||||
context func(r *http.Request) context.Context
|
||||
}
|
||||
|
||||
// Enables cross-site script calls.
|
||||
func setCORS(w http.ResponseWriter) {
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Origin")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Expose-Headers", "Date")
|
||||
}
|
||||
|
||||
type apiFunc func(r *http.Request) (interface{}, *apiError)
|
||||
|
||||
// Register the API's endpoints in the given router.
|
||||
func (api *API) Register(r *route.Router) {
|
||||
if api.context == nil {
|
||||
api.context = route.Context
|
||||
}
|
||||
|
||||
instr := func(name string, f apiFunc) http.HandlerFunc {
|
||||
return prometheus.InstrumentHandlerFunc(name, func(w http.ResponseWriter, r *http.Request) {
|
||||
setCORS(w)
|
||||
if data, err := f(r); err != nil {
|
||||
respondError(w, err, data)
|
||||
} else {
|
||||
respond(w, data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
r.Get("/query", instr("query", api.query))
|
||||
r.Get("/query_range", instr("query_range", api.queryRange))
|
||||
|
||||
r.Get("/label/:name/values", instr("label_values", api.labelValues))
|
||||
|
||||
r.Get("/series", instr("series", api.series))
|
||||
r.Del("/series", instr("drop_series", api.dropSeries))
|
||||
}
|
||||
|
||||
type queryData struct {
|
||||
ResultType promql.ExprType `json:"resultType"`
|
||||
Result promql.Value `json:"result"`
|
||||
}
|
||||
|
||||
func (api *API) query(r *http.Request) (interface{}, *apiError) {
|
||||
ts, err := parseTime(r.FormValue("time"))
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
qry, err := api.QueryEngine.NewInstantQuery(r.FormValue("query"), ts)
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
res := qry.Exec()
|
||||
if res.Err != nil {
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
return nil, &apiError{errorCanceled, res.Err}
|
||||
case promql.ErrQueryTimeout:
|
||||
return nil, &apiError{errorTimeout, res.Err}
|
||||
}
|
||||
return nil, &apiError{errorExec, res.Err}
|
||||
}
|
||||
return &queryData{
|
||||
ResultType: res.Value.Type(),
|
||||
Result: res.Value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (api *API) queryRange(r *http.Request) (interface{}, *apiError) {
|
||||
start, err := parseTime(r.FormValue("start"))
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
end, err := parseTime(r.FormValue("end"))
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
step, err := parseDuration(r.FormValue("step"))
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
// For safety, limit the number of returned points per timeseries.
|
||||
// This is sufficient for 60s resolution for a week or 1h resolution for a year.
|
||||
if end.Sub(start)/step > 11000 {
|
||||
err := errors.New("exceeded maximum resolution of 11,000 points per timeseries. Try decreasing the query resolution (?step=XX)")
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
qry, err := api.QueryEngine.NewRangeQuery(r.FormValue("query"), start, end, step)
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
res := qry.Exec()
|
||||
if res.Err != nil {
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
return nil, &apiError{errorCanceled, res.Err}
|
||||
case promql.ErrQueryTimeout:
|
||||
return nil, &apiError{errorTimeout, res.Err}
|
||||
}
|
||||
return nil, &apiError{errorExec, res.Err}
|
||||
}
|
||||
return &queryData{
|
||||
ResultType: res.Value.Type(),
|
||||
Result: res.Value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (api *API) labelValues(r *http.Request) (interface{}, *apiError) {
|
||||
name := route.Param(api.context(r), "name")
|
||||
|
||||
if !clientmodel.LabelNameRE.MatchString(name) {
|
||||
return nil, &apiError{errorBadData, fmt.Errorf("invalid label name: %q", name)}
|
||||
}
|
||||
vals := api.Storage.LabelValuesForLabelName(clientmodel.LabelName(name))
|
||||
sort.Sort(vals)
|
||||
|
||||
return vals, nil
|
||||
}
|
||||
|
||||
func (api *API) series(r *http.Request) (interface{}, *apiError) {
|
||||
r.ParseForm()
|
||||
if len(r.Form["match[]"]) == 0 {
|
||||
return nil, &apiError{errorBadData, fmt.Errorf("no match[] parameter provided")}
|
||||
}
|
||||
res := map[clientmodel.Fingerprint]clientmodel.COWMetric{}
|
||||
|
||||
for _, lm := range r.Form["match[]"] {
|
||||
matchers, err := promql.ParseMetricSelector(lm)
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
for fp, met := range api.Storage.MetricsForLabelMatchers(matchers...) {
|
||||
res[fp] = met
|
||||
}
|
||||
}
|
||||
|
||||
metrics := make([]clientmodel.Metric, 0, len(res))
|
||||
for _, met := range res {
|
||||
metrics = append(metrics, met.Metric)
|
||||
}
|
||||
return metrics, nil
|
||||
}
|
||||
|
||||
func (api *API) dropSeries(r *http.Request) (interface{}, *apiError) {
|
||||
r.ParseForm()
|
||||
if len(r.Form["match[]"]) == 0 {
|
||||
return nil, &apiError{errorBadData, fmt.Errorf("no match[] parameter provided")}
|
||||
}
|
||||
fps := map[clientmodel.Fingerprint]struct{}{}
|
||||
|
||||
for _, lm := range r.Form["match[]"] {
|
||||
matchers, err := promql.ParseMetricSelector(lm)
|
||||
if err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
for fp := range api.Storage.MetricsForLabelMatchers(matchers...) {
|
||||
fps[fp] = struct{}{}
|
||||
}
|
||||
}
|
||||
for fp := range fps {
|
||||
api.Storage.DropMetricsForFingerprints(fp)
|
||||
}
|
||||
|
||||
res := struct {
|
||||
NumDeleted int `json:"numDeleted"`
|
||||
}{
|
||||
NumDeleted: len(fps),
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func respond(w http.ResponseWriter, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
|
||||
b, err := json.Marshal(&response{
|
||||
Status: statusSuccess,
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(422)
|
||||
|
||||
b, err := json.Marshal(&response{
|
||||
Status: statusError,
|
||||
ErrorType: apiErr.typ,
|
||||
Error: apiErr.err.Error(),
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.Write(b)
|
||||
}
|
||||
|
||||
func parseTime(s string) (clientmodel.Timestamp, error) {
|
||||
if t, err := strconv.ParseFloat(s, 64); err == nil {
|
||||
ts := int64(t * float64(time.Second))
|
||||
return clientmodel.TimestampFromUnixNano(ts), nil
|
||||
}
|
||||
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
|
||||
return clientmodel.TimestampFromTime(t), nil
|
||||
}
|
||||
return 0, fmt.Errorf("cannot parse %q to a valid timestamp", s)
|
||||
}
|
||||
|
||||
func parseDuration(s string) (time.Duration, error) {
|
||||
if d, err := strconv.ParseFloat(s, 64); err == nil {
|
||||
return time.Duration(d * float64(time.Second)), nil
|
||||
}
|
||||
if d, err := strutil.StringToDuration(s); err == nil {
|
||||
return d, nil
|
||||
}
|
||||
return 0, fmt.Errorf("cannot parse %q to a valid duration", s)
|
||||
}
|
499
prom/web/api/v1/api_test.go
Normal file
499
prom/web/api/v1/api_test.go
Normal file
@ -0,0 +1,499 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/metric"
|
||||
"github.com/prometheus/prometheus/util/route"
|
||||
)
|
||||
|
||||
func TestEndpoints(t *testing.T) {
|
||||
suite, err := promql.NewTest(t, `
|
||||
load 1m
|
||||
test_metric1{foo="bar"} 0+100x100
|
||||
test_metric1{foo="boo"} 1+0x100
|
||||
test_metric2{foo="boo"} 1+0x100
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer suite.Close()
|
||||
|
||||
if err := suite.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
api := &API{
|
||||
Storage: suite.Storage(),
|
||||
QueryEngine: suite.QueryEngine(),
|
||||
}
|
||||
|
||||
start := clientmodel.Timestamp(0)
|
||||
var tests = []struct {
|
||||
endpoint apiFunc
|
||||
params map[string]string
|
||||
query url.Values
|
||||
response interface{}
|
||||
errType errorType
|
||||
}{
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"2"},
|
||||
"time": []string{"123.3"},
|
||||
},
|
||||
response: &queryData{
|
||||
ResultType: promql.ExprScalar,
|
||||
Result: &promql.Scalar{
|
||||
Value: 2,
|
||||
Timestamp: start.Add(123*time.Second + 300*time.Millisecond),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"0.333"},
|
||||
"time": []string{"1970-01-01T00:02:03Z"},
|
||||
},
|
||||
response: &queryData{
|
||||
ResultType: promql.ExprScalar,
|
||||
Result: &promql.Scalar{
|
||||
Value: 0.333,
|
||||
Timestamp: start.Add(123 * time.Second),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"0.333"},
|
||||
"time": []string{"1970-01-01T01:02:03+01:00"},
|
||||
},
|
||||
response: &queryData{
|
||||
ResultType: promql.ExprScalar,
|
||||
Result: &promql.Scalar{
|
||||
Value: 0.333,
|
||||
Timestamp: start.Add(123 * time.Second),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"2"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
response: &queryData{
|
||||
ResultType: promql.ExprMatrix,
|
||||
Result: promql.Matrix{
|
||||
&promql.SampleStream{
|
||||
Values: metric.Values{
|
||||
{Value: 0, Timestamp: start},
|
||||
{Value: 1, Timestamp: start.Add(1 * time.Second)},
|
||||
{Value: 2, Timestamp: start.Add(2 * time.Second)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// Missing query params in range queries.
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"end": []string{"2"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"2"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
// Missing evaluation time.
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"0.333"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
// Bad query expression.
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"invalid][query"},
|
||||
"time": []string{"1970-01-01T01:02:03+01:00"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"invalid][query"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"100"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
"name": "__name__",
|
||||
},
|
||||
response: clientmodel.LabelValues{
|
||||
"test_metric1",
|
||||
"test_metric2",
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
"name": "foo",
|
||||
},
|
||||
response: clientmodel.LabelValues{
|
||||
"bar",
|
||||
"boo",
|
||||
},
|
||||
},
|
||||
// Bad name parameter.
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
"name": "not!!!allowed",
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.series,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric2`},
|
||||
},
|
||||
response: []clientmodel.Metric{
|
||||
{
|
||||
"__name__": "test_metric2",
|
||||
"foo": "boo",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.series,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric1{foo=~"o$"}`},
|
||||
},
|
||||
response: []clientmodel.Metric{
|
||||
{
|
||||
"__name__": "test_metric1",
|
||||
"foo": "boo",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.series,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric1{foo=~"o$"}`, `test_metric1{foo=~"o$"}`},
|
||||
},
|
||||
response: []clientmodel.Metric{
|
||||
{
|
||||
"__name__": "test_metric1",
|
||||
"foo": "boo",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.series,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric1{foo=~"o$"}`, `none`},
|
||||
},
|
||||
response: []clientmodel.Metric{
|
||||
{
|
||||
"__name__": "test_metric1",
|
||||
"foo": "boo",
|
||||
},
|
||||
},
|
||||
},
|
||||
// Missing match[] query params in series requests.
|
||||
{
|
||||
endpoint: api.series,
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.dropSeries,
|
||||
errType: errorBadData,
|
||||
},
|
||||
// The following tests delete time series from the test storage. They
|
||||
// must remain at the end and are fixed in their order.
|
||||
{
|
||||
endpoint: api.dropSeries,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric1{foo=~"o$"}`},
|
||||
},
|
||||
response: struct {
|
||||
NumDeleted int `json:"numDeleted"`
|
||||
}{1},
|
||||
},
|
||||
{
|
||||
endpoint: api.series,
|
||||
query: url.Values{
|
||||
"match[]": []string{`test_metric1`},
|
||||
},
|
||||
response: []clientmodel.Metric{
|
||||
{
|
||||
"__name__": "test_metric1",
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
}, {
|
||||
endpoint: api.dropSeries,
|
||||
query: url.Values{
|
||||
"match[]": []string{`{__name__=~".+"}`},
|
||||
},
|
||||
response: struct {
|
||||
NumDeleted int `json:"numDeleted"`
|
||||
}{2},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
// Build a context with the correct request params.
|
||||
ctx := context.Background()
|
||||
for p, v := range test.params {
|
||||
ctx = route.WithParam(ctx, p, v)
|
||||
}
|
||||
api.context = func(r *http.Request) context.Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("ANY", fmt.Sprintf("http://example.com?%s", test.query.Encode()), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, apiErr := test.endpoint(req)
|
||||
if apiErr != nil {
|
||||
if test.errType == errorNone {
|
||||
t.Fatalf("Unexpected error: %s", apiErr)
|
||||
}
|
||||
if test.errType != apiErr.typ {
|
||||
t.Fatalf("Expected error of type %q but got type %q", test.errType, apiErr.typ)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if apiErr == nil && test.errType != errorNone {
|
||||
t.Fatalf("Expected error of type %q but got none", test.errType)
|
||||
}
|
||||
if !reflect.DeepEqual(resp, test.response) {
|
||||
t.Fatalf("Response does not match, expected:\n%#v\ngot:\n%#v", test.response, resp)
|
||||
}
|
||||
// Ensure that removed metrics are unindexed before the next request.
|
||||
suite.Storage().WaitForIndexing()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRespondSuccess(t *testing.T) {
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, "test")
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on test request: %s", err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading response body: %s", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Return code %d expected in success response but got %d", 200, resp.StatusCode)
|
||||
}
|
||||
if h := resp.Header.Get("Content-Type"); h != "application/json" {
|
||||
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
|
||||
}
|
||||
|
||||
var res response
|
||||
if err = json.Unmarshal([]byte(body), &res); err != nil {
|
||||
t.Fatalf("Error unmarshaling JSON body: %s", err)
|
||||
}
|
||||
|
||||
exp := &response{
|
||||
Status: statusSuccess,
|
||||
Data: "test",
|
||||
}
|
||||
if !reflect.DeepEqual(&res, exp) {
|
||||
t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRespondError(t *testing.T) {
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, &apiError{errorTimeout, errors.New("message")}, "test")
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
resp, err := http.Get(s.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Error on test request: %s", err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading response body: %s", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != 422 {
|
||||
t.Fatalf("Return code %d expected in error response but got %d", 422, resp.StatusCode)
|
||||
}
|
||||
if h := resp.Header.Get("Content-Type"); h != "application/json" {
|
||||
t.Fatalf("Expected Content-Type %q but got %q", "application/json", h)
|
||||
}
|
||||
|
||||
var res response
|
||||
if err = json.Unmarshal([]byte(body), &res); err != nil {
|
||||
t.Fatalf("Error unmarshaling JSON body: %s", err)
|
||||
}
|
||||
|
||||
exp := &response{
|
||||
Status: statusError,
|
||||
Data: "test",
|
||||
ErrorType: errorTimeout,
|
||||
Error: "message",
|
||||
}
|
||||
if !reflect.DeepEqual(&res, exp) {
|
||||
t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTime(t *testing.T) {
|
||||
ts, err := time.Parse(time.RFC3339Nano, "2015-06-03T13:21:58.555Z")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
input string
|
||||
fail bool
|
||||
result time.Time
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "abc",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "30s",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "123",
|
||||
result: time.Unix(123, 0),
|
||||
}, {
|
||||
input: "123.123",
|
||||
result: time.Unix(123, 123000000),
|
||||
}, {
|
||||
input: "123.123",
|
||||
result: time.Unix(123, 123000000),
|
||||
}, {
|
||||
input: "2015-06-03T13:21:58.555Z",
|
||||
result: ts,
|
||||
}, {
|
||||
input: "2015-06-03T14:21:58.555+01:00",
|
||||
result: ts,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
ts, err := parseTime(test.input)
|
||||
if err != nil && !test.fail {
|
||||
t.Errorf("Unexpected error for %q: %s", test.input, err)
|
||||
continue
|
||||
}
|
||||
if err == nil && test.fail {
|
||||
t.Errorf("Expected error for %q but got none", test.input)
|
||||
continue
|
||||
}
|
||||
res := clientmodel.TimestampFromTime(test.result)
|
||||
if !test.fail && ts != res {
|
||||
t.Errorf("Expected time %v for input %q but got %v", res, test.input, ts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDuration(t *testing.T) {
|
||||
var tests = []struct {
|
||||
input string
|
||||
fail bool
|
||||
result time.Duration
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "abc",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "2015-06-03T13:21:58.555Z",
|
||||
fail: true,
|
||||
}, {
|
||||
input: "123",
|
||||
result: 123 * time.Second,
|
||||
}, {
|
||||
input: "123.333",
|
||||
result: 123*time.Second + 333*time.Millisecond,
|
||||
}, {
|
||||
input: "15s",
|
||||
result: 15 * time.Second,
|
||||
}, {
|
||||
input: "5m",
|
||||
result: 5 * time.Minute,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
d, err := parseDuration(test.input)
|
||||
if err != nil && !test.fail {
|
||||
t.Errorf("Unexpected error for %q: %s", test.input, err)
|
||||
continue
|
||||
}
|
||||
if err == nil && test.fail {
|
||||
t.Errorf("Expected error for %q but got none", test.input)
|
||||
continue
|
||||
}
|
||||
if !test.fail && d != test.result {
|
||||
t.Errorf("Expected duration %v for input %q but got %v", test.result, test.input, d)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user