2020-02-16 18:59:02 +00:00
|
|
|
package datasource
|
|
|
|
|
2021-02-01 13:02:44 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2021-12-02 12:45:08 +00:00
|
|
|
"net/url"
|
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 {
|
|
|
|
Query(ctx context.Context, query string) ([]Metric, error)
|
|
|
|
QueryRange(ctx context.Context, query string, from, to time.Time) ([]Metric, error)
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
// QuerierBuilder builds Querier with given params.
|
|
|
|
type QuerierBuilder interface {
|
|
|
|
BuildWithParams(params QuerierParams) Querier
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:20:38 +00:00
|
|
|
// QuerierParams params for Querier.
|
|
|
|
type QuerierParams struct {
|
|
|
|
DataSourceType *Type
|
|
|
|
EvaluationInterval time.Duration
|
2021-12-02 12:45:08 +00:00
|
|
|
QueryParams url.Values
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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
|
|
|
}
|