2020-02-16 18:59:02 +00:00
|
|
|
package datasource
|
|
|
|
|
2021-02-01 13:02:44 +00:00
|
|
|
import (
|
2023-07-20 13:07:10 +00:00
|
|
|
"bytes"
|
2021-02-01 13:02:44 +00:00
|
|
|
"context"
|
2022-09-15 10:40:22 +00:00
|
|
|
"net/http"
|
2021-12-02 12:45:08 +00:00
|
|
|
"net/url"
|
2023-07-25 01:56:28 +00:00
|
|
|
"sort"
|
2023-07-20 13:07:10 +00:00
|
|
|
"strconv"
|
2021-06-09 09:20:38 +00:00
|
|
|
"time"
|
2021-02-01 13:02:44 +00:00
|
|
|
)
|
2020-04-06 11:44:03 +00:00
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
// Querier interface wraps Query and QueryRange methods
|
|
|
|
type Querier interface {
|
2022-09-15 10:40:22 +00:00
|
|
|
// Query executes instant request with the given query at the given ts.
|
|
|
|
// It returns list of Metric in response, the http.Request used for sending query
|
|
|
|
// and error if any. Returned http.Request can't be reused and its body is already read.
|
|
|
|
// Query should stop once ctx is cancelled.
|
2023-05-08 07:36:39 +00:00
|
|
|
Query(ctx context.Context, query string, ts time.Time) (Result, *http.Request, error)
|
2022-09-15 10:40:22 +00:00
|
|
|
// QueryRange executes range request with the given query on the given time range.
|
|
|
|
// It returns list of Metric in response and error if any.
|
|
|
|
// QueryRange should stop once ctx is cancelled.
|
2023-05-08 07:36:39 +00:00
|
|
|
QueryRange(ctx context.Context, query string, from, to time.Time) (Result, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result represents expected response from the datasource
|
|
|
|
type Result struct {
|
|
|
|
// Data contains list of received Metric
|
|
|
|
Data []Metric
|
|
|
|
// SeriesFetched contains amount of time series processed by datasource
|
|
|
|
// during query evaluation.
|
|
|
|
// If nil, then this feature is not supported by the datasource.
|
|
|
|
// SeriesFetched is supported by VictoriaMetrics since v1.90.
|
|
|
|
SeriesFetched *int
|
2021-06-09 09:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
// QuerierBuilder builds Querier with given params.
|
|
|
|
type QuerierBuilder interface {
|
2022-09-15 10:40:22 +00:00
|
|
|
// BuildWithParams creates a new Querier object with the given params
|
2021-04-28 20:41:15 +00:00
|
|
|
BuildWithParams(params QuerierParams) Querier
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
// QuerierParams params for Querier.
|
|
|
|
type QuerierParams struct {
|
2024-10-29 15:30:39 +00:00
|
|
|
DataSourceType string
|
|
|
|
// ApplyIntervalAsTimeFilter is only valid for vlogs datasource.
|
|
|
|
// Set to true if there is no [timeFilter](https://docs.victoriametrics.com/victorialogs/logsql/#time-filter) in the rule expression,
|
|
|
|
// and we will add evaluation interval as an additional timeFilter when querying.
|
|
|
|
ApplyIntervalAsTimeFilter bool
|
|
|
|
EvaluationInterval time.Duration
|
|
|
|
QueryParams url.Values
|
|
|
|
Headers map[string]string
|
|
|
|
Debug bool
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:19:31 +00:00
|
|
|
// Metric is the basic entity which should be return by datasource
|
|
|
|
type Metric struct {
|
2021-06-09 09:20:38 +00:00
|
|
|
Labels []Label
|
|
|
|
Timestamps []int64
|
|
|
|
Values []float64
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-11-09 22:27:32 +00:00
|
|
|
// SetLabel adds or updates existing one label
|
|
|
|
// by the given key and label
|
|
|
|
func (m *Metric) SetLabel(key, value string) {
|
|
|
|
for i, l := range m.Labels {
|
|
|
|
if l.Name == key {
|
|
|
|
m.Labels[i].Value = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.AddLabel(key, value)
|
|
|
|
}
|
|
|
|
|
2022-12-05 07:34:54 +00:00
|
|
|
// SetLabels sets the given map as Metric labels
|
|
|
|
func (m *Metric) SetLabels(ls map[string]string) {
|
|
|
|
var i int
|
|
|
|
m.Labels = make([]Label, len(ls))
|
|
|
|
for k, v := range ls {
|
|
|
|
m.Labels[i] = Label{
|
|
|
|
Name: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 22:27:32 +00:00
|
|
|
// AddLabel appends the given label to the label set
|
|
|
|
func (m *Metric) AddLabel(key, value string) {
|
|
|
|
m.Labels = append(m.Labels, Label{Name: key, Value: value})
|
|
|
|
}
|
|
|
|
|
2023-02-04 03:46:13 +00:00
|
|
|
// DelLabel deletes the given label from the label set
|
|
|
|
func (m *Metric) DelLabel(key string) {
|
|
|
|
for i, l := range m.Labels {
|
|
|
|
if l.Name == key {
|
|
|
|
m.Labels = append(m.Labels[:i], m.Labels[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 18:11:45 +00:00
|
|
|
// Label returns the given label value.
|
|
|
|
// If label is missing empty string will be returned
|
|
|
|
func (m *Metric) Label(key string) string {
|
|
|
|
for _, l := range m.Labels {
|
|
|
|
if l.Name == key {
|
|
|
|
return l.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:22:23 +00:00
|
|
|
// Label represents metric's label
|
2020-03-13 10:19:31 +00:00
|
|
|
type Label struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|
2023-07-20 13:07:10 +00:00
|
|
|
|
|
|
|
// Labels is collection of Label
|
|
|
|
type Labels []Label
|
|
|
|
|
|
|
|
func (ls Labels) Len() int { return len(ls) }
|
|
|
|
func (ls Labels) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] }
|
|
|
|
func (ls Labels) Less(i, j int) bool { return ls[i].Name < ls[j].Name }
|
|
|
|
|
|
|
|
func (ls Labels) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
b.WriteByte('{')
|
|
|
|
for i, l := range ls {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteByte(',')
|
|
|
|
b.WriteByte(' ')
|
|
|
|
}
|
|
|
|
b.WriteString(l.Name)
|
|
|
|
b.WriteByte('=')
|
|
|
|
b.WriteString(strconv.Quote(l.Value))
|
|
|
|
}
|
|
|
|
b.WriteByte('}')
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelCompare return negative if a is less than b, return 0 if they are the same
|
|
|
|
// eg.
|
|
|
|
// a=[]Label{{Name: "a", Value: "1"}},b=[]Label{{Name: "b", Value: "1"}}, return -1
|
|
|
|
// a=[]Label{{Name: "a", Value: "2"}},b=[]Label{{Name: "a", Value: "1"}}, return 1
|
|
|
|
// a=[]Label{{Name: "a", Value: "1"}},b=[]Label{{Name: "a", Value: "1"}}, return 0
|
|
|
|
func LabelCompare(a, b Labels) int {
|
|
|
|
l := len(a)
|
|
|
|
if len(b) < l {
|
|
|
|
l = len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if a[i].Name != b[i].Name {
|
|
|
|
if a[i].Name < b[i].Name {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if a[i].Value != b[i].Value {
|
|
|
|
if a[i].Value < b[i].Value {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if all labels so far were in common, the set with fewer labels comes first.
|
|
|
|
return len(a) - len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertToLabels convert map to Labels
|
|
|
|
func ConvertToLabels(m map[string]string) (labelset Labels) {
|
|
|
|
for k, v := range m {
|
|
|
|
labelset = append(labelset, Label{
|
|
|
|
Name: k,
|
|
|
|
Value: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// sort label
|
2023-07-25 01:56:28 +00:00
|
|
|
sort.Slice(labelset, func(i, j int) bool { return labelset[i].Name < labelset[j].Name })
|
2023-07-20 13:07:10 +00:00
|
|
|
return
|
|
|
|
}
|