2020-03-13 10:19:31 +00:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-09-21 12:53:49 +00:00
|
|
|
"time"
|
2020-03-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Data struct {
|
|
|
|
ResultType string `json:"resultType"`
|
|
|
|
Result []struct {
|
|
|
|
Labels map[string]string `json:"metric"`
|
|
|
|
TV [2]interface{} `json:"value"`
|
|
|
|
} `json:"result"`
|
|
|
|
} `json:"data"`
|
|
|
|
ErrorType string `json:"errorType"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r response) metrics() ([]Metric, error) {
|
|
|
|
var ms []Metric
|
|
|
|
var m Metric
|
|
|
|
var f float64
|
|
|
|
var err error
|
|
|
|
for i, res := range r.Data.Result {
|
|
|
|
f, err = strconv.ParseFloat(res.TV[1].(string), 64)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("metric %v, unable to parse float64 from %s: %w", res, res.TV[1], err)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
m.Labels = nil
|
|
|
|
for k, v := range r.Data.Result[i].Labels {
|
2020-11-09 22:27:32 +00:00
|
|
|
m.AddLabel(k, v)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
m.Timestamp = int64(res.TV[0].(float64))
|
|
|
|
m.Value = f
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VMStorage represents vmstorage entity with ability to read and write metrics
|
|
|
|
type VMStorage struct {
|
2020-06-29 19:21:03 +00:00
|
|
|
c *http.Client
|
|
|
|
queryURL string
|
|
|
|
basicAuthUser string
|
|
|
|
basicAuthPass string
|
2020-09-21 12:53:49 +00:00
|
|
|
lookBack time.Duration
|
2021-01-26 08:12:04 +00:00
|
|
|
queryStep time.Duration
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 12:53:49 +00:00
|
|
|
const queryPath = "/api/v1/query?query="
|
|
|
|
|
2020-03-13 10:19:31 +00:00
|
|
|
// NewVMStorage is a constructor for VMStorage
|
2021-01-26 08:12:04 +00:00
|
|
|
func NewVMStorage(baseURL, basicAuthUser, basicAuthPass string, lookBack time.Duration, queryStep time.Duration, c *http.Client) *VMStorage {
|
2020-03-13 10:19:31 +00:00
|
|
|
return &VMStorage{
|
|
|
|
c: c,
|
|
|
|
basicAuthUser: basicAuthUser,
|
|
|
|
basicAuthPass: basicAuthPass,
|
|
|
|
queryURL: strings.TrimSuffix(baseURL, "/") + queryPath,
|
2020-09-21 12:53:49 +00:00
|
|
|
lookBack: lookBack,
|
2021-01-26 08:12:04 +00:00
|
|
|
queryStep: queryStep,
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query reads metrics from datasource by given query
|
|
|
|
func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) {
|
|
|
|
const (
|
|
|
|
statusSuccess, statusError, rtVector = "success", "error", "vector"
|
|
|
|
)
|
2020-09-21 12:53:49 +00:00
|
|
|
q := s.queryURL + url.QueryEscape(query)
|
|
|
|
if s.lookBack > 0 {
|
2020-09-21 12:55:56 +00:00
|
|
|
lookBack := time.Now().Add(-s.lookBack)
|
2020-09-21 12:53:49 +00:00
|
|
|
q += fmt.Sprintf("&time=%d", lookBack.Unix())
|
|
|
|
}
|
2021-01-26 08:12:04 +00:00
|
|
|
if s.queryStep > 0 {
|
|
|
|
q += fmt.Sprintf("&step=%s", s.queryStep.String())
|
|
|
|
}
|
2020-09-21 12:53:49 +00:00
|
|
|
req, err := http.NewRequest("POST", q, nil)
|
2020-03-13 10:19:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-13 08:25:39 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
2020-03-13 10:19:31 +00:00
|
|
|
if s.basicAuthPass != "" {
|
|
|
|
req.SetBasicAuth(s.basicAuthUser, s.basicAuthPass)
|
|
|
|
}
|
|
|
|
resp, err := s.c.Do(req.WithContext(ctx))
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("error getting response from %s: %w", req.URL, err)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
2020-10-07 14:59:50 +00:00
|
|
|
return nil, fmt.Errorf("datasource returns unexpected response code %d for %s. Response body %s", resp.StatusCode, req.URL, body)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
r := &response{}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(r); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("error parsing metrics for %s: %w", req.URL, err)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
if r.Status == statusError {
|
|
|
|
return nil, fmt.Errorf("response error, query: %s, errorType: %s, error: %s", req.URL, r.ErrorType, r.Error)
|
|
|
|
}
|
|
|
|
if r.Status != statusSuccess {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("unknown status: %s, Expected success or error ", r.Status)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
if r.Data.ResultType != rtVector {
|
2020-10-07 14:59:50 +00:00
|
|
|
return nil, fmt.Errorf("unknown result type:%s. Expected vector", r.Data.ResultType)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
|
|
|
return r.metrics()
|
|
|
|
}
|