2021-06-09 11:20:38 +02:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-05-13 15:19:32 +02:00
|
|
|
"flag"
|
2021-06-09 11:20:38 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-05-13 15:19:32 +02:00
|
|
|
var (
|
|
|
|
disablePathAppend = flag.Bool("remoteRead.disablePathAppend", false, "Whether to disable automatic appending of '/api/v1/query' path "+
|
|
|
|
"to the configured -datasource.url and -remoteRead.url")
|
2023-07-07 07:44:34 +02:00
|
|
|
disableStepParam = flag.Bool("datasource.disableStepParam", false, "Whether to disable adding 'step' param to the issued instant queries. "+
|
|
|
|
"This might be useful when using vmalert with datasources that do not support 'step' param for instant queries, like Google Managed Prometheus. "+
|
|
|
|
"It is not recommended to enable this flag if you use vmalert with VictoriaMetrics.")
|
2022-05-13 15:19:32 +02:00
|
|
|
)
|
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
type promResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
ErrorType string `json:"errorType"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Data struct {
|
|
|
|
ResultType string `json:"resultType"`
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
} `json:"data"`
|
2023-05-08 09:36:39 +02:00
|
|
|
// Stats supported by VictoriaMetrics since v1.90
|
|
|
|
Stats struct {
|
|
|
|
SeriesFetched *string `json:"seriesFetched,omitempty"`
|
|
|
|
} `json:"stats,omitempty"`
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type promInstant struct {
|
|
|
|
Result []struct {
|
|
|
|
Labels map[string]string `json:"metric"`
|
|
|
|
TV [2]interface{} `json:"value"`
|
|
|
|
} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r promInstant) metrics() ([]Metric, error) {
|
2022-12-05 08:34:54 +01:00
|
|
|
result := make([]Metric, len(r.Result))
|
2021-06-09 11:20:38 +02:00
|
|
|
for i, res := range r.Result {
|
|
|
|
f, err := strconv.ParseFloat(res.TV[1].(string), 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("metric %v, unable to parse float64 from %s: %w", res, res.TV[1], err)
|
|
|
|
}
|
2021-06-11 10:22:05 +02:00
|
|
|
var m Metric
|
2022-12-05 08:34:54 +01:00
|
|
|
m.SetLabels(res.Labels)
|
2021-06-09 11:20:38 +02:00
|
|
|
m.Timestamps = append(m.Timestamps, int64(res.TV[0].(float64)))
|
|
|
|
m.Values = append(m.Values, f)
|
2022-12-05 08:34:54 +01:00
|
|
|
result[i] = m
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:50:46 +02:00
|
|
|
type promRange struct {
|
|
|
|
Result []struct {
|
|
|
|
Labels map[string]string `json:"metric"`
|
|
|
|
TVs [][2]interface{} `json:"values"`
|
|
|
|
} `json:"result"`
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
func (r promRange) metrics() ([]Metric, error) {
|
|
|
|
var result []Metric
|
|
|
|
for i, res := range r.Result {
|
|
|
|
var m Metric
|
|
|
|
for _, tv := range res.TVs {
|
|
|
|
f, err := strconv.ParseFloat(tv[1].(string), 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("metric %v, unable to parse float64 from %s: %w", res, tv[1], err)
|
|
|
|
}
|
|
|
|
m.Values = append(m.Values, f)
|
|
|
|
m.Timestamps = append(m.Timestamps, int64(tv[0].(float64)))
|
|
|
|
}
|
|
|
|
if len(m.Values) < 1 || len(m.Timestamps) < 1 {
|
|
|
|
return nil, fmt.Errorf("metric %v contains no values", res)
|
|
|
|
}
|
|
|
|
m.Labels = nil
|
|
|
|
for k, v := range r.Result[i].Labels {
|
|
|
|
m.AddLabel(k, v)
|
|
|
|
}
|
|
|
|
result = append(result, m)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:50:46 +02:00
|
|
|
type promScalar [2]interface{}
|
|
|
|
|
|
|
|
func (r promScalar) metrics() ([]Metric, error) {
|
|
|
|
var m Metric
|
|
|
|
f, err := strconv.ParseFloat(r[1].(string), 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("metric %v, unable to parse float64 from %s: %w", r, r[1], err)
|
|
|
|
}
|
|
|
|
m.Values = append(m.Values, f)
|
|
|
|
m.Timestamps = append(m.Timestamps, int64(r[0].(float64)))
|
|
|
|
return []Metric{m}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
const (
|
2022-05-18 09:50:46 +02:00
|
|
|
statusSuccess, statusError = "success", "error"
|
|
|
|
rtVector, rtMatrix, rScalar = "vector", "matrix", "scalar"
|
2021-06-09 11:20:38 +02:00
|
|
|
)
|
|
|
|
|
2023-05-08 09:36:39 +02:00
|
|
|
func parsePrometheusResponse(req *http.Request, resp *http.Response) (res Result, err error) {
|
2021-06-09 11:20:38 +02:00
|
|
|
r := &promResponse{}
|
2023-05-08 09:36:39 +02:00
|
|
|
if err = json.NewDecoder(resp.Body).Decode(r); err != nil {
|
|
|
|
return res, fmt.Errorf("error parsing prometheus metrics for %s: %w", req.URL.Redacted(), err)
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
|
|
|
if r.Status == statusError {
|
2023-05-08 09:36:39 +02:00
|
|
|
return res, fmt.Errorf("response error, query: %s, errorType: %s, error: %s", req.URL.Redacted(), r.ErrorType, r.Error)
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
|
|
|
if r.Status != statusSuccess {
|
2023-10-25 21:24:01 +02:00
|
|
|
return res, fmt.Errorf("unknown status: %s, Expected success or error", r.Status)
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
var parseFn func() ([]Metric, error)
|
2021-06-09 11:20:38 +02:00
|
|
|
switch r.Data.ResultType {
|
|
|
|
case rtVector:
|
|
|
|
var pi promInstant
|
|
|
|
if err := json.Unmarshal(r.Data.Result, &pi.Result); err != nil {
|
2023-10-25 21:24:01 +02:00
|
|
|
return res, fmt.Errorf("unmarshal err %w; \n %#v", err, string(r.Data.Result))
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
parseFn = pi.metrics
|
2021-06-09 11:20:38 +02:00
|
|
|
case rtMatrix:
|
|
|
|
var pr promRange
|
|
|
|
if err := json.Unmarshal(r.Data.Result, &pr.Result); err != nil {
|
2023-05-08 09:36:39 +02:00
|
|
|
return res, err
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
parseFn = pr.metrics
|
2022-05-18 09:50:46 +02:00
|
|
|
case rScalar:
|
|
|
|
var ps promScalar
|
|
|
|
if err := json.Unmarshal(r.Data.Result, &ps); err != nil {
|
2023-05-08 09:36:39 +02:00
|
|
|
return res, err
|
2022-05-18 09:50:46 +02:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
parseFn = ps.metrics
|
2021-06-09 11:20:38 +02:00
|
|
|
default:
|
2023-05-08 09:36:39 +02:00
|
|
|
return res, fmt.Errorf("unknown result type %q", r.Data.ResultType)
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2023-05-08 09:36:39 +02:00
|
|
|
|
|
|
|
ms, err := parseFn()
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
res = Result{Data: ms}
|
|
|
|
if r.Stats.SeriesFetched != nil {
|
|
|
|
intV, err := strconv.Atoi(*r.Stats.SeriesFetched)
|
|
|
|
if err != nil {
|
|
|
|
return res, fmt.Errorf("failed to convert stats.seriesFetched to int: %w", err)
|
|
|
|
}
|
|
|
|
res.SeriesFetched = &intV
|
|
|
|
}
|
|
|
|
return res, nil
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VMStorage) setPrometheusInstantReqParams(r *http.Request, query string, timestamp time.Time) {
|
|
|
|
if s.appendTypePrefix {
|
2022-05-13 15:19:32 +02:00
|
|
|
r.URL.Path += "/prometheus"
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2022-05-13 15:19:32 +02:00
|
|
|
if !*disablePathAppend {
|
|
|
|
r.URL.Path += "/api/v1/query"
|
2021-10-18 09:24:52 +02:00
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
q := r.URL.Query()
|
2023-10-10 12:41:19 +02:00
|
|
|
if s.lookBack > 0 {
|
|
|
|
timestamp = timestamp.Add(-s.lookBack)
|
|
|
|
}
|
2023-07-07 10:39:25 +02:00
|
|
|
q.Set("time", timestamp.Format(time.RFC3339))
|
2023-07-07 07:44:34 +02:00
|
|
|
if !*disableStepParam && s.evaluationInterval > 0 { // set step as evaluationInterval by default
|
2022-12-01 13:57:53 +01:00
|
|
|
// always convert to seconds to keep compatibility with older
|
|
|
|
// Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
|
|
|
|
q.Set("step", fmt.Sprintf("%ds", int(s.evaluationInterval.Seconds())))
|
|
|
|
}
|
2023-07-07 07:44:34 +02:00
|
|
|
if !*disableStepParam && s.queryStep > 0 { // override step with user-specified value
|
2022-12-01 13:57:53 +01:00
|
|
|
// always convert to seconds to keep compatibility with older
|
|
|
|
// Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
|
|
|
|
q.Set("step", fmt.Sprintf("%ds", int(s.queryStep.Seconds())))
|
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
s.setPrometheusReqParams(r, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VMStorage) setPrometheusRangeReqParams(r *http.Request, query string, start, end time.Time) {
|
|
|
|
if s.appendTypePrefix {
|
2022-05-13 15:19:32 +02:00
|
|
|
r.URL.Path += "/prometheus"
|
2021-06-09 11:20:38 +02:00
|
|
|
}
|
2022-05-13 15:19:32 +02:00
|
|
|
if !*disablePathAppend {
|
|
|
|
r.URL.Path += "/api/v1/query_range"
|
2021-10-18 09:24:52 +02:00
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
q := r.URL.Query()
|
2023-07-07 10:39:25 +02:00
|
|
|
q.Add("start", start.Format(time.RFC3339))
|
|
|
|
q.Add("end", end.Format(time.RFC3339))
|
2022-12-01 13:57:53 +01:00
|
|
|
if s.evaluationInterval > 0 { // set step as evaluationInterval by default
|
|
|
|
// always convert to seconds to keep compatibility with older
|
|
|
|
// Prometheus versions. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1943
|
|
|
|
q.Set("step", fmt.Sprintf("%ds", int(s.evaluationInterval.Seconds())))
|
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
s.setPrometheusReqParams(r, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *VMStorage) setPrometheusReqParams(r *http.Request, query string) {
|
|
|
|
q := r.URL.Query()
|
2021-12-02 13:45:08 +01:00
|
|
|
for k, vs := range s.extraParams {
|
|
|
|
if q.Has(k) { // extraParams are prior to params in URL
|
|
|
|
q.Del(k)
|
|
|
|
}
|
|
|
|
for _, v := range vs {
|
|
|
|
q.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 11:20:38 +02:00
|
|
|
q.Set("query", query)
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
}
|