2020-04-27 23:19:27 +02:00
|
|
|
package datasource
|
|
|
|
|
2021-02-01 14:02:44 +01:00
|
|
|
import (
|
|
|
|
"context"
|
2022-09-15 12:40:22 +02:00
|
|
|
"net/http"
|
2021-12-02 13:45:08 +01:00
|
|
|
"net/url"
|
2021-06-09 11:20:38 +02:00
|
|
|
"time"
|
2021-02-01 14:02:44 +01:00
|
|
|
)
|
2020-04-27 23:19:27 +02:00
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
// Querier interface wraps Query and QueryRange methods
|
|
|
|
type Querier interface {
|
2022-09-15 12:40:22 +02: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.
|
|
|
|
Query(ctx context.Context, query string, ts time.Time) ([]Metric, *http.Request, error)
|
|
|
|
// 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.
|
2021-06-09 11:20:38 +02:00
|
|
|
QueryRange(ctx context.Context, query string, from, to time.Time) ([]Metric, error)
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:41:15 +02:00
|
|
|
// QuerierBuilder builds Querier with given params.
|
|
|
|
type QuerierBuilder interface {
|
2022-09-15 12:40:22 +02:00
|
|
|
// BuildWithParams creates a new Querier object with the given params
|
2021-04-28 22:41:15 +02:00
|
|
|
BuildWithParams(params QuerierParams) Querier
|
|
|
|
}
|
|
|
|
|
2021-06-09 11:20:38 +02:00
|
|
|
// QuerierParams params for Querier.
|
|
|
|
type QuerierParams struct {
|
2022-07-22 10:44:55 +02:00
|
|
|
DataSourceType string
|
2021-06-09 11:20:38 +02:00
|
|
|
EvaluationInterval time.Duration
|
2021-12-02 13:45:08 +01:00
|
|
|
QueryParams url.Values
|
2022-07-22 10:44:55 +02:00
|
|
|
Headers map[string]string
|
2022-09-13 15:25:43 +02:00
|
|
|
Debug bool
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Metric is the basic entity which should be return by datasource
|
|
|
|
type Metric struct {
|
2021-06-09 11:20:38 +02:00
|
|
|
Labels []Label
|
|
|
|
Timestamps []int64
|
|
|
|
Values []float64
|
2020-04-27 23:19:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 23:27:32 +01: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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})
|
|
|
|
}
|
|
|
|
|
2020-12-14 19:11:45 +01: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-04-27 23:19:27 +02:00
|
|
|
// Label represents metric's label
|
|
|
|
type Label struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|