2016-04-13 14:08:22 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-24 15:07:11 +00:00
|
|
|
"github.com/prometheus/common/route"
|
2015-08-22 07:42:45 +00:00
|
|
|
"golang.org/x/net/context"
|
2015-06-04 16:07:57 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/promql"
|
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2015-08-24 16:04:41 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2015-09-17 12:49:50 +00:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2015-06-04 16:07:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type status string
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusSuccess status = "success"
|
|
|
|
statusError = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
type errorType string
|
|
|
|
|
|
|
|
const (
|
2015-06-08 19:19:52 +00:00
|
|
|
errorNone errorType = ""
|
|
|
|
errorTimeout = "timeout"
|
2015-06-04 16:07:57 +00:00
|
|
|
errorCanceled = "canceled"
|
|
|
|
errorExec = "execution"
|
|
|
|
errorBadData = "bad_data"
|
|
|
|
)
|
|
|
|
|
2016-01-26 00:32:46 +00:00
|
|
|
var corsHeaders = map[string]string{
|
|
|
|
"Access-Control-Allow-Headers": "Accept, Authorization, Content-Type, Origin",
|
|
|
|
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"Access-Control-Expose-Headers": "Date",
|
|
|
|
}
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2015-11-11 19:46:57 +00:00
|
|
|
// Enables cross-site script calls.
|
|
|
|
func setCORS(w http.ResponseWriter) {
|
2016-01-26 00:32:46 +00:00
|
|
|
for h, v := range corsHeaders {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
2015-11-11 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type apiFunc func(r *http.Request) (interface{}, *apiError)
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
// 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
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
QueryCtx context.Context
|
2015-06-08 19:19:52 +00:00
|
|
|
|
|
|
|
context func(r *http.Request) context.Context
|
2015-11-11 19:46:57 +00:00
|
|
|
now func() model.Time
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:46:57 +00:00
|
|
|
// NewAPI returns an initialized API type.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
func NewAPI(qe *promql.Engine, qc context.Context, st local.Storage) *API {
|
2015-11-11 19:46:57 +00:00
|
|
|
return &API{
|
|
|
|
QueryEngine: qe,
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
QueryCtx: qc,
|
2015-11-11 19:46:57 +00:00
|
|
|
Storage: st,
|
|
|
|
context: route.Context,
|
|
|
|
now: model.Now,
|
|
|
|
}
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the API's endpoints in the given router.
|
|
|
|
func (api *API) Register(r *route.Router) {
|
|
|
|
instr := func(name string, f apiFunc) http.HandlerFunc {
|
2015-09-17 12:49:50 +00:00
|
|
|
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-06-04 16:07:57 +00:00
|
|
|
setCORS(w)
|
|
|
|
if data, err := f(r); err != nil {
|
|
|
|
respondError(w, err, data)
|
2016-01-26 00:32:46 +00:00
|
|
|
} else if data != nil {
|
2015-06-04 16:07:57 +00:00
|
|
|
respond(w, data)
|
2016-01-26 00:32:46 +00:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
})
|
2015-09-18 14:51:53 +00:00
|
|
|
return prometheus.InstrumentHandler(name, httputil.CompressionHandler{
|
|
|
|
Handler: hf,
|
|
|
|
})
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 00:32:46 +00:00
|
|
|
r.Options("/*path", instr("options", api.options))
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
r.Get("/query", instr("query", api.query))
|
|
|
|
r.Get("/query_range", instr("query_range", api.queryRange))
|
|
|
|
|
2015-06-08 19:19:52 +00:00
|
|
|
r.Get("/label/:name/values", instr("label_values", api.labelValues))
|
2015-06-09 14:09:31 +00:00
|
|
|
|
|
|
|
r.Get("/series", instr("series", api.series))
|
|
|
|
r.Del("/series", instr("drop_series", api.dropSeries))
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type queryData struct {
|
2015-08-24 16:04:41 +00:00
|
|
|
ResultType model.ValueType `json:"resultType"`
|
|
|
|
Result model.Value `json:"result"`
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 00:32:46 +00:00
|
|
|
func (api *API) options(r *http.Request) (interface{}, *apiError) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
func (api *API) query(r *http.Request) (interface{}, *apiError) {
|
2015-11-11 19:46:57 +00:00
|
|
|
var ts model.Time
|
|
|
|
if t := r.FormValue("time"); t != "" {
|
|
|
|
var err error
|
|
|
|
ts, err = parseTime(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ts = api.now()
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
2015-11-11 19:46:57 +00:00
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
qry, err := api.QueryEngine.NewInstantQuery(r.FormValue("query"), ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
|
|
|
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res := qry.Exec(api.QueryCtx)
|
2015-06-04 16:07:57 +00:00
|
|
|
if res.Err != nil {
|
2015-06-09 11:44:49 +00:00
|
|
|
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}
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:10:02 +00:00
|
|
|
if step <= 0 {
|
|
|
|
err := errors.New("zero or negative query resolution step widths are not accepted. Try a positive integer")
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
// 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 {
|
2015-06-09 11:44:49 +00:00
|
|
|
return nil, &apiError{errorBadData, err}
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res := qry.Exec(api.QueryCtx)
|
2015-06-04 16:07:57 +00:00
|
|
|
if res.Err != nil {
|
2015-06-09 11:44:49 +00:00
|
|
|
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}
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
return &queryData{
|
|
|
|
ResultType: res.Value.Type(),
|
|
|
|
Result: res.Value,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-06-08 19:19:52 +00:00
|
|
|
func (api *API) labelValues(r *http.Request) (interface{}, *apiError) {
|
|
|
|
name := route.Param(api.context(r), "name")
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
if !model.LabelNameRE.MatchString(name) {
|
2015-06-08 19:19:52 +00:00
|
|
|
return nil, &apiError{errorBadData, fmt.Errorf("invalid label name: %q", name)}
|
|
|
|
}
|
2016-07-11 18:27:25 +00:00
|
|
|
vals, err := api.Storage.LabelValuesForLabelName(model.LabelName(name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorExec, err}
|
|
|
|
}
|
2015-06-08 19:19:52 +00:00
|
|
|
sort.Sort(vals)
|
2015-06-04 16:07:57 +00:00
|
|
|
|
2015-06-08 19:19:52 +00:00
|
|
|
return vals, nil
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 14:09:31 +00:00
|
|
|
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")}
|
|
|
|
}
|
2016-05-11 21:59:52 +00:00
|
|
|
|
|
|
|
var start model.Time
|
|
|
|
if t := r.FormValue("start"); t != "" {
|
|
|
|
var err error
|
|
|
|
start, err = parseTime(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start = model.Earliest
|
|
|
|
}
|
|
|
|
|
|
|
|
var end model.Time
|
|
|
|
if t := r.FormValue("end"); t != "" {
|
|
|
|
var err error
|
|
|
|
end, err = parseTime(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
end = model.Latest
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:27:25 +00:00
|
|
|
var matcherSets []metric.LabelMatchers
|
|
|
|
for _, s := range r.Form["match[]"] {
|
|
|
|
matchers, err := promql.ParseMetricSelector(s)
|
2015-06-09 14:09:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
2016-07-11 18:27:25 +00:00
|
|
|
matcherSets = append(matcherSets, matchers)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := api.Storage.MetricsForLabelMatchers(start, end, matcherSets...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorExec, err}
|
2015-06-09 14:09:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
metrics := make([]model.Metric, 0, len(res))
|
2015-06-15 16:25:31 +00:00
|
|
|
for _, met := range res {
|
|
|
|
metrics = append(metrics, met.Metric)
|
2015-06-09 14:09:31 +00:00
|
|
|
}
|
|
|
|
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")}
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:27:25 +00:00
|
|
|
numDeleted := 0
|
|
|
|
for _, s := range r.Form["match[]"] {
|
|
|
|
matchers, err := promql.ParseMetricSelector(s)
|
2015-06-09 14:09:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorBadData, err}
|
|
|
|
}
|
2016-07-11 18:27:25 +00:00
|
|
|
n, err := api.Storage.DropMetricsForLabelMatchers(matchers...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &apiError{errorExec, err}
|
2015-06-09 14:09:31 +00:00
|
|
|
}
|
2016-07-11 18:27:25 +00:00
|
|
|
numDeleted += n
|
2015-06-09 14:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := struct {
|
|
|
|
NumDeleted int `json:"numDeleted"`
|
|
|
|
}{
|
2016-07-11 18:27:25 +00:00
|
|
|
NumDeleted: numDeleted,
|
2015-06-09 14:09:31 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-06-04 16:07:57 +00:00
|
|
|
func respond(w http.ResponseWriter, data interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2016-01-26 00:32:46 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2015-06-04 16:07:57 +00:00
|
|
|
|
|
|
|
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")
|
2015-11-11 22:00:54 +00:00
|
|
|
|
|
|
|
var code int
|
|
|
|
switch apiErr.typ {
|
|
|
|
case errorBadData:
|
|
|
|
code = http.StatusBadRequest
|
|
|
|
case errorExec:
|
|
|
|
code = 422
|
|
|
|
case errorCanceled, errorTimeout:
|
|
|
|
code = http.StatusServiceUnavailable
|
|
|
|
default:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
w.WriteHeader(code)
|
2015-06-04 16:07:57 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(&response{
|
|
|
|
Status: statusError,
|
|
|
|
ErrorType: apiErr.typ,
|
|
|
|
Error: apiErr.err.Error(),
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
func parseTime(s string) (model.Time, error) {
|
2015-06-04 16:07:57 +00:00
|
|
|
if t, err := strconv.ParseFloat(s, 64); err == nil {
|
|
|
|
ts := int64(t * float64(time.Second))
|
2015-08-20 15:18:46 +00:00
|
|
|
return model.TimeFromUnixNano(ts), nil
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
|
2015-08-20 15:18:46 +00:00
|
|
|
return model.TimeFromUnixNano(t.UnixNano()), nil
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2016-01-29 14:23:11 +00:00
|
|
|
if d, err := model.ParseDuration(s); err == nil {
|
|
|
|
return time.Duration(d), nil
|
2015-06-04 16:07:57 +00:00
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("cannot parse %q to a valid duration", s)
|
|
|
|
}
|