2016-04-13 07:08:22 -07: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 09:07:57 -07:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2015-06-04 09:07:57 -07:00
|
|
|
"fmt"
|
2016-12-24 15:37:46 -08:00
|
|
|
"math"
|
2017-12-02 21:07:05 -08:00
|
|
|
"math/rand"
|
2020-02-17 09:19:15 -08:00
|
|
|
"net"
|
2015-06-04 09:07:57 -07:00
|
|
|
"net/http"
|
2017-04-24 22:42:33 -07:00
|
|
|
"net/url"
|
2017-12-02 21:07:05 -08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-26 03:44:49 -07:00
|
|
|
"sort"
|
2015-06-04 09:07:57 -07:00
|
|
|
"strconv"
|
2019-11-11 13:42:24 -08:00
|
|
|
"strings"
|
2015-06-04 09:07:57 -07:00
|
|
|
"time"
|
2018-02-07 04:27:57 -08:00
|
|
|
"unsafe"
|
2015-06-04 09:07:57 -07:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2022-02-12 15:58:27 -08:00
|
|
|
"github.com/grafana/regexp"
|
2018-02-07 04:27:57 -08:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2019-03-25 16:01:12 -07:00
|
|
|
"github.com/pkg/errors"
|
2018-10-10 16:09:08 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2015-09-24 08:07:11 -07:00
|
|
|
"github.com/prometheus/common/route"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
"github.com/prometheus/prometheus/model/textparse"
|
|
|
|
"github.com/prometheus/prometheus/model/timestamp"
|
2015-06-04 09:07:57 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2020-02-03 10:06:39 -08:00
|
|
|
"github.com/prometheus/prometheus/promql/parser"
|
2018-03-25 09:50:34 -07:00
|
|
|
"github.com/prometheus/prometheus/rules"
|
2018-02-01 01:55:07 -08:00
|
|
|
"github.com/prometheus/prometheus/scrape"
|
2016-12-29 00:27:30 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2017-10-23 13:28:17 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/remote"
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/index"
|
2015-09-17 05:49:50 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2017-02-08 03:58:40 -08:00
|
|
|
"github.com/prometheus/prometheus/util/stats"
|
2015-06-04 09:07:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type status string
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusSuccess status = "success"
|
2017-12-02 05:52:43 -08:00
|
|
|
statusError status = "error"
|
2015-06-04 09:07:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type errorType string
|
|
|
|
|
|
|
|
const (
|
2021-01-30 03:04:48 -08:00
|
|
|
errorNone errorType = ""
|
2017-12-03 10:46:10 -08:00
|
|
|
errorTimeout errorType = "timeout"
|
|
|
|
errorCanceled errorType = "canceled"
|
|
|
|
errorExec errorType = "execution"
|
|
|
|
errorBadData errorType = "bad_data"
|
|
|
|
errorInternal errorType = "internal"
|
|
|
|
errorUnavailable errorType = "unavailable"
|
2018-05-18 00:32:11 -07:00
|
|
|
errorNotFound errorType = "not_found"
|
2015-06-04 09:07:57 -07:00
|
|
|
)
|
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
var LocalhostRepresentations = []string{"127.0.0.1", "localhost", "::1"}
|
2018-10-10 16:09:08 -07:00
|
|
|
|
2015-06-04 09:07:57 -07:00
|
|
|
type apiError struct {
|
|
|
|
typ errorType
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *apiError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.typ, e.err)
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:30:47 -07:00
|
|
|
// TargetRetriever provides the list of active/dropped targets to scrape or not.
|
|
|
|
type TargetRetriever interface {
|
2018-09-26 02:20:56 -07:00
|
|
|
TargetsActive() map[string][]*scrape.Target
|
|
|
|
TargetsDropped() map[string][]*scrape.Target
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
|
|
|
|
2020-05-18 11:02:32 -07:00
|
|
|
// AlertmanagerRetriever provides a list of all/dropped AlertManager URLs.
|
|
|
|
type AlertmanagerRetriever interface {
|
2017-04-24 22:42:33 -07:00
|
|
|
Alertmanagers() []*url.URL
|
2018-02-21 01:00:07 -08:00
|
|
|
DroppedAlertmanagers() []*url.URL
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
|
|
|
|
2020-05-18 11:02:32 -07:00
|
|
|
// RulesRetriever provides a list of active rules and alerts.
|
|
|
|
type RulesRetriever interface {
|
2018-03-25 09:50:34 -07:00
|
|
|
RuleGroups() []*rules.Group
|
2018-06-27 00:15:17 -07:00
|
|
|
AlertingRules() []*rules.AlertingRule
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
type StatsRenderer func(context.Context, *stats.Statistics, string) stats.QueryStats
|
|
|
|
|
|
|
|
func defaultStatsRenderer(ctx context.Context, s *stats.Statistics, param string) stats.QueryStats {
|
|
|
|
if param != "" {
|
|
|
|
return stats.NewQueryStats(s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 08:53:32 -07:00
|
|
|
// PrometheusVersion contains build information about Prometheus.
|
|
|
|
type PrometheusVersion struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Revision string `json:"revision"`
|
|
|
|
Branch string `json:"branch"`
|
|
|
|
BuildUser string `json:"buildUser"`
|
|
|
|
BuildDate string `json:"buildDate"`
|
|
|
|
GoVersion string `json:"goVersion"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuntimeInfo contains runtime information about Prometheus.
|
|
|
|
type RuntimeInfo struct {
|
|
|
|
StartTime time.Time `json:"startTime"`
|
|
|
|
CWD string `json:"CWD"`
|
|
|
|
ReloadConfigSuccess bool `json:"reloadConfigSuccess"`
|
|
|
|
LastConfigTime time.Time `json:"lastConfigTime"`
|
|
|
|
CorruptionCount int64 `json:"corruptionCount"`
|
|
|
|
GoroutineCount int `json:"goroutineCount"`
|
|
|
|
GOMAXPROCS int `json:"GOMAXPROCS"`
|
|
|
|
GOGC string `json:"GOGC"`
|
|
|
|
GODEBUG string `json:"GODEBUG"`
|
|
|
|
StorageRetention string `json:"storageRetention"`
|
|
|
|
}
|
|
|
|
|
2015-06-04 09:07:57 -07:00
|
|
|
type response struct {
|
|
|
|
Status status `json:"status"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
ErrorType errorType `json:"errorType,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2018-11-30 06:27:12 -08:00
|
|
|
Warnings []string `json:"warnings,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiFuncResult struct {
|
|
|
|
data interface{}
|
|
|
|
err *apiError
|
|
|
|
warnings storage.Warnings
|
|
|
|
finalizer func()
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
type apiFunc func(r *http.Request) apiFuncResult
|
2015-11-11 11:46:57 -08:00
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
// TSDBAdminStats defines the tsdb interfaces used by the v1 API for admin operations as well as statistics.
|
|
|
|
type TSDBAdminStats interface {
|
2018-11-15 05:22:16 -08:00
|
|
|
CleanTombstones() error
|
2019-11-18 11:53:33 -08:00
|
|
|
Delete(mint, maxt int64, ms ...*labels.Matcher) error
|
2018-11-15 05:22:16 -08:00
|
|
|
Snapshot(dir string, withHead bool) error
|
2020-04-29 09:16:14 -07:00
|
|
|
Stats(statsByLabelName string) (*tsdb.Stats, error)
|
2021-06-05 07:29:32 -07:00
|
|
|
WALReplayStatus() (tsdb.WALReplayStatus, error)
|
2018-11-15 05:22:16 -08:00
|
|
|
}
|
|
|
|
|
2021-12-21 02:22:40 -08:00
|
|
|
// QueryEngine defines the interface for the *promql.Engine, so it can be replaced, wrapped or mocked.
|
|
|
|
type QueryEngine interface {
|
|
|
|
SetQueryLogger(l promql.QueryLogger)
|
2022-02-01 18:07:23 -08:00
|
|
|
NewInstantQuery(q storage.Queryable, opts *promql.QueryOpts, qs string, ts time.Time) (promql.Query, error)
|
|
|
|
NewRangeQuery(q storage.Queryable, opts *promql.QueryOpts, qs string, start, end time.Time, interval time.Duration) (promql.Query, error)
|
2021-12-21 02:22:40 -08:00
|
|
|
}
|
|
|
|
|
2015-06-04 09:07:57 -07:00
|
|
|
// API can register a set of endpoints in a router and handle
|
|
|
|
// them using the provided storage and query engine.
|
|
|
|
type API struct {
|
2021-03-16 02:47:45 -07:00
|
|
|
Queryable storage.SampleAndChunkQueryable
|
2021-12-21 02:22:40 -08:00
|
|
|
QueryEngine QueryEngine
|
2021-03-16 02:47:45 -07:00
|
|
|
ExemplarQueryable storage.ExemplarQueryable
|
2015-06-08 12:19:52 -07:00
|
|
|
|
2020-04-16 01:30:47 -07:00
|
|
|
targetRetriever func(context.Context) TargetRetriever
|
2020-05-18 11:02:32 -07:00
|
|
|
alertmanagerRetriever func(context.Context) AlertmanagerRetriever
|
|
|
|
rulesRetriever func(context.Context) RulesRetriever
|
2018-03-25 09:50:34 -07:00
|
|
|
now func() time.Time
|
|
|
|
config func() config.Config
|
|
|
|
flagsMap map[string]string
|
|
|
|
ready func(http.HandlerFunc) http.HandlerFunc
|
2020-02-17 09:19:15 -08:00
|
|
|
globalURLOptions GlobalURLOptions
|
2017-12-02 21:07:05 -08:00
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
db TSDBAdminStats
|
|
|
|
dbDir string
|
|
|
|
enableAdmin bool
|
|
|
|
logger log.Logger
|
|
|
|
CORSOrigin *regexp.Regexp
|
|
|
|
buildInfo *PrometheusVersion
|
|
|
|
runtimeInfo func() (RuntimeInfo, error)
|
|
|
|
gatherer prometheus.Gatherer
|
|
|
|
isAgent bool
|
|
|
|
statsRenderer StatsRenderer
|
2021-02-26 08:43:19 -08:00
|
|
|
|
|
|
|
remoteWriteHandler http.Handler
|
|
|
|
remoteReadHandler http.Handler
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:09:08 -07:00
|
|
|
func init() {
|
|
|
|
jsoniter.RegisterTypeEncoderFunc("promql.Point", marshalPointJSON, marshalPointJSONIsEmpty)
|
2021-03-16 02:47:45 -07:00
|
|
|
jsoniter.RegisterTypeEncoderFunc("exemplar.Exemplar", marshalExemplarJSON, marshalExemplarJSONEmpty)
|
2018-10-10 16:09:08 -07:00
|
|
|
}
|
|
|
|
|
2015-11-11 11:46:57 -08:00
|
|
|
// NewAPI returns an initialized API type.
|
2017-10-06 08:20:20 -07:00
|
|
|
func NewAPI(
|
2021-12-21 02:22:40 -08:00
|
|
|
qe QueryEngine,
|
2021-02-15 03:30:00 -08:00
|
|
|
q storage.SampleAndChunkQueryable,
|
|
|
|
ap storage.Appendable,
|
2021-03-16 02:47:45 -07:00
|
|
|
eq storage.ExemplarQueryable,
|
2020-04-16 01:30:47 -07:00
|
|
|
tr func(context.Context) TargetRetriever,
|
2020-05-18 11:02:32 -07:00
|
|
|
ar func(context.Context) AlertmanagerRetriever,
|
2017-10-06 08:20:20 -07:00
|
|
|
configFunc func() config.Config,
|
api: Added v1/status/flags endpoint. (#3864)
Endpoint URL: /api/v1/status/flags
Example Output:
```json
{
"status": "success",
"data": {
"alertmanager.notification-queue-capacity": "10000",
"alertmanager.timeout": "10s",
"completion-bash": "false",
"completion-script-bash": "false",
"completion-script-zsh": "false",
"config.file": "my_cool_prometheus.yaml",
"help": "false",
"help-long": "false",
"help-man": "false",
"log.level": "info",
"query.lookback-delta": "5m",
"query.max-concurrency": "20",
"query.timeout": "2m",
"storage.tsdb.max-block-duration": "36h",
"storage.tsdb.min-block-duration": "2h",
"storage.tsdb.no-lockfile": "false",
"storage.tsdb.path": "data/",
"storage.tsdb.retention": "15d",
"version": "false",
"web.console.libraries": "console_libraries",
"web.console.templates": "consoles",
"web.enable-admin-api": "false",
"web.enable-lifecycle": "false",
"web.external-url": "",
"web.listen-address": "0.0.0.0:9090",
"web.max-connections": "512",
"web.read-timeout": "5m",
"web.route-prefix": "/",
"web.user-assets": ""
}
}
```
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
2018-02-21 00:49:02 -08:00
|
|
|
flagsMap map[string]string,
|
2020-02-17 09:19:15 -08:00
|
|
|
globalURLOptions GlobalURLOptions,
|
2017-10-06 08:20:20 -07:00
|
|
|
readyFunc func(http.HandlerFunc) http.HandlerFunc,
|
2020-04-29 09:16:14 -07:00
|
|
|
db TSDBAdminStats,
|
|
|
|
dbDir string,
|
2017-12-02 21:07:05 -08:00
|
|
|
enableAdmin bool,
|
2018-07-06 10:44:45 -07:00
|
|
|
logger log.Logger,
|
2020-05-18 11:02:32 -07:00
|
|
|
rr func(context.Context) RulesRetriever,
|
2018-09-25 12:07:34 -07:00
|
|
|
remoteReadSampleLimit int,
|
|
|
|
remoteReadConcurrencyLimit int,
|
2019-08-19 13:16:10 -07:00
|
|
|
remoteReadMaxBytesInFrame int,
|
2021-10-29 08:25:05 -07:00
|
|
|
isAgent bool,
|
2019-01-17 07:01:06 -08:00
|
|
|
CORSOrigin *regexp.Regexp,
|
2019-11-02 08:53:32 -07:00
|
|
|
runtimeInfo func() (RuntimeInfo, error),
|
|
|
|
buildInfo *PrometheusVersion,
|
2020-09-29 13:05:33 -07:00
|
|
|
gatherer prometheus.Gatherer,
|
2021-02-26 08:43:19 -08:00
|
|
|
registerer prometheus.Registerer,
|
2022-02-10 06:17:05 -08:00
|
|
|
statsRenderer StatsRenderer,
|
2017-10-06 08:20:20 -07:00
|
|
|
) *API {
|
2021-01-30 03:04:48 -08:00
|
|
|
a := &API{
|
2021-03-16 02:47:45 -07:00
|
|
|
QueryEngine: qe,
|
|
|
|
Queryable: q,
|
|
|
|
ExemplarQueryable: eq,
|
2021-01-30 03:04:48 -08:00
|
|
|
|
2017-01-13 01:20:11 -08:00
|
|
|
targetRetriever: tr,
|
|
|
|
alertmanagerRetriever: ar,
|
2018-09-05 06:50:50 -07:00
|
|
|
|
2021-02-26 08:43:19 -08:00
|
|
|
now: time.Now,
|
|
|
|
config: configFunc,
|
|
|
|
flagsMap: flagsMap,
|
|
|
|
ready: readyFunc,
|
|
|
|
globalURLOptions: globalURLOptions,
|
|
|
|
db: db,
|
|
|
|
dbDir: dbDir,
|
|
|
|
enableAdmin: enableAdmin,
|
|
|
|
rulesRetriever: rr,
|
|
|
|
logger: logger,
|
|
|
|
CORSOrigin: CORSOrigin,
|
|
|
|
runtimeInfo: runtimeInfo,
|
|
|
|
buildInfo: buildInfo,
|
|
|
|
gatherer: gatherer,
|
2021-10-29 08:25:05 -07:00
|
|
|
isAgent: isAgent,
|
2022-02-10 06:17:05 -08:00
|
|
|
statsRenderer: defaultStatsRenderer,
|
2021-02-26 08:43:19 -08:00
|
|
|
|
|
|
|
remoteReadHandler: remote.NewReadHandler(logger, registerer, q, configFunc, remoteReadSampleLimit, remoteReadConcurrencyLimit, remoteReadMaxBytesInFrame),
|
2015-11-11 11:46:57 -08:00
|
|
|
}
|
2021-01-30 03:04:48 -08:00
|
|
|
|
2022-02-10 06:17:05 -08:00
|
|
|
if statsRenderer != nil {
|
|
|
|
a.statsRenderer = statsRenderer
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:30:00 -08:00
|
|
|
if ap != nil {
|
|
|
|
a.remoteWriteHandler = remote.NewWriteHandler(logger, ap)
|
2021-01-30 03:04:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
func setUnavailStatusOnTSDBNotReady(r apiFuncResult) apiFuncResult {
|
|
|
|
if r.err != nil && errors.Cause(r.err.err) == tsdb.ErrNotReady {
|
|
|
|
r.err.typ = errorUnavailable
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-06-04 09:07:57 -07:00
|
|
|
// Register the API's endpoints in the given router.
|
|
|
|
func (api *API) Register(r *route.Router) {
|
2018-03-21 01:16:16 -07:00
|
|
|
wrap := func(f apiFunc) http.HandlerFunc {
|
2015-09-17 05:49:50 -07:00
|
|
|
hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-01-17 07:01:06 -08:00
|
|
|
httputil.SetCORS(w, api.CORSOrigin, r)
|
2020-04-29 09:16:14 -07:00
|
|
|
result := setUnavailStatusOnTSDBNotReady(f(r))
|
2020-04-16 11:16:16 -07:00
|
|
|
if result.finalizer != nil {
|
|
|
|
defer result.finalizer()
|
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
if result.err != nil {
|
|
|
|
api.respondError(w, result.err, result.data)
|
2020-04-29 09:16:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.data != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
api.respond(w, result.data, result.warnings)
|
2020-04-29 09:16:14 -07:00
|
|
|
return
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2015-06-04 09:07:57 -07:00
|
|
|
})
|
2018-03-21 01:16:16 -07:00
|
|
|
return api.ready(httputil.CompressionHandler{
|
2015-09-18 07:51:53 -07:00
|
|
|
Handler: hf,
|
2018-03-21 01:16:16 -07:00
|
|
|
}.ServeHTTP)
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
wrapAgent := func(f apiFunc) http.HandlerFunc {
|
|
|
|
return wrap(func(r *http.Request) apiFuncResult {
|
|
|
|
if api.isAgent {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, errors.New("unavailable with Prometheus Agent")}, nil, nil}
|
|
|
|
}
|
|
|
|
return f(r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 01:16:16 -07:00
|
|
|
r.Options("/*path", wrap(api.options))
|
2016-01-25 16:32:46 -08:00
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/query", wrapAgent(api.query))
|
|
|
|
r.Post("/query", wrapAgent(api.query))
|
|
|
|
r.Get("/query_range", wrapAgent(api.queryRange))
|
|
|
|
r.Post("/query_range", wrapAgent(api.queryRange))
|
|
|
|
r.Get("/query_exemplars", wrapAgent(api.queryExemplars))
|
|
|
|
r.Post("/query_exemplars", wrapAgent(api.queryExemplars))
|
2015-06-04 09:07:57 -07:00
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/labels", wrapAgent(api.labelNames))
|
|
|
|
r.Post("/labels", wrapAgent(api.labelNames))
|
|
|
|
r.Get("/label/:name/values", wrapAgent(api.labelValues))
|
2015-06-09 07:09:31 -07:00
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/series", wrapAgent(api.series))
|
|
|
|
r.Post("/series", wrapAgent(api.series))
|
|
|
|
r.Del("/series", wrapAgent(api.dropSeries))
|
2016-12-02 04:31:43 -08:00
|
|
|
|
2018-03-21 01:16:16 -07:00
|
|
|
r.Get("/targets", wrap(api.targets))
|
2018-05-18 00:32:11 -07:00
|
|
|
r.Get("/targets/metadata", wrap(api.targetMetadata))
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/alertmanagers", wrapAgent(api.alertmanagers))
|
2017-05-11 08:09:24 -07:00
|
|
|
|
2019-12-10 06:56:16 -08:00
|
|
|
r.Get("/metadata", wrap(api.metricMetadata))
|
|
|
|
|
2018-03-21 01:16:16 -07:00
|
|
|
r.Get("/status/config", wrap(api.serveConfig))
|
2019-11-02 08:53:32 -07:00
|
|
|
r.Get("/status/runtimeinfo", wrap(api.serveRuntimeInfo))
|
|
|
|
r.Get("/status/buildinfo", wrap(api.serveBuildInfo))
|
2018-03-21 01:16:16 -07:00
|
|
|
r.Get("/status/flags", wrap(api.serveFlags))
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/status/tsdb", wrapAgent(api.serveTSDBStatus))
|
2021-06-05 07:29:32 -07:00
|
|
|
r.Get("/status/walreplay", api.serveWALReplayStatus)
|
2021-10-15 15:41:53 -07:00
|
|
|
r.Post("/read", api.ready(api.remoteRead))
|
|
|
|
r.Post("/write", api.ready(api.remoteWrite))
|
2017-12-02 21:07:05 -08:00
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Get("/alerts", wrapAgent(api.alerts))
|
|
|
|
r.Get("/rules", wrapAgent(api.rules))
|
2018-03-25 09:50:34 -07:00
|
|
|
|
2017-12-02 21:07:05 -08:00
|
|
|
// Admin APIs
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Post("/admin/tsdb/delete_series", wrapAgent(api.deleteSeries))
|
|
|
|
r.Post("/admin/tsdb/clean_tombstones", wrapAgent(api.cleanTombstones))
|
|
|
|
r.Post("/admin/tsdb/snapshot", wrapAgent(api.snapshot))
|
2019-03-20 10:33:45 -07:00
|
|
|
|
2021-10-29 08:25:05 -07:00
|
|
|
r.Put("/admin/tsdb/delete_series", wrapAgent(api.deleteSeries))
|
|
|
|
r.Put("/admin/tsdb/clean_tombstones", wrapAgent(api.cleanTombstones))
|
|
|
|
r.Put("/admin/tsdb/snapshot", wrapAgent(api.snapshot))
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type queryData struct {
|
2022-02-10 06:17:05 -08:00
|
|
|
ResultType parser.ValueType `json:"resultType"`
|
|
|
|
Result parser.Value `json:"result"`
|
|
|
|
Stats stats.QueryStats `json:"stats,omitempty"`
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func invalidParamError(err error, parameter string) apiFuncResult {
|
|
|
|
return apiFuncResult{nil, &apiError{
|
|
|
|
errorBadData, errors.Wrapf(err, "invalid parameter %q", parameter),
|
|
|
|
}, nil, nil}
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) options(r *http.Request) apiFuncResult {
|
|
|
|
return apiFuncResult{nil, nil, nil, nil}
|
2016-01-25 16:32:46 -08:00
|
|
|
}
|
|
|
|
|
2020-04-17 11:03:26 -07:00
|
|
|
func (api *API) query(r *http.Request) (result apiFuncResult) {
|
2021-08-31 04:31:19 -07:00
|
|
|
ts, err := parseTimeParam(r, "time", api.now())
|
|
|
|
if err != nil {
|
|
|
|
return invalidParamError(err, "time")
|
|
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
if to := r.FormValue("timeout"); to != "" {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
timeout, err := parseDuration(to)
|
|
|
|
if err != nil {
|
|
|
|
return invalidParamError(err, "timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
opts := extractQueryOpts(r)
|
|
|
|
qry, err := api.QueryEngine.NewInstantQuery(api.Queryable, opts, r.FormValue("query"), ts)
|
2021-08-31 04:31:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return invalidParamError(err, "query")
|
|
|
|
}
|
|
|
|
|
|
|
|
// From now on, we must only return with a finalizer in the result (to
|
|
|
|
// be called by the caller) or call qry.Close ourselves (which is
|
|
|
|
// required in the case of a panic).
|
|
|
|
defer func() {
|
|
|
|
if result.finalizer == nil {
|
|
|
|
qry.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx = httputil.ContextFromRequest(ctx, r)
|
|
|
|
|
|
|
|
res := qry.Exec(ctx)
|
|
|
|
if res.Err != nil {
|
|
|
|
return apiFuncResult{nil, returnAPIError(res.Err), res.Warnings, qry.Close}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional stats field in response if parameter "stats" is not empty.
|
2022-02-10 06:17:05 -08:00
|
|
|
sr := api.statsRenderer
|
|
|
|
if sr == nil {
|
|
|
|
sr = defaultStatsRenderer
|
2021-08-31 04:31:19 -07:00
|
|
|
}
|
2022-02-10 06:17:05 -08:00
|
|
|
qs := sr(ctx, qry.Stats(), r.FormValue("stats"))
|
2017-11-16 07:30:48 -08:00
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{&queryData{
|
2021-08-31 04:31:19 -07:00
|
|
|
ResultType: res.Value.Type(),
|
|
|
|
Result: res.Value,
|
|
|
|
Stats: qs,
|
|
|
|
}, nil, res.Warnings, qry.Close}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
func extractQueryOpts(r *http.Request) *promql.QueryOpts {
|
|
|
|
return &promql.QueryOpts{
|
|
|
|
EnablePerStepStats: r.FormValue("stats") == "all",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:03:26 -07:00
|
|
|
func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
|
2015-06-04 09:07:57 -07:00
|
|
|
start, err := parseTime(r.FormValue("start"))
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "start")
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
end, err := parseTime(r.FormValue("end"))
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "end")
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2016-11-01 06:25:34 -07:00
|
|
|
if end.Before(start) {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(errors.New("end timestamp must not be before start time"), "end")
|
2016-11-01 06:25:34 -07:00
|
|
|
}
|
|
|
|
|
2015-06-04 09:07:57 -07:00
|
|
|
step, err := parseDuration(r.FormValue("step"))
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "step")
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2016-08-16 06:10:02 -07:00
|
|
|
if step <= 0 {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(errors.New("zero or negative query resolution step widths are not accepted. Try a positive integer"), "step")
|
2016-08-16 06:10:02 -07:00
|
|
|
}
|
|
|
|
|
2015-06-04 09:07:57 -07: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)")
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2017-05-02 16:49:29 -07:00
|
|
|
ctx := r.Context()
|
2017-03-06 09:32:21 -08:00
|
|
|
if to := r.FormValue("timeout"); to != "" {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
timeout, err := parseDuration(to)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "timeout")
|
2017-03-06 09:32:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:07:23 -08:00
|
|
|
opts := extractQueryOpts(r)
|
|
|
|
qry, err := api.QueryEngine.NewRangeQuery(api.Queryable, opts, r.FormValue("query"), start, end, step)
|
2015-06-04 09:07:57 -07:00
|
|
|
if err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2021-08-31 04:31:19 -07:00
|
|
|
// From now on, we must only return with a finalizer in the result (to
|
|
|
|
// be called by the caller) or call qry.Close ourselves (which is
|
|
|
|
// required in the case of a panic).
|
|
|
|
defer func() {
|
|
|
|
if result.finalizer == nil {
|
|
|
|
qry.Close()
|
2021-07-05 03:47:34 -07:00
|
|
|
}
|
2021-08-31 04:31:19 -07:00
|
|
|
}()
|
2021-07-03 06:53:56 -07:00
|
|
|
|
2021-08-31 04:31:19 -07:00
|
|
|
ctx = httputil.ContextFromRequest(ctx, r)
|
2021-07-03 06:53:56 -07:00
|
|
|
|
2021-08-31 04:31:19 -07:00
|
|
|
res := qry.Exec(ctx)
|
|
|
|
if res.Err != nil {
|
|
|
|
return apiFuncResult{nil, returnAPIError(res.Err), res.Warnings, qry.Close}
|
2017-02-08 03:58:40 -08:00
|
|
|
}
|
|
|
|
|
2021-08-31 04:31:19 -07:00
|
|
|
// Optional stats field in response if parameter "stats" is not empty.
|
2022-02-10 06:17:05 -08:00
|
|
|
sr := api.statsRenderer
|
|
|
|
if sr == nil {
|
|
|
|
sr = defaultStatsRenderer
|
2021-07-05 03:47:34 -07:00
|
|
|
}
|
2022-02-10 06:17:05 -08:00
|
|
|
qs := sr(ctx, qry.Stats(), r.FormValue("stats"))
|
2021-07-05 03:47:34 -07:00
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{&queryData{
|
2021-08-31 04:31:19 -07:00
|
|
|
ResultType: res.Value.Type(),
|
|
|
|
Result: res.Value,
|
|
|
|
Stats: qs,
|
|
|
|
}, nil, res.Warnings, qry.Close}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2021-08-31 04:31:19 -07:00
|
|
|
// TODO: remove this when we have sparse histogram support in PromQL.
|
|
|
|
// This is a hack to query sparse histogram for buckets.
|
|
|
|
//func (api *API) queryRange(r *http.Request) (result apiFuncResult) {
|
|
|
|
// start, err := parseTime(r.FormValue("start"))
|
|
|
|
// if err != nil {
|
|
|
|
// return invalidParamError(err, "start")
|
|
|
|
// }
|
|
|
|
// end, err := parseTime(r.FormValue("end"))
|
|
|
|
// if err != nil {
|
|
|
|
// return invalidParamError(err, "end")
|
|
|
|
// }
|
|
|
|
// if end.Before(start) {
|
|
|
|
// return invalidParamError(errors.New("end timestamp must not be before start time"), "end")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// step, err := parseDuration(r.FormValue("step"))
|
|
|
|
// if err != nil {
|
|
|
|
// return invalidParamError(err, "step")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if step <= 0 {
|
|
|
|
// return invalidParamError(errors.New("zero or negative query resolution step widths are not accepted. Try a positive integer"), "step")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // 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 apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ctx := r.Context()
|
|
|
|
// if to := r.FormValue("timeout"); to != "" {
|
|
|
|
// var cancel context.CancelFunc
|
|
|
|
// timeout, err := parseDuration(to)
|
|
|
|
// if err != nil {
|
|
|
|
// return invalidParamError(err, "timeout")
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
// defer cancel()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// expr, err := parser.ParseExpr(r.FormValue("query"))
|
|
|
|
// if err != nil {
|
|
|
|
// return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// selectors := parser.ExtractSelectors(expr)
|
|
|
|
// if len(selectors) < 1 {
|
|
|
|
// return apiFuncResult{nil, nil, nil, nil}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if len(selectors) > 1 {
|
|
|
|
// return apiFuncResult{nil, &apiError{errorBadData, errors.New("need exactly 1 selector")}, nil, nil}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// hasRate, rateDuration := false, time.Duration(0)
|
|
|
|
// parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
|
|
|
|
// switch n := node.(type) {
|
|
|
|
// case *parser.Call:
|
|
|
|
// if n.Func.Name == "rate" {
|
|
|
|
// hasRate = true
|
|
|
|
// rateDuration = n.Args[0].(*parser.MatrixSelector).Range
|
|
|
|
// return errors.New("stop it here")
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return nil
|
|
|
|
// })
|
|
|
|
// var numRateSamples int
|
|
|
|
// if hasRate {
|
|
|
|
// numRateSamples = int(end.Sub(start)/step + 1)
|
|
|
|
// if start.Add(time.Duration(numRateSamples-1) * step).After(end) {
|
|
|
|
// numRateSamples--
|
|
|
|
// }
|
|
|
|
// start = start.Add(-rateDuration) // Adjusting for the first point lookback.
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// q, err := api.Queryable.Querier(ctx, timestamp.FromTime(start), timestamp.FromTime(end))
|
|
|
|
// if err != nil {
|
|
|
|
// return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// res := promql.Matrix{}
|
|
|
|
// ss := q.Select(true, nil, selectors[0]...)
|
|
|
|
//
|
|
|
|
// for ss.Next() {
|
|
|
|
// resSeries := make(map[float64]promql.Series) // le -> series.
|
|
|
|
//
|
|
|
|
// s := ss.At()
|
|
|
|
// it := s.Iterator()
|
|
|
|
// for it.Next() { // Per histogram.
|
|
|
|
// t, h := it.AtHistogram()
|
|
|
|
// buckets := histogram.CumulativeExpandSparseHistogram(h)
|
|
|
|
// for buckets.Next() {
|
|
|
|
// // Every bucket is a different series with different "le".
|
|
|
|
// b := buckets.At()
|
|
|
|
// rs, ok := resSeries[b.Le]
|
|
|
|
// if !ok {
|
|
|
|
// rs = promql.Series{
|
|
|
|
// Metric: append(
|
|
|
|
// s.Labels(),
|
|
|
|
// labels.Label{Name: "le", Value: fmt.Sprintf("%.16f", b.Le)}, // TODO: Set some precision for 'le'?
|
|
|
|
// ),
|
|
|
|
// }
|
|
|
|
// sort.Sort(rs.Metric)
|
|
|
|
// resSeries[b.Le] = rs
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// rs.Points = append(rs.Points, promql.Point{
|
|
|
|
// T: t,
|
|
|
|
// V: float64(b.Count),
|
|
|
|
// })
|
|
|
|
// resSeries[b.Le] = rs
|
|
|
|
// }
|
|
|
|
// if buckets.Err() != nil {
|
|
|
|
// return apiFuncResult{nil, &apiError{errorExec, buckets.Err()}, nil, nil}
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for _, rs := range resSeries {
|
|
|
|
// res = append(res, rs)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if hasRate {
|
|
|
|
// newRes := make(promql.Matrix, len(res))
|
|
|
|
// for i := range newRes {
|
|
|
|
// newRes[i].Metric = res[i].Metric
|
|
|
|
// points := make([]promql.Point, numRateSamples)
|
|
|
|
//
|
|
|
|
// rawPoints := res[i].Points
|
|
|
|
//
|
|
|
|
// startIdx, endIdx := 0, 0
|
|
|
|
// for idx := range points {
|
|
|
|
// pointTime := start.Add(time.Duration(idx) * step)
|
|
|
|
// lookbackTime := pointTime.Add(-rateDuration)
|
|
|
|
// points[idx].T = timestamp.FromTime(pointTime)
|
|
|
|
// if len(rawPoints) == 0 {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for startIdx < len(rawPoints) && timestamp.Time(rawPoints[startIdx].T).Before(lookbackTime) {
|
|
|
|
// startIdx++
|
|
|
|
// }
|
|
|
|
// if startIdx >= len(rawPoints) {
|
|
|
|
// startIdx = len(rawPoints) - 1
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for endIdx < len(rawPoints) && timestamp.Time(rawPoints[endIdx].T).Before(pointTime) {
|
|
|
|
// endIdx++
|
|
|
|
// }
|
|
|
|
// if endIdx >= len(rawPoints) {
|
|
|
|
// endIdx = len(rawPoints) - 1
|
|
|
|
// } else if timestamp.Time(rawPoints[endIdx].T).After(pointTime) && (len(rawPoints) == 1 || endIdx == 0) {
|
|
|
|
// continue
|
|
|
|
// } else {
|
|
|
|
// endIdx--
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// valDiff := rawPoints[endIdx].V - rawPoints[startIdx].V
|
|
|
|
// timeDiffSeconds := float64(timestamp.Time(rawPoints[endIdx].T).Sub(timestamp.Time(rawPoints[startIdx].T))) / float64(time.Second)
|
|
|
|
//
|
|
|
|
// if timeDiffSeconds != 0 {
|
|
|
|
// points[idx].V = valDiff / timeDiffSeconds
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// newRes[i].Points = points
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// res = newRes
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// sort.Sort(res)
|
|
|
|
//
|
|
|
|
// return apiFuncResult{&queryData{
|
|
|
|
// ResultType: res.Type(),
|
|
|
|
// Result: res,
|
|
|
|
// }, nil, nil, nil}
|
|
|
|
//}
|
|
|
|
|
2021-03-16 02:47:45 -07:00
|
|
|
func (api *API) queryExemplars(r *http.Request) apiFuncResult {
|
|
|
|
start, err := parseTimeParam(r, "start", minTime)
|
|
|
|
if err != nil {
|
|
|
|
return invalidParamError(err, "start")
|
|
|
|
}
|
|
|
|
end, err := parseTimeParam(r, "end", maxTime)
|
|
|
|
if err != nil {
|
|
|
|
return invalidParamError(err, "end")
|
|
|
|
}
|
|
|
|
if end.Before(start) {
|
|
|
|
err := errors.New("end timestamp must not be before start timestamp")
|
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
expr, err := parser.ParseExpr(r.FormValue("query"))
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectors := parser.ExtractSelectors(expr)
|
|
|
|
if len(selectors) < 1 {
|
|
|
|
return apiFuncResult{nil, nil, nil, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
eq, err := api.ExemplarQueryable.ExemplarQuerier(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := eq.Select(timestamp.FromTime(start), timestamp.FromTime(end), selectors...)
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
|
|
|
}
|
|
|
|
|
2018-11-14 01:55:54 -08:00
|
|
|
func returnAPIError(err error) *apiError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 07:29:35 -07:00
|
|
|
switch errors.Cause(err).(type) {
|
2018-11-14 01:55:54 -08:00
|
|
|
case promql.ErrQueryCanceled:
|
|
|
|
return &apiError{errorCanceled, err}
|
|
|
|
case promql.ErrQueryTimeout:
|
|
|
|
return &apiError{errorTimeout, err}
|
|
|
|
case promql.ErrStorage:
|
|
|
|
return &apiError{errorInternal, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &apiError{errorExec, err}
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) labelNames(r *http.Request) apiFuncResult {
|
2020-05-30 05:50:09 -07:00
|
|
|
start, err := parseTimeParam(r, "start", minTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "start")
|
2020-05-30 05:50:09 -07:00
|
|
|
}
|
|
|
|
end, err := parseTimeParam(r, "end", maxTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "end")
|
2020-05-30 05:50:09 -07:00
|
|
|
}
|
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:50:09 -07:00
|
|
|
q, err := api.Queryable.Querier(r.Context(), timestamp.FromTime(start), timestamp.FromTime(end))
|
2018-11-19 02:21:14 -08:00
|
|
|
if err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
2018-11-19 02:21:14 -08:00
|
|
|
}
|
|
|
|
defer q.Close()
|
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
var (
|
|
|
|
names []string
|
|
|
|
warnings storage.Warnings
|
|
|
|
)
|
|
|
|
if len(matcherSets) > 0 {
|
|
|
|
labelNamesSet := make(map[string]struct{})
|
2021-07-20 05:38:08 -07:00
|
|
|
|
|
|
|
for _, matchers := range matcherSets {
|
|
|
|
vals, callWarnings, err := q.LabelNames(matchers...)
|
|
|
|
if err != nil {
|
2020-12-22 03:02:19 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, nil}
|
|
|
|
}
|
2021-07-20 05:38:08 -07:00
|
|
|
|
|
|
|
warnings = append(warnings, callWarnings...)
|
|
|
|
for _, val := range vals {
|
|
|
|
labelNamesSet[val] = struct{}{}
|
|
|
|
}
|
2020-12-22 03:02:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the map to an array.
|
|
|
|
names = make([]string, 0, len(labelNamesSet))
|
|
|
|
for key := range labelNamesSet {
|
|
|
|
names = append(names, key)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
} else {
|
|
|
|
names, warnings, err = q.LabelNames()
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, nil}
|
|
|
|
}
|
2018-11-19 02:21:14 -08:00
|
|
|
}
|
2020-12-22 03:02:19 -08:00
|
|
|
|
2020-07-31 08:03:02 -07:00
|
|
|
if names == nil {
|
|
|
|
names = []string{}
|
|
|
|
}
|
2019-06-17 00:31:17 -07:00
|
|
|
return apiFuncResult{names, nil, warnings, nil}
|
2018-11-19 02:21:14 -08:00
|
|
|
}
|
|
|
|
|
2020-04-17 11:03:26 -07:00
|
|
|
func (api *API) labelValues(r *http.Request) (result apiFuncResult) {
|
2017-10-04 12:04:15 -07:00
|
|
|
ctx := r.Context()
|
|
|
|
name := route.Param(ctx, "name")
|
2015-06-08 12:19:52 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
if !model.LabelNameRE.MatchString(name) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.Errorf("invalid label name: %q", name)}, nil, nil}
|
2015-06-08 12:19:52 -07:00
|
|
|
}
|
2020-05-30 05:50:09 -07:00
|
|
|
|
|
|
|
start, err := parseTimeParam(r, "start", minTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "start")
|
2020-05-30 05:50:09 -07:00
|
|
|
}
|
|
|
|
end, err := parseTimeParam(r, "end", maxTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "end")
|
2020-05-30 05:50:09 -07:00
|
|
|
}
|
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:50:09 -07:00
|
|
|
q, err := api.Queryable.Querier(r.Context(), timestamp.FromTime(start), timestamp.FromTime(end))
|
2016-10-12 10:34:22 -07:00
|
|
|
if err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
2016-10-12 10:34:22 -07:00
|
|
|
}
|
2020-04-17 11:03:26 -07:00
|
|
|
// From now on, we must only return with a finalizer in the result (to
|
|
|
|
// be called by the caller) or call q.Close ourselves (which is required
|
|
|
|
// in the case of a panic).
|
|
|
|
defer func() {
|
|
|
|
if result.finalizer == nil {
|
|
|
|
q.Close()
|
|
|
|
}
|
|
|
|
}()
|
Load only some offsets into the symbol table into memory.
Rather than keeping the entire symbol table in memory, keep every nth
offset and walk from there to the entry we need. This ends up slightly
slower, ~360ms per 1M series returned from PostingsForMatchers which is
not much considering the rest of the CPU such a query would go on to
use.
Make LabelValues use the postings tables, rather than having
to do symbol lookups. Use yoloString, as PostingsForMatchers
doesn't need the strings to stick around and adjust the API
call to keep the Querier open until it's all marshalled.
Remove allocatedSymbols memory optimisation, we no longer keep all the
symbol strings in heap memory. Remove LabelValuesFor and LabelIndices,
they're dead code. Ensure we've still tests for label indices,
and add missing test that we can work with old V1 Format index files.
PostingForMatchers performance is slightly better, with a big drop in
allocation counts due to using yoloString for LabelValues:
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 36698 36681 -0.05%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 522786 560887 +7.29%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 511652 537680 +5.09%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 522102 564239 +8.07%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113689911 111795919 -1.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 135825572 132871085 -2.18%
BenchmarkPostingsForMatchers/Block/i=~""-4 40782628 38038181 -6.73%
BenchmarkPostingsForMatchers/Block/i!=""-4 31267869 29194327 -6.63%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112733329 111568823 -1.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112868153 111232029 -1.45%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 31338257 29349446 -6.35%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 32054482 29972436 -6.50%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 136504654 133968442 -1.86%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 27960350 27264997 -2.49%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 136765564 133860724 -2.12%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 163714583 159453668 -2.60%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 6 6 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 11 11 +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 11 11 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 17 15 -11.76%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100012 12 -99.99%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200040 100040 -49.99%
BenchmarkPostingsForMatchers/Block/i=~""-4 200045 100045 -49.99%
BenchmarkPostingsForMatchers/Block/i!=""-4 200041 100041 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100017 17 -99.98%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100023 23 -99.98%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200046 100046 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200050 100050 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200049 100049 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111150 11150 -89.97%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200055 100055 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311238 111238 -64.26%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 296 296 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 424 424 +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 424 424 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 552 1544 +179.71%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600482 1606125 +0.35%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 17259065 17264709 +0.03%
BenchmarkPostingsForMatchers/Block/i=~""-4 17259150 17264780 +0.03%
BenchmarkPostingsForMatchers/Block/i!=""-4 17259048 17264680 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600610 1606242 +0.35%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600813 1606434 +0.35%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 17259176 17264808 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 17259304 17264936 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 17259333 17264965 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3142628 3148262 +0.18%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 17259509 17265141 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 20405680 20416944 +0.06%
However overall Select performance is down and involves more allocs, due to
having to do more than a simple map lookup to resolve a symbol and that all the strings
returned are allocated:
benchmark old ns/op new ns/op delta
BenchmarkQuerierSelect/Block/1of1000000-4 506092636 862678244 +70.46%
BenchmarkQuerierSelect/Block/10of1000000-4 505638968 860917636 +70.26%
BenchmarkQuerierSelect/Block/100of1000000-4 505229450 882150048 +74.60%
BenchmarkQuerierSelect/Block/1000of1000000-4 515905414 862241115 +67.13%
BenchmarkQuerierSelect/Block/10000of1000000-4 516785354 874841110 +69.29%
BenchmarkQuerierSelect/Block/100000of1000000-4 540742808 907030187 +67.74%
BenchmarkQuerierSelect/Block/1000000of1000000-4 815224288 1181236903 +44.90%
benchmark old allocs new allocs delta
BenchmarkQuerierSelect/Block/1of1000000-4 4000020 6000020 +50.00%
BenchmarkQuerierSelect/Block/10of1000000-4 4000038 6000038 +50.00%
BenchmarkQuerierSelect/Block/100of1000000-4 4000218 6000218 +50.00%
BenchmarkQuerierSelect/Block/1000of1000000-4 4002018 6002018 +49.97%
BenchmarkQuerierSelect/Block/10000of1000000-4 4020018 6020018 +49.75%
BenchmarkQuerierSelect/Block/100000of1000000-4 4200018 6200018 +47.62%
BenchmarkQuerierSelect/Block/1000000of1000000-4 6000018 8000019 +33.33%
benchmark old bytes new bytes delta
BenchmarkQuerierSelect/Block/1of1000000-4 176001468 227201476 +29.09%
BenchmarkQuerierSelect/Block/10of1000000-4 176002620 227202628 +29.09%
BenchmarkQuerierSelect/Block/100of1000000-4 176014140 227214148 +29.09%
BenchmarkQuerierSelect/Block/1000of1000000-4 176129340 227329348 +29.07%
BenchmarkQuerierSelect/Block/10000of1000000-4 177281340 228481348 +28.88%
BenchmarkQuerierSelect/Block/100000of1000000-4 188801340 240001348 +27.12%
BenchmarkQuerierSelect/Block/1000000of1000000-4 304001340 355201616 +16.84%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-12 08:55:32 -08:00
|
|
|
closer := func() {
|
|
|
|
q.Close()
|
|
|
|
}
|
2016-10-12 10:34:22 -07:00
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
var (
|
|
|
|
vals []string
|
|
|
|
warnings storage.Warnings
|
|
|
|
)
|
|
|
|
if len(matcherSets) > 0 {
|
2021-02-09 09:38:35 -08:00
|
|
|
var callWarnings storage.Warnings
|
2020-12-22 03:02:19 -08:00
|
|
|
labelValuesSet := make(map[string]struct{})
|
2021-02-09 09:38:35 -08:00
|
|
|
for _, matchers := range matcherSets {
|
|
|
|
vals, callWarnings, err = q.LabelValues(name, matchers...)
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, closer}
|
2020-12-22 03:02:19 -08:00
|
|
|
}
|
2021-02-09 09:38:35 -08:00
|
|
|
warnings = append(warnings, callWarnings...)
|
|
|
|
for _, val := range vals {
|
|
|
|
labelValuesSet[val] = struct{}{}
|
2020-12-22 03:02:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = make([]string, 0, len(labelValuesSet))
|
2021-02-09 09:38:35 -08:00
|
|
|
for val := range labelValuesSet {
|
|
|
|
vals = append(vals, val)
|
2020-12-22 03:02:19 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vals, warnings, err = q.LabelValues(name)
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, warnings, closer}
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:38:35 -08:00
|
|
|
if vals == nil {
|
|
|
|
vals = []string{}
|
|
|
|
}
|
2020-07-31 08:03:02 -07:00
|
|
|
}
|
2015-06-04 09:07:57 -07:00
|
|
|
|
2021-02-09 09:38:35 -08:00
|
|
|
sort.Strings(vals)
|
|
|
|
|
Load only some offsets into the symbol table into memory.
Rather than keeping the entire symbol table in memory, keep every nth
offset and walk from there to the entry we need. This ends up slightly
slower, ~360ms per 1M series returned from PostingsForMatchers which is
not much considering the rest of the CPU such a query would go on to
use.
Make LabelValues use the postings tables, rather than having
to do symbol lookups. Use yoloString, as PostingsForMatchers
doesn't need the strings to stick around and adjust the API
call to keep the Querier open until it's all marshalled.
Remove allocatedSymbols memory optimisation, we no longer keep all the
symbol strings in heap memory. Remove LabelValuesFor and LabelIndices,
they're dead code. Ensure we've still tests for label indices,
and add missing test that we can work with old V1 Format index files.
PostingForMatchers performance is slightly better, with a big drop in
allocation counts due to using yoloString for LabelValues:
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 36698 36681 -0.05%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 522786 560887 +7.29%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 511652 537680 +5.09%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 522102 564239 +8.07%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113689911 111795919 -1.67%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 135825572 132871085 -2.18%
BenchmarkPostingsForMatchers/Block/i=~""-4 40782628 38038181 -6.73%
BenchmarkPostingsForMatchers/Block/i!=""-4 31267869 29194327 -6.63%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112733329 111568823 -1.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112868153 111232029 -1.45%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 31338257 29349446 -6.35%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 32054482 29972436 -6.50%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 136504654 133968442 -1.86%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 27960350 27264997 -2.49%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 136765564 133860724 -2.12%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 163714583 159453668 -2.60%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 6 6 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 11 11 +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 11 11 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 17 15 -11.76%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100012 12 -99.99%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200040 100040 -49.99%
BenchmarkPostingsForMatchers/Block/i=~""-4 200045 100045 -49.99%
BenchmarkPostingsForMatchers/Block/i!=""-4 200041 100041 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100017 17 -99.98%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100023 23 -99.98%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200046 100046 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200050 100050 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200049 100049 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111150 11150 -89.97%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200055 100055 -49.99%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311238 111238 -64.26%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 296 296 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 424 424 +0.00%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 424 424 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 552 1544 +179.71%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600482 1606125 +0.35%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 17259065 17264709 +0.03%
BenchmarkPostingsForMatchers/Block/i=~""-4 17259150 17264780 +0.03%
BenchmarkPostingsForMatchers/Block/i!=""-4 17259048 17264680 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600610 1606242 +0.35%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600813 1606434 +0.35%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 17259176 17264808 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 17259304 17264936 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 17259333 17264965 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3142628 3148262 +0.18%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 17259509 17265141 +0.03%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 20405680 20416944 +0.06%
However overall Select performance is down and involves more allocs, due to
having to do more than a simple map lookup to resolve a symbol and that all the strings
returned are allocated:
benchmark old ns/op new ns/op delta
BenchmarkQuerierSelect/Block/1of1000000-4 506092636 862678244 +70.46%
BenchmarkQuerierSelect/Block/10of1000000-4 505638968 860917636 +70.26%
BenchmarkQuerierSelect/Block/100of1000000-4 505229450 882150048 +74.60%
BenchmarkQuerierSelect/Block/1000of1000000-4 515905414 862241115 +67.13%
BenchmarkQuerierSelect/Block/10000of1000000-4 516785354 874841110 +69.29%
BenchmarkQuerierSelect/Block/100000of1000000-4 540742808 907030187 +67.74%
BenchmarkQuerierSelect/Block/1000000of1000000-4 815224288 1181236903 +44.90%
benchmark old allocs new allocs delta
BenchmarkQuerierSelect/Block/1of1000000-4 4000020 6000020 +50.00%
BenchmarkQuerierSelect/Block/10of1000000-4 4000038 6000038 +50.00%
BenchmarkQuerierSelect/Block/100of1000000-4 4000218 6000218 +50.00%
BenchmarkQuerierSelect/Block/1000of1000000-4 4002018 6002018 +49.97%
BenchmarkQuerierSelect/Block/10000of1000000-4 4020018 6020018 +49.75%
BenchmarkQuerierSelect/Block/100000of1000000-4 4200018 6200018 +47.62%
BenchmarkQuerierSelect/Block/1000000of1000000-4 6000018 8000019 +33.33%
benchmark old bytes new bytes delta
BenchmarkQuerierSelect/Block/1of1000000-4 176001468 227201476 +29.09%
BenchmarkQuerierSelect/Block/10of1000000-4 176002620 227202628 +29.09%
BenchmarkQuerierSelect/Block/100of1000000-4 176014140 227214148 +29.09%
BenchmarkQuerierSelect/Block/1000of1000000-4 176129340 227329348 +29.07%
BenchmarkQuerierSelect/Block/10000of1000000-4 177281340 228481348 +28.88%
BenchmarkQuerierSelect/Block/100000of1000000-4 188801340 240001348 +27.12%
BenchmarkQuerierSelect/Block/1000000of1000000-4 304001340 355201616 +16.84%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-12 08:55:32 -08:00
|
|
|
return apiFuncResult{vals, nil, warnings, closer}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2017-01-16 05:09:59 -08:00
|
|
|
var (
|
2019-07-08 02:43:59 -07:00
|
|
|
minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()
|
|
|
|
maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()
|
|
|
|
|
|
|
|
minTimeFormatted = minTime.Format(time.RFC3339Nano)
|
|
|
|
maxTimeFormatted = maxTime.Format(time.RFC3339Nano)
|
2017-01-16 05:09:59 -08:00
|
|
|
)
|
|
|
|
|
2020-04-17 13:40:39 -07:00
|
|
|
func (api *API) series(r *http.Request) (result apiFuncResult) {
|
2018-08-17 08:24:35 -07:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrapf(err, "error parsing form values")}, nil, nil}
|
2018-08-17 08:24:35 -07:00
|
|
|
}
|
2015-06-09 07:09:31 -07:00
|
|
|
if len(r.Form["match[]"]) == 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.New("no match[] parameter provided")}, nil, nil}
|
2015-06-09 07:09:31 -07:00
|
|
|
}
|
2016-05-11 14:59:52 -07:00
|
|
|
|
2020-03-06 02:33:01 -08:00
|
|
|
start, err := parseTimeParam(r, "start", minTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "start")
|
2016-05-11 14:59:52 -07:00
|
|
|
}
|
2020-03-06 02:33:01 -08:00
|
|
|
end, err := parseTimeParam(r, "end", maxTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "end")
|
2016-05-11 14:59:52 -07:00
|
|
|
}
|
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
matcherSets, err := parseMatchersParam(r.Form["match[]"])
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "match[]")
|
2020-12-15 09:24:57 -08:00
|
|
|
}
|
|
|
|
|
2021-01-30 03:04:48 -08:00
|
|
|
q, err := api.Queryable.Querier(r.Context(), timestamp.FromTime(start), timestamp.FromTime(end))
|
2016-12-30 01:43:44 -08:00
|
|
|
if err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorExec, err}, nil, nil}
|
2016-12-30 01:43:44 -08:00
|
|
|
}
|
2020-04-17 13:40:39 -07:00
|
|
|
// From now on, we must only return with a finalizer in the result (to
|
|
|
|
// be called by the caller) or call q.Close ourselves (which is required
|
|
|
|
// in the case of a panic).
|
|
|
|
defer func() {
|
|
|
|
if result.finalizer == nil {
|
|
|
|
q.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
closer := func() {
|
|
|
|
q.Close()
|
|
|
|
}
|
2016-12-30 01:43:44 -08:00
|
|
|
|
2020-10-14 02:06:17 -07:00
|
|
|
hints := &storage.SelectHints{
|
|
|
|
Start: timestamp.FromTime(start),
|
|
|
|
End: timestamp.FromTime(end),
|
|
|
|
Func: "series", // There is no series function, this token is used for lookups that don't need samples.
|
|
|
|
}
|
|
|
|
|
2017-12-10 03:00:23 -08:00
|
|
|
var sets []storage.SeriesSet
|
2016-12-30 01:43:44 -08:00
|
|
|
for _, mset := range matcherSets {
|
2020-08-28 16:21:39 -07:00
|
|
|
// We need to sort this select results to merge (deduplicate) the series sets later.
|
2020-10-14 02:06:17 -07:00
|
|
|
s := q.Select(true, hints, mset...)
|
2017-12-10 03:00:23 -08:00
|
|
|
sets = append(sets, s)
|
2016-12-30 01:43:44 -08:00
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
set := storage.NewMergeSeriesSet(sets, storage.ChainedSeriesMerge)
|
2017-04-04 02:09:11 -07:00
|
|
|
metrics := []labels.Labels{}
|
|
|
|
for set.Next() {
|
|
|
|
metrics = append(metrics, set.At().Labels())
|
|
|
|
}
|
2020-06-09 09:57:31 -07:00
|
|
|
|
|
|
|
warnings := set.Warnings()
|
2017-04-04 02:09:11 -07:00
|
|
|
if set.Err() != nil {
|
2020-04-17 13:40:39 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorExec, set.Err()}, warnings, closer}
|
2017-04-04 02:09:11 -07:00
|
|
|
}
|
|
|
|
|
2020-04-17 13:40:39 -07:00
|
|
|
return apiFuncResult{metrics, nil, warnings, closer}
|
2015-06-09 07:09:31 -07:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func (api *API) dropSeries(_ *http.Request) apiFuncResult {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, errors.New("not implemented")}, nil, nil}
|
2015-06-09 07:09:31 -07:00
|
|
|
}
|
|
|
|
|
2017-03-06 09:51:27 -08:00
|
|
|
// Target has the information for one target.
|
2016-12-02 04:31:43 -08:00
|
|
|
type Target struct {
|
|
|
|
// Labels before any processing.
|
2016-12-29 00:27:30 -08:00
|
|
|
DiscoveredLabels map[string]string `json:"discoveredLabels"`
|
2016-12-02 04:31:43 -08:00
|
|
|
// Any labels that are added to this target and its metrics.
|
2016-12-29 00:27:30 -08:00
|
|
|
Labels map[string]string `json:"labels"`
|
2016-12-02 04:31:43 -08:00
|
|
|
|
2019-11-11 13:42:24 -08:00
|
|
|
ScrapePool string `json:"scrapePool"`
|
|
|
|
ScrapeURL string `json:"scrapeUrl"`
|
2020-02-17 09:19:15 -08:00
|
|
|
GlobalURL string `json:"globalUrl"`
|
2016-12-02 04:31:43 -08:00
|
|
|
|
2019-11-11 13:42:24 -08:00
|
|
|
LastError string `json:"lastError"`
|
|
|
|
LastScrape time.Time `json:"lastScrape"`
|
|
|
|
LastScrapeDuration float64 `json:"lastScrapeDuration"`
|
|
|
|
Health scrape.TargetHealth `json:"health"`
|
2021-08-31 08:37:32 -07:00
|
|
|
|
|
|
|
ScrapeInterval string `json:"scrapeInterval"`
|
|
|
|
ScrapeTimeout string `json:"scrapeTimeout"`
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 09:26:18 -08:00
|
|
|
// DroppedTarget has the information for one target that was dropped during relabelling.
|
|
|
|
type DroppedTarget struct {
|
|
|
|
// Labels before any processing.
|
|
|
|
DiscoveredLabels map[string]string `json:"discoveredLabels"`
|
|
|
|
}
|
|
|
|
|
2017-03-06 03:46:37 -08:00
|
|
|
// TargetDiscovery has all the active targets.
|
2017-01-13 08:15:04 -08:00
|
|
|
type TargetDiscovery struct {
|
2018-10-25 01:19:20 -07:00
|
|
|
ActiveTargets []*Target `json:"activeTargets"`
|
|
|
|
DroppedTargets []*DroppedTarget `json:"droppedTargets"`
|
2017-01-13 08:15:04 -08:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:19:15 -08:00
|
|
|
// GlobalURLOptions contains fields used for deriving the global URL for local targets.
|
|
|
|
type GlobalURLOptions struct {
|
|
|
|
ListenAddress string
|
|
|
|
Host string
|
|
|
|
Scheme string
|
|
|
|
}
|
|
|
|
|
2021-02-05 03:45:44 -08:00
|
|
|
// sanitizeSplitHostPort acts like net.SplitHostPort.
|
|
|
|
// Additionally, if there is no port in the host passed as input, we return the
|
|
|
|
// original host, making sure that IPv6 addresses are not surrounded by square
|
|
|
|
// brackets.
|
|
|
|
func sanitizeSplitHostPort(input string) (string, string, error) {
|
|
|
|
host, port, err := net.SplitHostPort(input)
|
|
|
|
if err != nil && strings.HasSuffix(err.Error(), "missing port in address") {
|
|
|
|
var errWithPort error
|
|
|
|
host, _, errWithPort = net.SplitHostPort(input + ":80")
|
|
|
|
if errWithPort == nil {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return host, port, err
|
|
|
|
}
|
|
|
|
|
2020-02-17 09:19:15 -08:00
|
|
|
func getGlobalURL(u *url.URL, opts GlobalURLOptions) (*url.URL, error) {
|
2021-02-05 03:45:44 -08:00
|
|
|
host, port, err := sanitizeSplitHostPort(u.Host)
|
2020-02-17 09:19:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, lhr := range LocalhostRepresentations {
|
|
|
|
if host == lhr {
|
|
|
|
_, ownPort, err := net.SplitHostPort(opts.ListenAddress)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if port == ownPort {
|
|
|
|
// Only in the case where the target is on localhost and its port is
|
|
|
|
// the same as the one we're listening on, we know for sure that
|
|
|
|
// we're monitoring our own process and that we need to change the
|
|
|
|
// scheme, hostname, and port to the externally reachable ones as
|
|
|
|
// well. We shouldn't need to touch the path at all, since if a
|
|
|
|
// path prefix is defined, the path under which we scrape ourselves
|
|
|
|
// should already contain the prefix.
|
|
|
|
u.Scheme = opts.Scheme
|
|
|
|
u.Host = opts.Host
|
|
|
|
} else {
|
|
|
|
// Otherwise, we only know that localhost is not reachable
|
|
|
|
// externally, so we replace only the hostname by the one in the
|
|
|
|
// external URL. It could be the wrong hostname for the service on
|
|
|
|
// this port, but it's still the best possible guess.
|
2021-02-05 03:45:44 -08:00
|
|
|
host, _, err := sanitizeSplitHostPort(opts.Host)
|
2020-02-17 09:19:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
2021-02-05 03:45:44 -08:00
|
|
|
u.Host = host
|
|
|
|
if port != "" {
|
|
|
|
u.Host = net.JoinHostPort(u.Host, port)
|
|
|
|
}
|
2020-02-17 09:19:15 -08:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) targets(r *http.Request) apiFuncResult {
|
2019-11-11 13:42:24 -08:00
|
|
|
sortKeys := func(targets map[string][]*scrape.Target) ([]string, int) {
|
2018-10-25 01:19:20 -07:00
|
|
|
var n int
|
|
|
|
keys := make([]string, 0, len(targets))
|
|
|
|
for k := range targets {
|
|
|
|
keys = append(keys, k)
|
|
|
|
n += len(targets[k])
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2019-11-11 13:42:24 -08:00
|
|
|
return keys, n
|
|
|
|
}
|
|
|
|
|
|
|
|
flatten := func(targets map[string][]*scrape.Target) []*scrape.Target {
|
|
|
|
keys, n := sortKeys(targets)
|
2018-10-25 01:19:20 -07:00
|
|
|
res := make([]*scrape.Target, 0, n)
|
|
|
|
for _, k := range keys {
|
|
|
|
res = append(res, targets[k]...)
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
2018-10-25 01:19:20 -07:00
|
|
|
return res
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
|
|
|
|
2019-11-11 13:42:24 -08:00
|
|
|
state := strings.ToLower(r.URL.Query().Get("state"))
|
|
|
|
showActive := state == "" || state == "any" || state == "active"
|
|
|
|
showDropped := state == "" || state == "any" || state == "dropped"
|
|
|
|
res := &TargetDiscovery{}
|
|
|
|
|
|
|
|
if showActive {
|
2020-04-16 01:30:47 -07:00
|
|
|
targetsActive := api.targetRetriever(r.Context()).TargetsActive()
|
2019-11-11 13:42:24 -08:00
|
|
|
activeKeys, numTargets := sortKeys(targetsActive)
|
|
|
|
res.ActiveTargets = make([]*Target, 0, numTargets)
|
|
|
|
|
|
|
|
for _, key := range activeKeys {
|
|
|
|
for _, target := range targetsActive[key] {
|
|
|
|
lastErrStr := ""
|
|
|
|
lastErr := target.LastError()
|
|
|
|
if lastErr != nil {
|
|
|
|
lastErrStr = lastErr.Error()
|
|
|
|
}
|
|
|
|
|
2020-02-17 09:19:15 -08:00
|
|
|
globalURL, err := getGlobalURL(target.URL(), api.globalURLOptions)
|
|
|
|
|
2019-11-11 13:42:24 -08:00
|
|
|
res.ActiveTargets = append(res.ActiveTargets, &Target{
|
2020-02-17 09:19:15 -08:00
|
|
|
DiscoveredLabels: target.DiscoveredLabels().Map(),
|
|
|
|
Labels: target.Labels().Map(),
|
|
|
|
ScrapePool: key,
|
|
|
|
ScrapeURL: target.URL().String(),
|
|
|
|
GlobalURL: globalURL.String(),
|
|
|
|
LastError: func() string {
|
|
|
|
if err == nil && lastErrStr == "" {
|
|
|
|
return ""
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.Wrapf(err, lastErrStr).Error()
|
|
|
|
}
|
|
|
|
return lastErrStr
|
|
|
|
}(),
|
2019-11-11 13:42:24 -08:00
|
|
|
LastScrape: target.LastScrape(),
|
|
|
|
LastScrapeDuration: target.LastScrapeDuration().Seconds(),
|
|
|
|
Health: target.Health(),
|
2021-08-31 08:37:32 -07:00
|
|
|
ScrapeInterval: target.GetValue(model.ScrapeIntervalLabel),
|
|
|
|
ScrapeTimeout: target.GetValue(model.ScrapeTimeoutLabel),
|
2019-11-11 13:42:24 -08:00
|
|
|
})
|
|
|
|
}
|
2018-02-21 09:26:18 -08:00
|
|
|
}
|
2019-11-11 13:42:24 -08:00
|
|
|
} else {
|
|
|
|
res.ActiveTargets = []*Target{}
|
|
|
|
}
|
|
|
|
if showDropped {
|
2020-04-16 01:30:47 -07:00
|
|
|
tDropped := flatten(api.targetRetriever(r.Context()).TargetsDropped())
|
2019-11-11 13:42:24 -08:00
|
|
|
res.DroppedTargets = make([]*DroppedTarget, 0, len(tDropped))
|
|
|
|
for _, t := range tDropped {
|
|
|
|
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
|
|
|
DiscoveredLabels: t.DiscoveredLabels().Map(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.DroppedTargets = []*DroppedTarget{}
|
2018-02-21 09:26:18 -08:00
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
2016-12-02 04:31:43 -08:00
|
|
|
}
|
|
|
|
|
2018-11-08 06:11:38 -08:00
|
|
|
func matchLabels(lset labels.Labels, matchers []*labels.Matcher) bool {
|
|
|
|
for _, m := range matchers {
|
|
|
|
if !m.Matches(lset.Get(m.Name)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) targetMetadata(r *http.Request) apiFuncResult {
|
2018-05-18 00:32:11 -07:00
|
|
|
limit := -1
|
|
|
|
if s := r.FormValue("limit"); s != "" {
|
|
|
|
var err error
|
|
|
|
if limit, err = strconv.Atoi(s); err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.New("limit must be a number")}, nil, nil}
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 03:09:44 -08:00
|
|
|
matchTarget := r.FormValue("match_target")
|
|
|
|
var matchers []*labels.Matcher
|
|
|
|
var err error
|
|
|
|
if matchTarget != "" {
|
2020-02-03 10:06:39 -08:00
|
|
|
matchers, err = parser.ParseMetricSelector(matchTarget)
|
2019-11-14 03:09:44 -08:00
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "match_target")
|
2019-11-14 03:09:44 -08:00
|
|
|
}
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
|
|
|
|
2018-06-05 03:30:19 -07:00
|
|
|
metric := r.FormValue("metric")
|
2019-12-10 06:56:16 -08:00
|
|
|
res := []metricMetadata{}
|
2020-04-16 01:30:47 -07:00
|
|
|
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
2018-09-26 02:20:56 -07:00
|
|
|
for _, t := range tt {
|
|
|
|
if limit >= 0 && len(res) >= limit {
|
|
|
|
break
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
2018-11-08 06:11:38 -08:00
|
|
|
// Filter targets that don't satisfy the label matchers.
|
2019-11-14 03:09:44 -08:00
|
|
|
if matchTarget != "" && !matchLabels(t.Labels(), matchers) {
|
2018-11-08 06:11:38 -08:00
|
|
|
continue
|
2018-09-26 02:20:56 -07:00
|
|
|
}
|
|
|
|
// If no metric is specified, get the full list for the target.
|
|
|
|
if metric == "" {
|
|
|
|
for _, md := range t.MetadataList() {
|
|
|
|
res = append(res, metricMetadata{
|
|
|
|
Target: t.Labels(),
|
|
|
|
Metric: md.Metric,
|
|
|
|
Type: md.Type,
|
|
|
|
Help: md.Help,
|
2018-10-05 09:11:16 -07:00
|
|
|
Unit: md.Unit,
|
2018-09-26 02:20:56 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get metadata for the specified metric.
|
|
|
|
if md, ok := t.Metadata(metric); ok {
|
2018-05-18 00:32:11 -07:00
|
|
|
res = append(res, metricMetadata{
|
|
|
|
Target: t.Labels(),
|
|
|
|
Type: md.Type,
|
|
|
|
Help: md.Help,
|
2018-10-05 09:11:16 -07:00
|
|
|
Unit: md.Unit,
|
2018-05-18 00:32:11 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 06:56:16 -08:00
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricMetadata struct {
|
|
|
|
Target labels.Labels `json:"target"`
|
|
|
|
Metric string `json:"metric,omitempty"`
|
|
|
|
Type textparse.MetricType `json:"type"`
|
|
|
|
Help string `json:"help"`
|
2018-10-05 09:11:16 -07:00
|
|
|
Unit string `json:"unit"`
|
2018-05-18 00:32:11 -07:00
|
|
|
}
|
|
|
|
|
2017-03-06 09:51:27 -08:00
|
|
|
// AlertmanagerDiscovery has all the active Alertmanagers.
|
2017-01-13 01:20:11 -08:00
|
|
|
type AlertmanagerDiscovery struct {
|
2018-02-21 01:00:07 -08:00
|
|
|
ActiveAlertmanagers []*AlertmanagerTarget `json:"activeAlertmanagers"`
|
|
|
|
DroppedAlertmanagers []*AlertmanagerTarget `json:"droppedAlertmanagers"`
|
2017-01-13 01:20:11 -08:00
|
|
|
}
|
|
|
|
|
2017-03-06 09:51:27 -08:00
|
|
|
// AlertmanagerTarget has info on one AM.
|
2017-01-13 01:20:11 -08:00
|
|
|
type AlertmanagerTarget struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) alertmanagers(r *http.Request) apiFuncResult {
|
2020-05-18 11:02:32 -07:00
|
|
|
urls := api.alertmanagerRetriever(r.Context()).Alertmanagers()
|
|
|
|
droppedURLS := api.alertmanagerRetriever(r.Context()).DroppedAlertmanagers()
|
2018-02-21 01:00:07 -08:00
|
|
|
ams := &AlertmanagerDiscovery{ActiveAlertmanagers: make([]*AlertmanagerTarget, len(urls)), DroppedAlertmanagers: make([]*AlertmanagerTarget, len(droppedURLS))}
|
2017-04-24 22:42:33 -07:00
|
|
|
for i, url := range urls {
|
|
|
|
ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: url.String()}
|
2017-01-13 01:20:11 -08:00
|
|
|
}
|
2018-02-21 01:00:07 -08:00
|
|
|
for i, url := range droppedURLS {
|
|
|
|
ams.DroppedAlertmanagers[i] = &AlertmanagerTarget{URL: url.String()}
|
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{ams, nil, nil, nil}
|
2017-01-13 01:20:11 -08:00
|
|
|
}
|
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
// AlertDiscovery has info for all active alerts.
|
2018-03-25 09:50:34 -07:00
|
|
|
type AlertDiscovery struct {
|
2018-06-27 00:15:17 -07:00
|
|
|
Alerts []*Alert `json:"alerts"`
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
// Alert has info for an alert.
|
2018-03-25 09:50:34 -07:00
|
|
|
type Alert struct {
|
|
|
|
Labels labels.Labels `json:"labels"`
|
2018-06-27 00:15:17 -07:00
|
|
|
Annotations labels.Labels `json:"annotations"`
|
|
|
|
State string `json:"state"`
|
|
|
|
ActiveAt *time.Time `json:"activeAt,omitempty"`
|
2019-05-21 02:41:54 -07:00
|
|
|
Value string `json:"value"`
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) alerts(r *http.Request) apiFuncResult {
|
2020-05-18 11:02:32 -07:00
|
|
|
alertingRules := api.rulesRetriever(r.Context()).AlertingRules()
|
2018-06-27 00:15:17 -07:00
|
|
|
alerts := []*Alert{}
|
2018-03-25 09:50:34 -07:00
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
for _, alertingRule := range alertingRules {
|
|
|
|
alerts = append(
|
|
|
|
alerts,
|
|
|
|
rulesAlertsToAPIAlerts(alertingRule.ActiveAlerts())...,
|
|
|
|
)
|
|
|
|
}
|
2018-03-25 09:50:34 -07:00
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
res := &AlertDiscovery{Alerts: alerts}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
2018-06-27 00:15:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func rulesAlertsToAPIAlerts(rulesAlerts []*rules.Alert) []*Alert {
|
|
|
|
apiAlerts := make([]*Alert, len(rulesAlerts))
|
|
|
|
for i, ruleAlert := range rulesAlerts {
|
|
|
|
apiAlerts[i] = &Alert{
|
|
|
|
Labels: ruleAlert.Labels,
|
|
|
|
Annotations: ruleAlert.Annotations,
|
|
|
|
State: ruleAlert.State.String(),
|
|
|
|
ActiveAt: &ruleAlert.ActiveAt,
|
2019-05-21 02:41:54 -07:00
|
|
|
Value: strconv.FormatFloat(ruleAlert.Value, 'e', -1, 64),
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
return apiAlerts
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 06:56:16 -08:00
|
|
|
type metadata struct {
|
|
|
|
Type textparse.MetricType `json:"type"`
|
|
|
|
Help string `json:"help"`
|
|
|
|
Unit string `json:"unit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) metricMetadata(r *http.Request) apiFuncResult {
|
|
|
|
metrics := map[string]map[metadata]struct{}{}
|
|
|
|
|
|
|
|
limit := -1
|
|
|
|
if s := r.FormValue("limit"); s != "" {
|
|
|
|
var err error
|
|
|
|
if limit, err = strconv.Atoi(s); err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.New("limit must be a number")}, nil, nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 07:22:10 -08:00
|
|
|
metric := r.FormValue("metric")
|
2020-04-16 01:30:47 -07:00
|
|
|
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
2019-12-10 06:56:16 -08:00
|
|
|
for _, t := range tt {
|
2019-12-10 07:22:10 -08:00
|
|
|
|
|
|
|
if metric == "" {
|
|
|
|
for _, mm := range t.MetadataList() {
|
|
|
|
m := metadata{Type: mm.Type, Help: mm.Help, Unit: mm.Unit}
|
|
|
|
ms, ok := metrics[mm.Metric]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
ms = map[metadata]struct{}{}
|
|
|
|
metrics[mm.Metric] = ms
|
|
|
|
}
|
|
|
|
ms[m] = struct{}{}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if md, ok := t.Metadata(metric); ok {
|
|
|
|
m := metadata{Type: md.Type, Help: md.Help, Unit: md.Unit}
|
|
|
|
ms, ok := metrics[md.Metric]
|
2019-12-10 06:56:16 -08:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
ms = map[metadata]struct{}{}
|
2019-12-10 07:22:10 -08:00
|
|
|
metrics[md.Metric] = ms
|
2019-12-10 06:56:16 -08:00
|
|
|
}
|
|
|
|
ms[m] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 07:22:10 -08:00
|
|
|
// Put the elements from the pseudo-set into a slice for marshaling.
|
2019-12-10 06:56:16 -08:00
|
|
|
res := map[string][]metadata{}
|
|
|
|
for name, set := range metrics {
|
|
|
|
if limit >= 0 && len(res) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
s := []metadata{}
|
|
|
|
for metadata := range set {
|
|
|
|
s = append(s, metadata)
|
|
|
|
}
|
|
|
|
res[name] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
|
|
|
}
|
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
// RuleDiscovery has info for all rules
|
|
|
|
type RuleDiscovery struct {
|
|
|
|
RuleGroups []*RuleGroup `json:"groups"`
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
// RuleGroup has info for rules which are part of a group
|
|
|
|
type RuleGroup struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
File string `json:"file"`
|
|
|
|
// In order to preserve rule ordering, while exposing type (alerting or recording)
|
|
|
|
// specific properties, both alerting and recording rules are exposed in the
|
|
|
|
// same array.
|
2021-11-21 09:00:27 -08:00
|
|
|
Rules []Rule `json:"rules"`
|
2020-01-27 01:27:43 -08:00
|
|
|
Interval float64 `json:"interval"`
|
2022-01-11 19:44:22 -08:00
|
|
|
Limit int `json:"limit"`
|
2020-01-27 01:27:43 -08:00
|
|
|
EvaluationTime float64 `json:"evaluationTime"`
|
|
|
|
LastEvaluation time.Time `json:"lastEvaluation"`
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2021-11-21 09:00:27 -08:00
|
|
|
type Rule interface{}
|
2018-03-25 09:50:34 -07:00
|
|
|
|
2021-11-21 09:00:27 -08:00
|
|
|
type AlertingRule struct {
|
2019-12-09 14:42:59 -08:00
|
|
|
// State can be "pending", "firing", "inactive".
|
2020-01-27 01:27:43 -08:00
|
|
|
State string `json:"state"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Duration float64 `json:"duration"`
|
|
|
|
Labels labels.Labels `json:"labels"`
|
|
|
|
Annotations labels.Labels `json:"annotations"`
|
|
|
|
Alerts []*Alert `json:"alerts"`
|
|
|
|
Health rules.RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
EvaluationTime float64 `json:"evaluationTime"`
|
|
|
|
LastEvaluation time.Time `json:"lastEvaluation"`
|
2018-06-27 00:15:17 -07:00
|
|
|
// Type of an alertingRule is always "alerting".
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2021-11-21 09:00:27 -08:00
|
|
|
type RecordingRule struct {
|
2020-01-27 01:27:43 -08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Labels labels.Labels `json:"labels,omitempty"`
|
|
|
|
Health rules.RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
EvaluationTime float64 `json:"evaluationTime"`
|
|
|
|
LastEvaluation time.Time `json:"lastEvaluation"`
|
2018-06-27 00:15:17 -07:00
|
|
|
// Type of a recordingRule is always "recording".
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) rules(r *http.Request) apiFuncResult {
|
2020-05-18 11:02:32 -07:00
|
|
|
ruleGroups := api.rulesRetriever(r.Context()).RuleGroups()
|
2018-06-27 00:15:17 -07:00
|
|
|
res := &RuleDiscovery{RuleGroups: make([]*RuleGroup, len(ruleGroups))}
|
2020-12-23 16:27:13 -08:00
|
|
|
typ := strings.ToLower(r.URL.Query().Get("type"))
|
2019-12-09 14:42:59 -08:00
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
if typ != "" && typ != "alert" && typ != "record" {
|
|
|
|
return invalidParamError(errors.Errorf("not supported value %q", typ), "type")
|
2019-12-09 14:42:59 -08:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
returnAlerts := typ == "" || typ == "alert"
|
|
|
|
returnRecording := typ == "" || typ == "record"
|
2019-12-09 14:42:59 -08:00
|
|
|
|
2018-06-27 00:15:17 -07:00
|
|
|
for i, grp := range ruleGroups {
|
|
|
|
apiRuleGroup := &RuleGroup{
|
2020-01-27 01:27:43 -08:00
|
|
|
Name: grp.Name(),
|
|
|
|
File: grp.File(),
|
|
|
|
Interval: grp.Interval().Seconds(),
|
2022-01-11 19:44:22 -08:00
|
|
|
Limit: grp.Limit(),
|
2021-11-21 09:00:27 -08:00
|
|
|
Rules: []Rule{},
|
2020-08-25 03:38:06 -07:00
|
|
|
EvaluationTime: grp.GetEvaluationTime().Seconds(),
|
|
|
|
LastEvaluation: grp.GetLastEvaluation(),
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
2018-06-27 00:15:17 -07:00
|
|
|
for _, r := range grp.Rules() {
|
2021-11-21 09:00:27 -08:00
|
|
|
var enrichedRule Rule
|
2018-06-27 00:15:17 -07:00
|
|
|
|
2018-08-23 06:00:10 -07:00
|
|
|
lastError := ""
|
|
|
|
if r.LastError() != nil {
|
|
|
|
lastError = r.LastError().Error()
|
|
|
|
}
|
2018-06-27 00:15:17 -07:00
|
|
|
switch rule := r.(type) {
|
|
|
|
case *rules.AlertingRule:
|
2019-12-09 14:42:59 -08:00
|
|
|
if !returnAlerts {
|
|
|
|
break
|
|
|
|
}
|
2021-11-21 09:00:27 -08:00
|
|
|
enrichedRule = AlertingRule{
|
2020-01-27 01:27:43 -08:00
|
|
|
State: rule.State().String(),
|
|
|
|
Name: rule.Name(),
|
|
|
|
Query: rule.Query().String(),
|
2020-01-31 23:31:37 -08:00
|
|
|
Duration: rule.HoldDuration().Seconds(),
|
2020-01-27 01:27:43 -08:00
|
|
|
Labels: rule.Labels(),
|
|
|
|
Annotations: rule.Annotations(),
|
|
|
|
Alerts: rulesAlertsToAPIAlerts(rule.ActiveAlerts()),
|
|
|
|
Health: rule.Health(),
|
|
|
|
LastError: lastError,
|
|
|
|
EvaluationTime: rule.GetEvaluationDuration().Seconds(),
|
|
|
|
LastEvaluation: rule.GetEvaluationTimestamp(),
|
|
|
|
Type: "alerting",
|
2018-06-27 00:15:17 -07:00
|
|
|
}
|
|
|
|
case *rules.RecordingRule:
|
2019-12-09 14:42:59 -08:00
|
|
|
if !returnRecording {
|
|
|
|
break
|
|
|
|
}
|
2021-11-21 09:00:27 -08:00
|
|
|
enrichedRule = RecordingRule{
|
2020-01-27 01:27:43 -08:00
|
|
|
Name: rule.Name(),
|
|
|
|
Query: rule.Query().String(),
|
|
|
|
Labels: rule.Labels(),
|
|
|
|
Health: rule.Health(),
|
|
|
|
LastError: lastError,
|
|
|
|
EvaluationTime: rule.GetEvaluationDuration().Seconds(),
|
|
|
|
LastEvaluation: rule.GetEvaluationTimestamp(),
|
|
|
|
Type: "recording",
|
2018-06-27 00:15:17 -07:00
|
|
|
}
|
|
|
|
default:
|
2019-03-25 16:01:12 -07:00
|
|
|
err := errors.Errorf("failed to assert type of rule '%v'", rule.Name())
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil}
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
2019-12-09 14:42:59 -08:00
|
|
|
if enrichedRule != nil {
|
|
|
|
apiRuleGroup.Rules = append(apiRuleGroup.Rules, enrichedRule)
|
|
|
|
}
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
2018-06-27 00:15:17 -07:00
|
|
|
res.RuleGroups[i] = apiRuleGroup
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{res, nil, nil, nil}
|
2018-03-25 09:50:34 -07:00
|
|
|
}
|
|
|
|
|
2017-05-11 08:09:24 -07:00
|
|
|
type prometheusConfig struct {
|
|
|
|
YAML string `json:"yaml"`
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func (api *API) serveRuntimeInfo(_ *http.Request) apiFuncResult {
|
2019-11-02 08:53:32 -07:00
|
|
|
status, err := api.runtimeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{status, &apiError{errorInternal, err}, nil, nil}
|
|
|
|
}
|
|
|
|
return apiFuncResult{status, nil, nil, nil}
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func (api *API) serveBuildInfo(_ *http.Request) apiFuncResult {
|
2019-11-02 08:53:32 -07:00
|
|
|
return apiFuncResult{api.buildInfo, nil, nil, nil}
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func (api *API) serveConfig(_ *http.Request) apiFuncResult {
|
2017-05-11 08:09:24 -07:00
|
|
|
cfg := &prometheusConfig{
|
|
|
|
YAML: api.config().String(),
|
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{cfg, nil, nil, nil}
|
2017-05-11 08:09:24 -07:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:27:13 -08:00
|
|
|
func (api *API) serveFlags(_ *http.Request) apiFuncResult {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{api.flagsMap, nil, nil, nil}
|
api: Added v1/status/flags endpoint. (#3864)
Endpoint URL: /api/v1/status/flags
Example Output:
```json
{
"status": "success",
"data": {
"alertmanager.notification-queue-capacity": "10000",
"alertmanager.timeout": "10s",
"completion-bash": "false",
"completion-script-bash": "false",
"completion-script-zsh": "false",
"config.file": "my_cool_prometheus.yaml",
"help": "false",
"help-long": "false",
"help-man": "false",
"log.level": "info",
"query.lookback-delta": "5m",
"query.max-concurrency": "20",
"query.timeout": "2m",
"storage.tsdb.max-block-duration": "36h",
"storage.tsdb.min-block-duration": "2h",
"storage.tsdb.no-lockfile": "false",
"storage.tsdb.path": "data/",
"storage.tsdb.retention": "15d",
"version": "false",
"web.console.libraries": "console_libraries",
"web.console.templates": "consoles",
"web.enable-admin-api": "false",
"web.enable-lifecycle": "false",
"web.external-url": "",
"web.listen-address": "0.0.0.0:9090",
"web.max-connections": "512",
"web.read-timeout": "5m",
"web.route-prefix": "/",
"web.user-assets": ""
}
}
```
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
2018-02-21 00:49:02 -08:00
|
|
|
}
|
|
|
|
|
2019-11-12 02:15:20 -08:00
|
|
|
// stat holds the information about individual cardinality.
|
|
|
|
type stat struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value uint64 `json:"value"`
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:05:33 -07:00
|
|
|
// HeadStats has information about the TSDB head.
|
|
|
|
type HeadStats struct {
|
2021-01-06 22:41:32 -08:00
|
|
|
NumSeries uint64 `json:"numSeries"`
|
|
|
|
NumLabelPairs int `json:"numLabelPairs"`
|
|
|
|
ChunkCount int64 `json:"chunkCount"`
|
|
|
|
MinTime int64 `json:"minTime"`
|
|
|
|
MaxTime int64 `json:"maxTime"`
|
2020-09-29 13:05:33 -07:00
|
|
|
}
|
|
|
|
|
2019-11-12 02:15:20 -08:00
|
|
|
// tsdbStatus has information of cardinality statistics from postings.
|
|
|
|
type tsdbStatus struct {
|
2020-09-29 13:05:33 -07:00
|
|
|
HeadStats HeadStats `json:"headStats"`
|
|
|
|
SeriesCountByMetricName []stat `json:"seriesCountByMetricName"`
|
|
|
|
LabelValueCountByLabelName []stat `json:"labelValueCountByLabelName"`
|
|
|
|
MemoryInBytesByLabelName []stat `json:"memoryInBytesByLabelName"`
|
|
|
|
SeriesCountByLabelValuePair []stat `json:"seriesCountByLabelValuePair"`
|
2019-11-12 02:15:20 -08:00
|
|
|
}
|
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
func convertStats(stats []index.Stat) []stat {
|
|
|
|
result := make([]stat, 0, len(stats))
|
|
|
|
for _, item := range stats {
|
|
|
|
item := stat{Name: item.Name, Value: item.Count}
|
|
|
|
result = append(result, item)
|
2019-11-12 02:15:20 -08:00
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
return result
|
|
|
|
}
|
2019-11-12 02:15:20 -08:00
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
func (api *API) serveTSDBStatus(*http.Request) apiFuncResult {
|
|
|
|
s, err := api.db.Stats("__name__")
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil}
|
2019-11-12 02:15:20 -08:00
|
|
|
}
|
2020-09-29 13:05:33 -07:00
|
|
|
metrics, err := api.gatherer.Gather()
|
|
|
|
if err != nil {
|
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, fmt.Errorf("error gathering runtime status: %s", err)}, nil, nil}
|
|
|
|
}
|
|
|
|
chunkCount := int64(math.NaN())
|
|
|
|
for _, mF := range metrics {
|
|
|
|
if *mF.Name == "prometheus_tsdb_head_chunks" {
|
|
|
|
m := *mF.Metric[0]
|
|
|
|
if m.Gauge != nil {
|
|
|
|
chunkCount = int64(m.Gauge.GetValue())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
return apiFuncResult{tsdbStatus{
|
2020-09-29 13:05:33 -07:00
|
|
|
HeadStats: HeadStats{
|
2021-01-06 22:41:32 -08:00
|
|
|
NumSeries: s.NumSeries,
|
|
|
|
ChunkCount: chunkCount,
|
|
|
|
MinTime: s.MinTime,
|
|
|
|
MaxTime: s.MaxTime,
|
|
|
|
NumLabelPairs: s.IndexPostingStats.NumLabelPairs,
|
2020-09-29 13:05:33 -07:00
|
|
|
},
|
2020-04-29 09:16:14 -07:00
|
|
|
SeriesCountByMetricName: convertStats(s.IndexPostingStats.CardinalityMetricsStats),
|
|
|
|
LabelValueCountByLabelName: convertStats(s.IndexPostingStats.CardinalityLabelStats),
|
|
|
|
MemoryInBytesByLabelName: convertStats(s.IndexPostingStats.LabelValueStats),
|
|
|
|
SeriesCountByLabelValuePair: convertStats(s.IndexPostingStats.LabelValuePairsStats),
|
|
|
|
}, nil, nil, nil}
|
2019-11-12 02:15:20 -08:00
|
|
|
}
|
|
|
|
|
2021-06-05 07:29:32 -07:00
|
|
|
type walReplayStatus struct {
|
|
|
|
Min int `json:"min"`
|
|
|
|
Max int `json:"max"`
|
|
|
|
Current int `json:"current"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) serveWALReplayStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httputil.SetCORS(w, api.CORSOrigin, r)
|
|
|
|
status, err := api.db.WALReplayStatus()
|
|
|
|
if err != nil {
|
|
|
|
api.respondError(w, &apiError{errorInternal, err}, nil)
|
|
|
|
}
|
|
|
|
api.respond(w, walReplayStatus{
|
|
|
|
Min: status.Min,
|
|
|
|
Max: status.Max,
|
|
|
|
Current: status.Current,
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
|
2017-10-23 13:28:17 -07:00
|
|
|
func (api *API) remoteRead(w http.ResponseWriter, r *http.Request) {
|
2021-02-26 08:43:19 -08:00
|
|
|
// This is only really for tests - this will never be nil IRL.
|
|
|
|
if api.remoteReadHandler != nil {
|
|
|
|
api.remoteReadHandler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
2019-08-19 13:16:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 03:04:48 -08:00
|
|
|
func (api *API) remoteWrite(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if api.remoteWriteHandler != nil {
|
|
|
|
api.remoteWriteHandler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "remote write receiver needs to be enabled with --enable-feature=remote-write-receiver", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) deleteSeries(r *http.Request) apiFuncResult {
|
2017-12-02 21:07:05 -08:00
|
|
|
if !api.enableAdmin {
|
2019-01-04 05:47:38 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorUnavailable, errors.New("admin APIs disabled")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2018-08-17 08:24:35 -07:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.Wrap(err, "error parsing form values")}, nil, nil}
|
2018-08-17 08:24:35 -07:00
|
|
|
}
|
2017-12-02 21:07:05 -08:00
|
|
|
if len(r.Form["match[]"]) == 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorBadData, errors.New("no match[] parameter provided")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2020-03-06 02:33:01 -08:00
|
|
|
start, err := parseTimeParam(r, "start", minTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "start")
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2020-03-06 02:33:01 -08:00
|
|
|
end, err := parseTimeParam(r, "end", maxTime)
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "end")
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range r.Form["match[]"] {
|
2020-02-03 10:06:39 -08:00
|
|
|
matchers, err := parser.ParseMetricSelector(s)
|
2017-12-02 21:07:05 -08:00
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(err, "match[]")
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
if err := api.db.Delete(timestamp.FromTime(start), timestamp.FromTime(end), matchers...); err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, nil, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) snapshot(r *http.Request) apiFuncResult {
|
2017-12-02 21:07:05 -08:00
|
|
|
if !api.enableAdmin {
|
2019-01-04 05:47:38 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorUnavailable, errors.New("admin APIs disabled")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2018-10-02 03:48:07 -07:00
|
|
|
var (
|
|
|
|
skipHead bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if r.FormValue("skip_head") != "" {
|
|
|
|
skipHead, err = strconv.ParseBool(r.FormValue("skip_head"))
|
|
|
|
if err != nil {
|
2020-12-23 16:27:13 -08:00
|
|
|
return invalidParamError(errors.Wrapf(err, "unable to parse boolean"), "skip_head")
|
2018-10-02 03:48:07 -07:00
|
|
|
}
|
2018-08-17 08:24:35 -07:00
|
|
|
}
|
2018-03-08 02:43:41 -08:00
|
|
|
|
2017-12-02 21:07:05 -08:00
|
|
|
var (
|
2020-04-29 09:16:14 -07:00
|
|
|
snapdir = filepath.Join(api.dbDir, "snapshots")
|
2020-11-01 12:07:38 -08:00
|
|
|
name = fmt.Sprintf("%s-%016x",
|
2017-12-10 11:19:34 -08:00
|
|
|
time.Now().UTC().Format("20060102T150405Z0700"),
|
2020-11-01 12:07:38 -08:00
|
|
|
rand.Int63())
|
2017-12-10 11:19:34 -08:00
|
|
|
dir = filepath.Join(snapdir, name)
|
2017-12-02 21:07:05 -08:00
|
|
|
)
|
2021-10-22 01:06:44 -07:00
|
|
|
if err := os.MkdirAll(dir, 0o777); err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, errors.Wrap(err, "create snapshot directory")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
if err := api.db.Snapshot(dir, !skipHead); err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, errors.Wrap(err, "create snapshot")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{struct {
|
2017-12-02 21:07:05 -08:00
|
|
|
Name string `json:"name"`
|
2018-11-30 06:27:12 -08:00
|
|
|
}{name}, nil, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) cleanTombstones(r *http.Request) apiFuncResult {
|
2017-12-02 21:07:05 -08:00
|
|
|
if !api.enableAdmin {
|
2019-01-04 05:47:38 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorUnavailable, errors.New("admin APIs disabled")}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
2020-04-29 09:16:14 -07:00
|
|
|
if err := api.db.CleanTombstones(); err != nil {
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, &apiError{errorInternal, err}, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
return apiFuncResult{nil, nil, nil, nil}
|
2017-12-02 21:07:05 -08:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:27:12 -08:00
|
|
|
func (api *API) respond(w http.ResponseWriter, data interface{}, warnings storage.Warnings) {
|
|
|
|
statusMessage := statusSuccess
|
|
|
|
var warningStrings []string
|
|
|
|
for _, warning := range warnings {
|
|
|
|
warningStrings = append(warningStrings, warning.Error())
|
|
|
|
}
|
2017-12-02 05:52:59 -08:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2015-06-04 09:07:57 -07:00
|
|
|
b, err := json.Marshal(&response{
|
2018-11-30 06:27:12 -08:00
|
|
|
Status: statusMessage,
|
|
|
|
Data: data,
|
|
|
|
Warnings: warningStrings,
|
2015-06-04 09:07:57 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-11-27 08:44:29 -08:00
|
|
|
level.Error(api.logger).Log("msg", "error marshaling json response", "err", err)
|
2018-07-13 11:31:23 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2015-06-04 09:07:57 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-07-13 11:31:23 -07:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-07-06 10:44:45 -07:00
|
|
|
if n, err := w.Write(b); err != nil {
|
2018-07-25 05:35:47 -07:00
|
|
|
level.Error(api.logger).Log("msg", "error writing response", "bytesWritten", n, "err", err)
|
2018-07-06 10:44:45 -07:00
|
|
|
}
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 10:44:45 -07:00
|
|
|
func (api *API) respondError(w http.ResponseWriter, apiErr *apiError, data interface{}) {
|
2018-07-25 05:17:10 -07:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
b, err := json.Marshal(&response{
|
|
|
|
Status: statusError,
|
|
|
|
ErrorType: apiErr.typ,
|
|
|
|
Error: apiErr.err.Error(),
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-11-27 08:44:29 -08:00
|
|
|
level.Error(api.logger).Log("msg", "error marshaling json response", "err", err)
|
2018-07-25 05:17:10 -07:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-11-11 14:00:54 -08:00
|
|
|
|
|
|
|
var code int
|
|
|
|
switch apiErr.typ {
|
|
|
|
case errorBadData:
|
|
|
|
code = http.StatusBadRequest
|
|
|
|
case errorExec:
|
2020-08-28 03:18:31 -07:00
|
|
|
code = http.StatusUnprocessableEntity
|
2015-11-11 14:00:54 -08:00
|
|
|
case errorCanceled, errorTimeout:
|
|
|
|
code = http.StatusServiceUnavailable
|
2017-04-04 09:22:51 -07:00
|
|
|
case errorInternal:
|
|
|
|
code = http.StatusInternalServerError
|
2018-05-18 00:32:11 -07:00
|
|
|
case errorNotFound:
|
|
|
|
code = http.StatusNotFound
|
2015-11-11 14:00:54 -08:00
|
|
|
default:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
}
|
2015-06-04 09:07:57 -07:00
|
|
|
|
2018-07-25 05:17:10 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
2018-07-06 10:44:45 -07:00
|
|
|
if n, err := w.Write(b); err != nil {
|
2018-07-25 05:35:47 -07:00
|
|
|
level.Error(api.logger).Log("msg", "error writing response", "bytesWritten", n, "err", err)
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 02:33:01 -08:00
|
|
|
func parseTimeParam(r *http.Request, paramName string, defaultValue time.Time) (time.Time, error) {
|
|
|
|
val := r.FormValue(paramName)
|
|
|
|
if val == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
result, err := parseTime(val)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errors.Wrapf(err, "Invalid time value for '%s'", paramName)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-12-24 15:37:46 -08:00
|
|
|
func parseTime(s string) (time.Time, error) {
|
2015-06-04 09:07:57 -07:00
|
|
|
if t, err := strconv.ParseFloat(s, 64); err == nil {
|
2016-12-24 15:37:46 -08:00
|
|
|
s, ns := math.Modf(t)
|
2018-12-03 04:25:54 -08:00
|
|
|
ns = math.Round(ns*1000) / 1000
|
2020-03-29 09:35:39 -07:00
|
|
|
return time.Unix(int64(s), int64(ns*float64(time.Second))).UTC(), nil
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
|
2016-12-24 15:37:46 -08:00
|
|
|
return t, nil
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2019-07-08 02:43:59 -07:00
|
|
|
|
|
|
|
// Stdlib's time parser can only handle 4 digit years. As a workaround until
|
|
|
|
// that is fixed we want to at least support our own boundary times.
|
|
|
|
// Context: https://github.com/prometheus/client_golang/issues/614
|
|
|
|
// Upstream issue: https://github.com/golang/go/issues/20555
|
|
|
|
switch s {
|
|
|
|
case minTimeFormatted:
|
|
|
|
return minTime, nil
|
|
|
|
case maxTimeFormatted:
|
|
|
|
return maxTime, nil
|
|
|
|
}
|
2019-03-25 16:01:12 -07:00
|
|
|
return time.Time{}, errors.Errorf("cannot parse %q to a valid timestamp", s)
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseDuration(s string) (time.Duration, error) {
|
|
|
|
if d, err := strconv.ParseFloat(s, 64); err == nil {
|
2017-03-16 07:16:20 -07:00
|
|
|
ts := d * float64(time.Second)
|
|
|
|
if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return 0, errors.Errorf("cannot parse %q to a valid duration. It overflows int64", s)
|
2017-03-16 07:16:20 -07:00
|
|
|
}
|
|
|
|
return time.Duration(ts), nil
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2016-01-29 06:23:11 -08:00
|
|
|
if d, err := model.ParseDuration(s); err == nil {
|
|
|
|
return time.Duration(d), nil
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2019-03-25 16:01:12 -07:00
|
|
|
return 0, errors.Errorf("cannot parse %q to a valid duration", s)
|
2015-06-04 09:07:57 -07:00
|
|
|
}
|
2018-02-07 04:27:57 -08:00
|
|
|
|
2020-12-22 03:02:19 -08:00
|
|
|
func parseMatchersParam(matchers []string) ([][]*labels.Matcher, error) {
|
|
|
|
var matcherSets [][]*labels.Matcher
|
|
|
|
for _, s := range matchers {
|
|
|
|
matchers, err := parser.ParseMetricSelector(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
matcherSets = append(matcherSets, matchers)
|
|
|
|
}
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for _, ms := range matcherSets {
|
|
|
|
for _, lm := range ms {
|
|
|
|
if lm != nil && !lm.Matches("") {
|
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("match[] must contain at least one non-empty matcher")
|
|
|
|
}
|
|
|
|
return matcherSets, nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 02:47:45 -07:00
|
|
|
// marshalPointJSON writes `[ts, "val"]`.
|
2018-02-07 04:27:57 -08:00
|
|
|
func marshalPointJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
p := *((*promql.Point)(ptr))
|
|
|
|
stream.WriteArrayStart()
|
2021-03-16 02:47:45 -07:00
|
|
|
marshalTimestamp(p.T, stream)
|
|
|
|
stream.WriteMore()
|
|
|
|
marshalValue(p.V, stream)
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalPointJSONIsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshalExemplarJSON writes.
|
|
|
|
// {
|
|
|
|
// labels: <labels>,
|
|
|
|
// value: "<string>",
|
|
|
|
// timestamp: <float>
|
|
|
|
// }
|
|
|
|
func marshalExemplarJSON(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
|
|
|
p := *((*exemplar.Exemplar)(ptr))
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
|
|
|
|
// "labels" key.
|
|
|
|
stream.WriteObjectField(`labels`)
|
|
|
|
lbls, err := p.Labels.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
stream.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stream.SetBuffer(append(stream.Buffer(), lbls...))
|
|
|
|
|
|
|
|
// "value" key.
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField(`value`)
|
|
|
|
marshalValue(p.Value, stream)
|
|
|
|
|
|
|
|
// "timestamp" key.
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField(`timestamp`)
|
|
|
|
marshalTimestamp(p.Ts, stream)
|
2021-10-22 01:06:44 -07:00
|
|
|
// marshalTimestamp(p.Ts, stream)
|
2021-03-16 02:47:45 -07:00
|
|
|
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalExemplarJSONEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalTimestamp(t int64, stream *jsoniter.Stream) {
|
2018-02-07 04:27:57 -08:00
|
|
|
// Write out the timestamp as a float divided by 1000.
|
|
|
|
// This is ~3x faster than converting to a float.
|
|
|
|
if t < 0 {
|
|
|
|
stream.WriteRaw(`-`)
|
|
|
|
t = -t
|
|
|
|
}
|
|
|
|
stream.WriteInt64(t / 1000)
|
|
|
|
fraction := t % 1000
|
|
|
|
if fraction != 0 {
|
|
|
|
stream.WriteRaw(`.`)
|
|
|
|
if fraction < 100 {
|
|
|
|
stream.WriteRaw(`0`)
|
|
|
|
}
|
|
|
|
if fraction < 10 {
|
|
|
|
stream.WriteRaw(`0`)
|
|
|
|
}
|
|
|
|
stream.WriteInt64(fraction)
|
|
|
|
}
|
2021-03-16 02:47:45 -07:00
|
|
|
}
|
2019-09-17 06:52:26 -07:00
|
|
|
|
2021-03-16 02:47:45 -07:00
|
|
|
func marshalValue(v float64, stream *jsoniter.Stream) {
|
|
|
|
stream.WriteRaw(`"`)
|
2019-09-17 06:52:26 -07:00
|
|
|
// Taken from https://github.com/json-iterator/go/blob/master/stream_float.go#L71 as a workaround
|
|
|
|
// to https://github.com/json-iterator/go/issues/365 (jsoniter, to follow json standard, doesn't allow inf/nan).
|
|
|
|
buf := stream.Buffer()
|
2021-03-16 02:47:45 -07:00
|
|
|
abs := math.Abs(v)
|
2019-09-17 06:52:26 -07:00
|
|
|
fmt := byte('f')
|
|
|
|
// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
|
|
|
|
if abs != 0 {
|
|
|
|
if abs < 1e-6 || abs >= 1e21 {
|
|
|
|
fmt = 'e'
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 02:47:45 -07:00
|
|
|
buf = strconv.AppendFloat(buf, v, fmt, -1, 64)
|
2019-09-17 06:52:26 -07:00
|
|
|
stream.SetBuffer(buf)
|
2018-02-07 04:27:57 -08:00
|
|
|
stream.WriteRaw(`"`)
|
|
|
|
}
|