2017-04-20 00:24:14 +03:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
2015-08-19 16:33:59 +03:00
|
|
|
// 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.
|
|
|
|
|
2017-04-25 00:05:17 +03:00
|
|
|
// +build go1.7
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
// Package v1 provides bindings to the Prometheus HTTP API v1:
|
2015-08-19 16:33:59 +03:00
|
|
|
// http://prometheus.io/docs/querying/api/
|
2017-04-20 00:24:14 +03:00
|
|
|
package v1
|
2015-08-19 16:33:59 +03:00
|
|
|
|
|
|
|
import (
|
2017-04-20 15:57:46 +03:00
|
|
|
"context"
|
2015-08-19 16:33:59 +03:00
|
|
|
"encoding/json"
|
2019-01-16 00:34:51 +03:00
|
|
|
"errors"
|
2015-08-19 16:33:59 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
"github.com/prometheus/client_golang/api"
|
|
|
|
"github.com/prometheus/common/model"
|
2015-08-19 16:33:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusAPIError = 422
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
apiPrefix = "/api/v1"
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
epAlertManagers = apiPrefix + "/alertmanagers"
|
2018-04-06 02:47:48 +03:00
|
|
|
epQuery = apiPrefix + "/query"
|
|
|
|
epQueryRange = apiPrefix + "/query_range"
|
|
|
|
epLabelValues = apiPrefix + "/label/:name/values"
|
|
|
|
epSeries = apiPrefix + "/series"
|
2018-04-10 17:21:25 +03:00
|
|
|
epTargets = apiPrefix + "/targets"
|
2019-01-16 00:34:51 +03:00
|
|
|
epRules = apiPrefix + "/rules"
|
2018-04-06 02:47:48 +03:00
|
|
|
epSnapshot = apiPrefix + "/admin/tsdb/snapshot"
|
|
|
|
epDeleteSeries = apiPrefix + "/admin/tsdb/delete_series"
|
|
|
|
epCleanTombstones = apiPrefix + "/admin/tsdb/clean_tombstones"
|
2018-04-10 17:21:25 +03:00
|
|
|
epConfig = apiPrefix + "/status/config"
|
|
|
|
epFlags = apiPrefix + "/status/flags"
|
2015-08-19 16:33:59 +03:00
|
|
|
)
|
|
|
|
|
2019-01-16 00:34:51 +03:00
|
|
|
// AlertState models the state of an alert.
|
|
|
|
type AlertState string
|
|
|
|
|
2016-09-16 20:59:04 +03:00
|
|
|
// ErrorType models the different API error types.
|
2015-08-19 16:33:59 +03:00
|
|
|
type ErrorType string
|
|
|
|
|
2018-04-11 17:38:10 +03:00
|
|
|
// HealthStatus models the health status of a scrape target.
|
|
|
|
type HealthStatus string
|
|
|
|
|
2019-01-16 00:34:51 +03:00
|
|
|
// RuleType models the type of a rule.
|
|
|
|
type RuleType string
|
|
|
|
|
|
|
|
// RuleHealth models the health status of a rule.
|
|
|
|
type RuleHealth string
|
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
const (
|
2019-01-16 00:34:51 +03:00
|
|
|
// Possible values for AlertState.
|
|
|
|
AlertStateFiring AlertState = "firing"
|
|
|
|
AlertStateInactive AlertState = "inactive"
|
|
|
|
AlertStatePending AlertState = "pending"
|
|
|
|
|
2018-04-11 17:38:10 +03:00
|
|
|
// Possible values for ErrorType.
|
2015-08-19 16:33:59 +03:00
|
|
|
ErrBadData ErrorType = "bad_data"
|
2018-11-15 19:51:14 +03:00
|
|
|
ErrTimeout ErrorType = "timeout"
|
|
|
|
ErrCanceled ErrorType = "canceled"
|
|
|
|
ErrExec ErrorType = "execution"
|
|
|
|
ErrBadResponse ErrorType = "bad_response"
|
|
|
|
ErrServer ErrorType = "server_error"
|
|
|
|
ErrClient ErrorType = "client_error"
|
2018-04-11 17:38:10 +03:00
|
|
|
|
|
|
|
// Possible values for HealthStatus.
|
|
|
|
HealthGood HealthStatus = "up"
|
|
|
|
HealthUnknown HealthStatus = "unknown"
|
|
|
|
HealthBad HealthStatus = "down"
|
2019-01-16 00:34:51 +03:00
|
|
|
|
|
|
|
// Possible values for RuleType.
|
|
|
|
RuleTypeRecording RuleType = "recording"
|
|
|
|
RuleTypeAlerting RuleType = "alerting"
|
|
|
|
|
|
|
|
// Possible values for RuleHealth.
|
|
|
|
RuleHealthGood = "ok"
|
|
|
|
RuleHealthUnknown = "unknown"
|
|
|
|
RuleHealthBad = "err"
|
2015-08-19 16:33:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error is an error returned by the API.
|
|
|
|
type Error struct {
|
2018-10-25 20:44:21 +03:00
|
|
|
Type ErrorType
|
|
|
|
Msg string
|
|
|
|
Detail string
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Error) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.Type, e.Msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range represents a sliced time range.
|
|
|
|
type Range struct {
|
|
|
|
// The boundaries of the time range.
|
|
|
|
Start, End time.Time
|
|
|
|
// The maximum time between two slices within the boundaries.
|
|
|
|
Step time.Duration
|
|
|
|
}
|
|
|
|
|
2017-04-26 00:15:57 +03:00
|
|
|
// API provides bindings for Prometheus's v1 API.
|
2017-04-20 00:24:14 +03:00
|
|
|
type API interface {
|
2018-04-10 17:21:25 +03:00
|
|
|
// AlertManagers returns an overview of the current state of the Prometheus alert manager discovery.
|
|
|
|
AlertManagers(ctx context.Context) (AlertManagersResult, error)
|
|
|
|
// CleanTombstones removes the deleted data from disk and cleans up the existing tombstones.
|
|
|
|
CleanTombstones(ctx context.Context) error
|
|
|
|
// Config returns the current Prometheus configuration.
|
|
|
|
Config(ctx context.Context) (ConfigResult, error)
|
|
|
|
// DeleteSeries deletes data for a selection of series in a time range.
|
|
|
|
DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
|
|
|
|
// Flags returns the flag values that Prometheus was launched with.
|
|
|
|
Flags(ctx context.Context) (FlagsResult, error)
|
|
|
|
// LabelValues performs a query for the values of the given label.
|
|
|
|
LabelValues(ctx context.Context, label string) (model.LabelValues, error)
|
2017-04-20 00:24:14 +03:00
|
|
|
// Query performs a query for the given time.
|
|
|
|
Query(ctx context.Context, query string, ts time.Time) (model.Value, error)
|
|
|
|
// QueryRange performs a query for the given range.
|
|
|
|
QueryRange(ctx context.Context, query string, r Range) (model.Value, error)
|
2017-11-24 15:12:53 +03:00
|
|
|
// Series finds series by label matchers.
|
|
|
|
Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error)
|
2018-04-06 02:47:48 +03:00
|
|
|
// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
|
|
|
|
// under the TSDB's data directory and returns the directory as response.
|
|
|
|
Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
|
2019-01-16 00:34:51 +03:00
|
|
|
// Rules returns a list of alerting and recording rules that are currently loaded.
|
|
|
|
Rules(ctx context.Context) (RulesResult, error)
|
2018-04-10 17:21:25 +03:00
|
|
|
// Targets returns an overview of the current state of the Prometheus target discovery.
|
|
|
|
Targets(ctx context.Context) (TargetsResult, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlertManagersResult contains the result from querying the alertmanagers endpoint.
|
|
|
|
type AlertManagersResult struct {
|
|
|
|
Active []AlertManager `json:"activeAlertManagers"`
|
|
|
|
Dropped []AlertManager `json:"droppedAlertManagers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlertManager models a configured Alert Manager.
|
|
|
|
type AlertManager struct {
|
2018-04-11 17:38:10 +03:00
|
|
|
URL string `json:"url"`
|
2018-04-10 17:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigResult contains the result from querying the config endpoint.
|
|
|
|
type ConfigResult struct {
|
|
|
|
YAML string `json:"yaml"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagsResult contains the result from querying the flag endpoint.
|
2018-04-11 17:38:10 +03:00
|
|
|
type FlagsResult map[string]string
|
2018-04-10 17:21:25 +03:00
|
|
|
|
|
|
|
// SnapshotResult contains the result from querying the snapshot endpoint.
|
|
|
|
type SnapshotResult struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:34:51 +03:00
|
|
|
// RulesResult contains the result from querying the rules endpoint.
|
|
|
|
type RulesResult struct {
|
|
|
|
Groups []RuleGroup `json:"groups"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleGroup models a rule group that contains a set of recording and alerting rules.
|
|
|
|
type RuleGroup struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
File string `json:"file"`
|
|
|
|
Interval float64 `json:"interval"`
|
|
|
|
Rules Rules `json:"rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recording and alerting rules are stored in the same slice to preserve the order
|
|
|
|
// that rules are returned in by the API.
|
|
|
|
//
|
|
|
|
// Rule types can be determined using a type switch:
|
|
|
|
// switch v := rule.(type) {
|
|
|
|
// case RecordingRule:
|
|
|
|
// fmt.Print("got a recording rule")
|
|
|
|
// case AlertingRule:
|
|
|
|
// fmt.Print("got a alerting rule")
|
|
|
|
// default:
|
|
|
|
// fmt.Printf("unknown rule type %s", v)
|
|
|
|
// }
|
|
|
|
type Rules []interface{}
|
|
|
|
|
|
|
|
// AlertingRule models a alerting rule.
|
|
|
|
type AlertingRule struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Duration float64 `json:"duration"`
|
|
|
|
Labels model.LabelSet `json:"labels"`
|
|
|
|
Annotations model.LabelSet `json:"annotations"`
|
|
|
|
Alerts []*Alert `json:"alerts"`
|
|
|
|
Health RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordingRule models a recording rule.
|
|
|
|
type RecordingRule struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Labels model.LabelSet `json:"labels,omitempty"`
|
|
|
|
Health RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alert models an active alert.
|
|
|
|
type Alert struct {
|
|
|
|
ActiveAt time.Time `json:"activeAt"`
|
|
|
|
Annotations model.LabelSet
|
|
|
|
Labels model.LabelSet
|
|
|
|
State AlertState
|
|
|
|
Value float64
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
// TargetsResult contains the result from querying the targets endpoint.
|
|
|
|
type TargetsResult struct {
|
|
|
|
Active []ActiveTarget `json:"activeTargets"`
|
|
|
|
Dropped []DroppedTarget `json:"droppedTargets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveTarget models an active Prometheus scrape target.
|
|
|
|
type ActiveTarget struct {
|
2019-01-23 18:39:45 +03:00
|
|
|
DiscoveredLabels map[string]string `json:"discoveredLabels"`
|
|
|
|
Labels model.LabelSet `json:"labels"`
|
|
|
|
ScrapeURL string `json:"scrapeUrl"`
|
|
|
|
LastError string `json:"lastError"`
|
|
|
|
LastScrape time.Time `json:"lastScrape"`
|
|
|
|
Health HealthStatus `json:"health"`
|
2018-04-10 17:21:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DroppedTarget models a dropped Prometheus scrape target.
|
|
|
|
type DroppedTarget struct {
|
2019-01-23 18:39:45 +03:00
|
|
|
DiscoveredLabels map[string]string `json:"discoveredLabels"`
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
// queryResult contains result data for a query.
|
|
|
|
type queryResult struct {
|
|
|
|
Type model.ValueType `json:"resultType"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
|
|
|
|
// The decoded value.
|
|
|
|
v model.Value
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:34:51 +03:00
|
|
|
func (rg *RuleGroup) UnmarshalJSON(b []byte) error {
|
|
|
|
v := struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
File string `json:"file"`
|
|
|
|
Interval float64 `json:"interval"`
|
|
|
|
Rules []json.RawMessage `json:"rules"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rg.Name = v.Name
|
|
|
|
rg.File = v.File
|
|
|
|
rg.Interval = v.Interval
|
|
|
|
|
|
|
|
for _, rule := range v.Rules {
|
|
|
|
alertingRule := AlertingRule{}
|
|
|
|
if err := json.Unmarshal(rule, &alertingRule); err == nil {
|
|
|
|
rg.Rules = append(rg.Rules, alertingRule)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
recordingRule := RecordingRule{}
|
|
|
|
if err := json.Unmarshal(rule, &recordingRule); err == nil {
|
|
|
|
rg.Rules = append(rg.Rules, recordingRule)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return errors.New("failed to decode JSON into an alerting or recording rule")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AlertingRule) UnmarshalJSON(b []byte) error {
|
|
|
|
v := struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.Type == "" {
|
|
|
|
return errors.New("type field not present in rule")
|
|
|
|
}
|
|
|
|
if v.Type != string(RuleTypeAlerting) {
|
|
|
|
return fmt.Errorf("expected rule of type %s but got %s", string(RuleTypeAlerting), v.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Duration float64 `json:"duration"`
|
|
|
|
Labels model.LabelSet `json:"labels"`
|
|
|
|
Annotations model.LabelSet `json:"annotations"`
|
|
|
|
Alerts []*Alert `json:"alerts"`
|
|
|
|
Health RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &rule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Health = rule.Health
|
|
|
|
r.Annotations = rule.Annotations
|
|
|
|
r.Name = rule.Name
|
|
|
|
r.Query = rule.Query
|
|
|
|
r.Alerts = rule.Alerts
|
|
|
|
r.Duration = rule.Duration
|
|
|
|
r.Labels = rule.Labels
|
|
|
|
r.LastError = rule.LastError
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RecordingRule) UnmarshalJSON(b []byte) error {
|
|
|
|
v := struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.Type == "" {
|
|
|
|
return errors.New("type field not present in rule")
|
|
|
|
}
|
|
|
|
if v.Type != string(RuleTypeRecording) {
|
|
|
|
return fmt.Errorf("expected rule of type %s but got %s", string(RuleTypeRecording), v.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Labels model.LabelSet `json:"labels,omitempty"`
|
|
|
|
Health RuleHealth `json:"health"`
|
|
|
|
LastError string `json:"lastError,omitempty"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &rule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Health = rule.Health
|
|
|
|
r.Labels = rule.Labels
|
|
|
|
r.Name = rule.Name
|
|
|
|
r.LastError = rule.LastError
|
|
|
|
r.Query = rule.Query
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
func (qr *queryResult) UnmarshalJSON(b []byte) error {
|
|
|
|
v := struct {
|
|
|
|
Type model.ValueType `json:"resultType"`
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(b, &v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v.Type {
|
|
|
|
case model.ValScalar:
|
|
|
|
var sv model.Scalar
|
|
|
|
err = json.Unmarshal(v.Result, &sv)
|
|
|
|
qr.v = &sv
|
|
|
|
|
|
|
|
case model.ValVector:
|
|
|
|
var vv model.Vector
|
|
|
|
err = json.Unmarshal(v.Result, &vv)
|
|
|
|
qr.v = vv
|
|
|
|
|
|
|
|
case model.ValMatrix:
|
|
|
|
var mv model.Matrix
|
|
|
|
err = json.Unmarshal(v.Result, &mv)
|
|
|
|
qr.v = mv
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unexpected value type %q", v.Type)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
// NewAPI returns a new API for the client.
|
2016-08-01 01:11:57 +03:00
|
|
|
//
|
2017-04-20 00:24:14 +03:00
|
|
|
// It is safe to use the returned API from multiple goroutines.
|
|
|
|
func NewAPI(c api.Client) API {
|
|
|
|
return &httpAPI{client: apiClient{c}}
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
type httpAPI struct {
|
|
|
|
client api.Client
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error) {
|
|
|
|
u := h.client.URL(epAlertManagers, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return AlertManagersResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return AlertManagersResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res AlertManagersResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpAPI) CleanTombstones(ctx context.Context) error {
|
|
|
|
u := h.client.URL(epCleanTombstones, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = h.client.Do(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
|
|
|
|
u := h.client.URL(epConfig, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return ConfigResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return ConfigResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res ConfigResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error {
|
|
|
|
u := h.client.URL(epDeleteSeries, nil)
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
for _, m := range matches {
|
|
|
|
q.Add("match[]", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
q.Set("start", startTime.Format(time.RFC3339Nano))
|
|
|
|
q.Set("end", endTime.Format(time.RFC3339Nano))
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = h.client.Do(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
|
|
|
|
u := h.client.URL(epFlags, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return FlagsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return FlagsResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res FlagsResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelValues, error) {
|
|
|
|
u := h.client.URL(epLabelValues, map[string]string{"name": label})
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var labelValues model.LabelValues
|
|
|
|
err = json.Unmarshal(body, &labelValues)
|
|
|
|
return labelValues, err
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, error) {
|
|
|
|
u := h.client.URL(epQuery, nil)
|
2015-08-19 16:33:59 +03:00
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
q.Set("query", query)
|
2017-11-17 00:39:52 +03:00
|
|
|
if !ts.IsZero() {
|
|
|
|
q.Set("time", ts.Format(time.RFC3339Nano))
|
|
|
|
}
|
2015-08-19 16:33:59 +03:00
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
2018-02-22 22:15:29 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2017-04-01 03:05:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-19 16:33:59 +03:00
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
_, body, err := h.client.Do(ctx, req)
|
2015-08-19 16:33:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var qres queryResult
|
|
|
|
err = json.Unmarshal(body, &qres)
|
|
|
|
|
|
|
|
return model.Value(qres.v), err
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.Value, error) {
|
|
|
|
u := h.client.URL(epQueryRange, nil)
|
2015-08-19 16:33:59 +03:00
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
var (
|
|
|
|
start = r.Start.Format(time.RFC3339Nano)
|
|
|
|
end = r.End.Format(time.RFC3339Nano)
|
|
|
|
step = strconv.FormatFloat(r.Step.Seconds(), 'f', 3, 64)
|
|
|
|
)
|
|
|
|
|
|
|
|
q.Set("query", query)
|
|
|
|
q.Set("start", start)
|
|
|
|
q.Set("end", end)
|
|
|
|
q.Set("step", step)
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
2018-02-22 22:15:29 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2017-04-01 03:05:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-19 16:33:59 +03:00
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
_, body, err := h.client.Do(ctx, req)
|
2015-08-19 16:33:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var qres queryResult
|
|
|
|
err = json.Unmarshal(body, &qres)
|
|
|
|
|
|
|
|
return model.Value(qres.v), err
|
|
|
|
}
|
2017-04-01 01:42:19 +03:00
|
|
|
|
2017-11-24 15:12:53 +03:00
|
|
|
func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, error) {
|
|
|
|
u := h.client.URL(epSeries, nil)
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
for _, m := range matches {
|
|
|
|
q.Add("match[]", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
q.Set("start", startTime.Format(time.RFC3339Nano))
|
|
|
|
q.Set("end", endTime.Format(time.RFC3339Nano))
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
2018-02-22 22:15:29 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2017-11-24 15:12:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mset []model.LabelSet
|
|
|
|
err = json.Unmarshal(body, &mset)
|
|
|
|
return mset, err
|
|
|
|
}
|
|
|
|
|
2018-04-06 02:47:48 +03:00
|
|
|
func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error) {
|
|
|
|
u := h.client.URL(epSnapshot, nil)
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
q.Set("skip_head", strconv.FormatBool(skipHead))
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return SnapshotResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res SnapshotResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:34:51 +03:00
|
|
|
func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
|
|
|
|
u := h.client.URL(epRules, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return RulesResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, body, err := h.client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return RulesResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res RulesResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
|
|
|
|
u := h.client.URL(epTargets, nil)
|
2018-04-06 02:47:48 +03:00
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
2018-04-06 02:47:48 +03:00
|
|
|
if err != nil {
|
2018-04-10 17:21:25 +03:00
|
|
|
return TargetsResult{}, err
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
_, body, err := h.client.Do(ctx, req)
|
2018-04-06 02:47:48 +03:00
|
|
|
if err != nil {
|
2018-04-10 17:21:25 +03:00
|
|
|
return TargetsResult{}, err
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
var res TargetsResult
|
|
|
|
err = json.Unmarshal(body, &res)
|
|
|
|
return res, err
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
// apiClient wraps a regular client and processes successful API responses.
|
|
|
|
// Successful also includes responses that errored at the API level.
|
|
|
|
type apiClient struct {
|
|
|
|
api.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Data json.RawMessage `json:"data"`
|
|
|
|
ErrorType ErrorType `json:"errorType"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2018-05-31 17:15:36 +03:00
|
|
|
func apiError(code int) bool {
|
|
|
|
// These are the codes that Prometheus sends when it returns an error.
|
|
|
|
return code == statusAPIError || code == http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
2018-10-25 20:44:21 +03:00
|
|
|
func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
|
|
|
|
switch resp.StatusCode / 100 {
|
|
|
|
case 4:
|
|
|
|
return ErrClient, fmt.Sprintf("client error: %d", resp.StatusCode)
|
|
|
|
case 5:
|
|
|
|
return ErrServer, fmt.Sprintf("server error: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
|
|
|
resp, body, err := c.Client.Do(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return resp, body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
code := resp.StatusCode
|
|
|
|
|
2018-05-31 17:15:36 +03:00
|
|
|
if code/100 != 2 && !apiError(code) {
|
2018-10-25 20:44:21 +03:00
|
|
|
errorType, errorMsg := errorTypeAndMsgFor(resp)
|
2017-04-20 00:24:14 +03:00
|
|
|
return resp, body, &Error{
|
2018-10-25 20:44:21 +03:00
|
|
|
Type: errorType,
|
|
|
|
Msg: errorMsg,
|
|
|
|
Detail: string(body),
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result apiResponse
|
|
|
|
|
2018-10-10 17:20:17 +03:00
|
|
|
if http.StatusNoContent != code {
|
|
|
|
if err = json.Unmarshal(body, &result); err != nil {
|
|
|
|
return resp, body, &Error{
|
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: err.Error(),
|
|
|
|
}
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 17:15:36 +03:00
|
|
|
if apiError(code) != (result.Status == "error") {
|
2017-04-20 00:24:14 +03:00
|
|
|
err = &Error{
|
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: "inconsistent body for response code",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 17:15:36 +03:00
|
|
|
if apiError(code) && result.Status == "error" {
|
2017-04-20 00:24:14 +03:00
|
|
|
err = &Error{
|
|
|
|
Type: result.ErrorType,
|
|
|
|
Msg: result.Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, []byte(result.Data), err
|
|
|
|
}
|