prom: add support for default step param (#9866)
Alerting for prometheus have been depending on the step parameter from each query. In https://github.com/grafana/grafana/pull/9226 we changed the behavior for step in the frontend which caused problems for alerting. This commit fixes that by introducing a default min interval value so alerting always have something to depend on. closes #9777
This commit is contained in:
@@ -34,6 +34,7 @@ Name | Description
|
|||||||
*Basic Auth* | Enable basic authentication to the Prometheus data source.
|
*Basic Auth* | Enable basic authentication to the Prometheus data source.
|
||||||
*User* | Name of your Prometheus user
|
*User* | Name of your Prometheus user
|
||||||
*Password* | Database user's password
|
*Password* | Database user's password
|
||||||
|
*Scrape interval* | This will be used as a lower limit for the Prometheus step query parameter. Default value is 15s.
|
||||||
|
|
||||||
## Query editor
|
## Query editor
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package influxdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InfluxdbQueryParser struct{}
|
type InfluxdbQueryParser struct{}
|
||||||
@@ -37,13 +39,7 @@ func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json, dsInfo *models.Data
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
interval := model.Get("interval").MustString("")
|
parsedInterval, err := tsdb.GetIntervalFrom(dsInfo, model, time.Millisecond*1)
|
||||||
if interval == "" && dsInfo.JsonData != nil {
|
|
||||||
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
|
|
||||||
if dsInterval != "" {
|
|
||||||
interval = dsInterval
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Query{
|
return &Query{
|
||||||
Measurement: measurement,
|
Measurement: measurement,
|
||||||
@@ -53,7 +49,7 @@ func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json, dsInfo *models.Data
|
|||||||
Tags: tags,
|
Tags: tags,
|
||||||
Selects: selects,
|
Selects: selects,
|
||||||
RawQuery: rawQuery,
|
RawQuery: rawQuery,
|
||||||
Interval: interval,
|
Interval: parsedInterval,
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
UseRawQuery: useRawQuery,
|
UseRawQuery: useRawQuery,
|
||||||
}, nil
|
}, nil
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package influxdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -115,7 +116,7 @@ func TestInfluxdbQueryParser(t *testing.T) {
|
|||||||
So(len(res.GroupBy), ShouldEqual, 3)
|
So(len(res.GroupBy), ShouldEqual, 3)
|
||||||
So(len(res.Selects), ShouldEqual, 3)
|
So(len(res.Selects), ShouldEqual, 3)
|
||||||
So(len(res.Tags), ShouldEqual, 2)
|
So(len(res.Tags), ShouldEqual, 2)
|
||||||
So(res.Interval, ShouldEqual, ">20s")
|
So(res.Interval, ShouldEqual, time.Second*20)
|
||||||
So(res.Alias, ShouldEqual, "serie alias")
|
So(res.Alias, ShouldEqual, "serie alias")
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -174,7 +175,7 @@ func TestInfluxdbQueryParser(t *testing.T) {
|
|||||||
So(len(res.GroupBy), ShouldEqual, 2)
|
So(len(res.GroupBy), ShouldEqual, 2)
|
||||||
So(len(res.Selects), ShouldEqual, 1)
|
So(len(res.Selects), ShouldEqual, 1)
|
||||||
So(len(res.Tags), ShouldEqual, 0)
|
So(len(res.Tags), ShouldEqual, 0)
|
||||||
So(res.Interval, ShouldEqual, ">10s")
|
So(res.Interval, ShouldEqual, time.Second*10)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package influxdb
|
package influxdb
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Measurement string
|
Measurement string
|
||||||
Policy string
|
Policy string
|
||||||
@@ -10,8 +12,7 @@ type Query struct {
|
|||||||
RawQuery string
|
RawQuery string
|
||||||
UseRawQuery bool
|
UseRawQuery bool
|
||||||
Alias string
|
Alias string
|
||||||
|
Interval time.Duration
|
||||||
Interval string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
|
|||||||
@@ -29,10 +29,8 @@ func (query *Query) Build(queryContext *tsdb.TsdbQuery) (string, error) {
|
|||||||
res += query.renderGroupBy(queryContext)
|
res += query.renderGroupBy(queryContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
interval, err := getDefinedInterval(query, queryContext)
|
calculator := tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{})
|
||||||
if err != nil {
|
interval := calculator.Calculate(queryContext.TimeRange, query.Interval)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryContext), -1)
|
res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryContext), -1)
|
||||||
res = strings.Replace(res, "$interval", interval.Text, -1)
|
res = strings.Replace(res, "$interval", interval.Text, -1)
|
||||||
@@ -41,29 +39,6 @@ func (query *Query) Build(queryContext *tsdb.TsdbQuery) (string, error) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDefinedInterval(query *Query, queryContext *tsdb.TsdbQuery) (*tsdb.Interval, error) {
|
|
||||||
defaultInterval := tsdb.CalculateInterval(queryContext.TimeRange)
|
|
||||||
|
|
||||||
if query.Interval == "" {
|
|
||||||
return &defaultInterval, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
setInterval := strings.Replace(strings.Replace(query.Interval, "<", "", 1), ">", "", 1)
|
|
||||||
parsedSetInterval, err := time.ParseDuration(setInterval)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(query.Interval, ">") {
|
|
||||||
if defaultInterval.Value > parsedSetInterval {
|
|
||||||
return &defaultInterval, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &tsdb.Interval{Value: parsedSetInterval, Text: setInterval}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (query *Query) renderTags() []string {
|
func (query *Query) renderTags() []string {
|
||||||
var res []string
|
var res []string
|
||||||
for i, tag := range query.Tags {
|
for i, tag := range query.Tags {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package influxdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
|
|||||||
Measurement: "cpu",
|
Measurement: "cpu",
|
||||||
Policy: "policy",
|
Policy: "policy",
|
||||||
GroupBy: []*QueryPart{groupBy1, groupBy3},
|
GroupBy: []*QueryPart{groupBy1, groupBy3},
|
||||||
Interval: "10s",
|
Interval: time.Second * 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
rawQuery, err := query.Build(queryContext)
|
rawQuery, err := query.Build(queryContext)
|
||||||
@@ -52,7 +53,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
|
|||||||
Measurement: "cpu",
|
Measurement: "cpu",
|
||||||
GroupBy: []*QueryPart{groupBy1, groupBy2, groupBy3},
|
GroupBy: []*QueryPart{groupBy1, groupBy2, groupBy3},
|
||||||
Tags: []*Tag{tag1, tag2},
|
Tags: []*Tag{tag1, tag2},
|
||||||
Interval: "5s",
|
Interval: time.Second * 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
rawQuery, err := query.Build(queryContext)
|
rawQuery, err := query.Build(queryContext)
|
||||||
@@ -64,7 +65,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
|
|||||||
query := &Query{
|
query := &Query{
|
||||||
Selects: []*Select{{*qp1, *qp2, *mathPartDivideBy100}},
|
Selects: []*Select{{*qp1, *qp2, *mathPartDivideBy100}},
|
||||||
Measurement: "cpu",
|
Measurement: "cpu",
|
||||||
Interval: "5s",
|
Interval: time.Second * 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
rawQuery, err := query.Build(queryContext)
|
rawQuery, err := query.Build(queryContext)
|
||||||
@@ -76,7 +77,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
|
|||||||
query := &Query{
|
query := &Query{
|
||||||
Selects: []*Select{{*qp1, *qp2, *mathPartDivideByIntervalMs}},
|
Selects: []*Select{{*qp1, *qp2, *mathPartDivideByIntervalMs}},
|
||||||
Measurement: "cpu",
|
Measurement: "cpu",
|
||||||
Interval: "5s",
|
Interval: time.Second * 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
rawQuery, err := query.Build(queryContext)
|
rawQuery, err := query.Build(queryContext)
|
||||||
@@ -117,7 +118,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
|
|||||||
Measurement: "cpu",
|
Measurement: "cpu",
|
||||||
Policy: "policy",
|
Policy: "policy",
|
||||||
GroupBy: []*QueryPart{groupBy1, groupBy3},
|
GroupBy: []*QueryPart{groupBy1, groupBy3},
|
||||||
Interval: "10s",
|
Interval: time.Second * 10,
|
||||||
RawQuery: "Raw query",
|
RawQuery: "Raw query",
|
||||||
UseRawQuery: true,
|
UseRawQuery: true,
|
||||||
}
|
}
|
||||||
|
|||||||
+67
-9
@@ -2,14 +2,18 @@ package tsdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultRes int64 = 1500
|
defaultRes int64 = 1500
|
||||||
minInterval time.Duration = 1 * time.Millisecond
|
defaultMinInterval time.Duration = 1 * time.Millisecond
|
||||||
year time.Duration = time.Hour * 24 * 365
|
year time.Duration = time.Hour * 24 * 365
|
||||||
day time.Duration = time.Hour * 24 * 365
|
day time.Duration = time.Hour * 24
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interval struct {
|
type Interval struct {
|
||||||
@@ -17,14 +21,68 @@ type Interval struct {
|
|||||||
Value time.Duration
|
Value time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateInterval(timerange *TimeRange) Interval {
|
type intervalCalculator struct {
|
||||||
interval := time.Duration((timerange.MustGetTo().UnixNano() - timerange.MustGetFrom().UnixNano()) / defaultRes)
|
minInterval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
if interval < minInterval {
|
type IntervalCalculator interface {
|
||||||
return Interval{Text: formatDuration(minInterval), Value: interval}
|
Calculate(timeRange *TimeRange, minInterval time.Duration) Interval
|
||||||
|
}
|
||||||
|
|
||||||
|
type IntervalOptions struct {
|
||||||
|
MinInterval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIntervalCalculator(opt *IntervalOptions) *intervalCalculator {
|
||||||
|
if opt == nil {
|
||||||
|
opt = &IntervalOptions{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Interval{Text: formatDuration(roundInterval(interval)), Value: interval}
|
calc := &intervalCalculator{}
|
||||||
|
|
||||||
|
if opt.MinInterval == 0 {
|
||||||
|
calc.minInterval = defaultMinInterval
|
||||||
|
} else {
|
||||||
|
calc.minInterval = opt.MinInterval
|
||||||
|
}
|
||||||
|
|
||||||
|
return calc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ic *intervalCalculator) Calculate(timerange *TimeRange, minInterval time.Duration) Interval {
|
||||||
|
to := timerange.MustGetTo().UnixNano()
|
||||||
|
from := timerange.MustGetFrom().UnixNano()
|
||||||
|
interval := time.Duration((to - from) / defaultRes)
|
||||||
|
|
||||||
|
if interval < minInterval {
|
||||||
|
return Interval{Text: formatDuration(minInterval), Value: minInterval}
|
||||||
|
}
|
||||||
|
|
||||||
|
rounded := roundInterval(interval)
|
||||||
|
return Interval{Text: formatDuration(rounded), Value: rounded}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error) {
|
||||||
|
interval := queryModel.Get("interval").MustString("")
|
||||||
|
|
||||||
|
if interval == "" && dsInfo.JsonData != nil {
|
||||||
|
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
|
||||||
|
if dsInterval != "" {
|
||||||
|
interval = dsInterval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if interval == "" {
|
||||||
|
return defaultInterval, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
interval = strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1)
|
||||||
|
parsedInterval, err := time.ParseDuration(interval)
|
||||||
|
if err != nil {
|
||||||
|
return time.Duration(0), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedInterval, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatDuration(inter time.Duration) string {
|
func formatDuration(inter time.Duration) string {
|
||||||
|
|||||||
@@ -14,31 +14,33 @@ func TestInterval(t *testing.T) {
|
|||||||
HomePath: "../../",
|
HomePath: "../../",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
calculator := NewIntervalCalculator(&IntervalOptions{})
|
||||||
|
|
||||||
Convey("for 5min", func() {
|
Convey("for 5min", func() {
|
||||||
tr := NewTimeRange("5m", "now")
|
tr := NewTimeRange("5m", "now")
|
||||||
|
|
||||||
interval := CalculateInterval(tr)
|
interval := calculator.Calculate(tr, time.Millisecond*1)
|
||||||
So(interval.Text, ShouldEqual, "200ms")
|
So(interval.Text, ShouldEqual, "200ms")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("for 15min", func() {
|
Convey("for 15min", func() {
|
||||||
tr := NewTimeRange("15m", "now")
|
tr := NewTimeRange("15m", "now")
|
||||||
|
|
||||||
interval := CalculateInterval(tr)
|
interval := calculator.Calculate(tr, time.Millisecond*1)
|
||||||
So(interval.Text, ShouldEqual, "500ms")
|
So(interval.Text, ShouldEqual, "500ms")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("for 30min", func() {
|
Convey("for 30min", func() {
|
||||||
tr := NewTimeRange("30m", "now")
|
tr := NewTimeRange("30m", "now")
|
||||||
|
|
||||||
interval := CalculateInterval(tr)
|
interval := calculator.Calculate(tr, time.Millisecond*1)
|
||||||
So(interval.Text, ShouldEqual, "1s")
|
So(interval.Text, ShouldEqual, "1s")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("for 1h", func() {
|
Convey("for 1h", func() {
|
||||||
tr := NewTimeRange("1h", "now")
|
tr := NewTimeRange("1h", "now")
|
||||||
|
|
||||||
interval := CalculateInterval(tr)
|
interval := calculator.Calculate(tr, time.Millisecond*1)
|
||||||
So(interval.Text, ShouldEqual, "2s")
|
So(interval.Text, ShouldEqual, "2s")
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -51,6 +53,7 @@ func TestInterval(t *testing.T) {
|
|||||||
So(formatDuration(time.Second*61), ShouldEqual, "1m")
|
So(formatDuration(time.Second*61), ShouldEqual, "1m")
|
||||||
So(formatDuration(time.Millisecond*30), ShouldEqual, "30ms")
|
So(formatDuration(time.Millisecond*30), ShouldEqual, "30ms")
|
||||||
So(formatDuration(time.Hour*23), ShouldEqual, "23h")
|
So(formatDuration(time.Hour*23), ShouldEqual, "23h")
|
||||||
|
So(formatDuration(time.Hour*24), ShouldEqual, "1d")
|
||||||
So(formatDuration(time.Hour*24*367), ShouldEqual, "1y")
|
So(formatDuration(time.Hour*24*367), ShouldEqual, "1y")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -48,14 +48,16 @@ func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
plog log.Logger
|
plog log.Logger
|
||||||
legendFormat *regexp.Regexp
|
legendFormat *regexp.Regexp
|
||||||
|
intervalCalculator tsdb.IntervalCalculator
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
plog = log.New("tsdb.prometheus")
|
plog = log.New("tsdb.prometheus")
|
||||||
tsdb.RegisterTsdbQueryEndpoint("prometheus", NewPrometheusExecutor)
|
tsdb.RegisterTsdbQueryEndpoint("prometheus", NewPrometheusExecutor)
|
||||||
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
||||||
|
intervalCalculator = tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{MinInterval: time.Second * 1})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *PrometheusExecutor) getClient(dsInfo *models.DataSource) (apiv1.API, error) {
|
func (e *PrometheusExecutor) getClient(dsInfo *models.DataSource) (apiv1.API, error) {
|
||||||
@@ -88,7 +90,7 @@ func (e *PrometheusExecutor) Query(ctx context.Context, dsInfo *models.DataSourc
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
query, err := parseQuery(tsdbQuery.Queries, tsdbQuery)
|
query, err := parseQuery(dsInfo, tsdbQuery.Queries, tsdbQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -138,7 +140,7 @@ func formatLegend(metric model.Metric, query *PrometheusQuery) string {
|
|||||||
return string(result)
|
return string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseQuery(queries []*tsdb.Query, queryContext *tsdb.TsdbQuery) (*PrometheusQuery, error) {
|
func parseQuery(dsInfo *models.DataSource, queries []*tsdb.Query, queryContext *tsdb.TsdbQuery) (*PrometheusQuery, error) {
|
||||||
queryModel := queries[0]
|
queryModel := queries[0]
|
||||||
|
|
||||||
expr, err := queryModel.Model.Get("expr").String()
|
expr, err := queryModel.Model.Get("expr").String()
|
||||||
@@ -146,11 +148,6 @@ func parseQuery(queries []*tsdb.Query, queryContext *tsdb.TsdbQuery) (*Prometheu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
step, err := queryModel.Model.Get("step").Int64()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
format := queryModel.Model.Get("legendFormat").MustString("")
|
format := queryModel.Model.Get("legendFormat").MustString("")
|
||||||
|
|
||||||
start, err := queryContext.TimeRange.ParseFrom()
|
start, err := queryContext.TimeRange.ParseFrom()
|
||||||
@@ -163,9 +160,18 @@ func parseQuery(queries []*tsdb.Query, queryContext *tsdb.TsdbQuery) (*Prometheu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dsInterval, err := tsdb.GetIntervalFrom(dsInfo, queryModel.Model, time.Second*15)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
intervalFactor := queryModel.Model.Get("intervalFactor").MustInt64(1)
|
||||||
|
interval := intervalCalculator.Calculate(queryContext.TimeRange, dsInterval)
|
||||||
|
step := time.Duration(int64(interval.Value) * intervalFactor)
|
||||||
|
|
||||||
return &PrometheusQuery{
|
return &PrometheusQuery{
|
||||||
Expr: expr,
|
Expr: expr,
|
||||||
Step: time.Second * time.Duration(step),
|
Step: step,
|
||||||
LegendFormat: format,
|
LegendFormat: format,
|
||||||
Start: start,
|
Start: start,
|
||||||
End: end,
|
End: end,
|
||||||
|
|||||||
@@ -2,13 +2,21 @@ package prometheus
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
p "github.com/prometheus/common/model"
|
p "github.com/prometheus/common/model"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPrometheus(t *testing.T) {
|
func TestPrometheus(t *testing.T) {
|
||||||
Convey("Prometheus", t, func() {
|
Convey("Prometheus", t, func() {
|
||||||
|
dsInfo := &models.DataSource{
|
||||||
|
JsonData: simplejson.New(),
|
||||||
|
}
|
||||||
|
|
||||||
Convey("converting metric name", func() {
|
Convey("converting metric name", func() {
|
||||||
metric := map[p.LabelName]p.LabelValue{
|
metric := map[p.LabelName]p.LabelValue{
|
||||||
@@ -36,5 +44,108 @@ func TestPrometheus(t *testing.T) {
|
|||||||
|
|
||||||
So(formatLegend(metric, query), ShouldEqual, `http_request_total{app="backend", device="mobile"}`)
|
So(formatLegend(metric, query), ShouldEqual, `http_request_total{app="backend", device="mobile"}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("parsing query model with step", func() {
|
||||||
|
json := `{
|
||||||
|
"expr": "go_goroutines",
|
||||||
|
"format": "time_series",
|
||||||
|
"refId": "A"
|
||||||
|
}`
|
||||||
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
||||||
|
queryContext := &tsdb.TsdbQuery{}
|
||||||
|
queryModels := []*tsdb.Query{
|
||||||
|
{Model: jsonModel},
|
||||||
|
}
|
||||||
|
|
||||||
|
Convey("with 48h time range", func() {
|
||||||
|
queryContext.TimeRange = tsdb.NewTimeRange("12h", "now")
|
||||||
|
|
||||||
|
model, err := parseQuery(dsInfo, queryModels, queryContext)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(model.Step, ShouldEqual, time.Second*30)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("parsing query model without step parameter", func() {
|
||||||
|
json := `{
|
||||||
|
"expr": "go_goroutines",
|
||||||
|
"format": "time_series",
|
||||||
|
"intervalFactor": 1,
|
||||||
|
"refId": "A"
|
||||||
|
}`
|
||||||
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
||||||
|
queryContext := &tsdb.TsdbQuery{}
|
||||||
|
queryModels := []*tsdb.Query{
|
||||||
|
{Model: jsonModel},
|
||||||
|
}
|
||||||
|
|
||||||
|
Convey("with 48h time range", func() {
|
||||||
|
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
|
||||||
|
|
||||||
|
model, err := parseQuery(dsInfo, queryModels, queryContext)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(model.Step, ShouldEqual, time.Minute*2)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("with 1h time range", func() {
|
||||||
|
queryContext.TimeRange = tsdb.NewTimeRange("1h", "now")
|
||||||
|
|
||||||
|
model, err := parseQuery(dsInfo, queryModels, queryContext)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(model.Step, ShouldEqual, time.Second*15)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("parsing query model with intervalFactor", func() {
|
||||||
|
Convey("high intervalFactor", func() {
|
||||||
|
json := `{
|
||||||
|
"expr": "go_goroutines",
|
||||||
|
"format": "time_series",
|
||||||
|
"intervalFactor": 10,
|
||||||
|
"refId": "A"
|
||||||
|
}`
|
||||||
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
||||||
|
queryContext := &tsdb.TsdbQuery{}
|
||||||
|
queryModels := []*tsdb.Query{
|
||||||
|
{Model: jsonModel},
|
||||||
|
}
|
||||||
|
|
||||||
|
Convey("with 48h time range", func() {
|
||||||
|
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
|
||||||
|
|
||||||
|
model, err := parseQuery(dsInfo, queryModels, queryContext)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(model.Step, ShouldEqual, time.Minute*20)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("low intervalFactor", func() {
|
||||||
|
json := `{
|
||||||
|
"expr": "go_goroutines",
|
||||||
|
"format": "time_series",
|
||||||
|
"intervalFactor": 1,
|
||||||
|
"refId": "A"
|
||||||
|
}`
|
||||||
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
||||||
|
queryContext := &tsdb.TsdbQuery{}
|
||||||
|
queryModels := []*tsdb.Query{
|
||||||
|
{Model: jsonModel},
|
||||||
|
}
|
||||||
|
|
||||||
|
Convey("with 48h time range", func() {
|
||||||
|
queryContext.TimeRange = tsdb.NewTimeRange("48h", "now")
|
||||||
|
|
||||||
|
model, err := parseQuery(dsInfo, queryModels, queryContext)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(model.Step, ShouldEqual, time.Minute*2)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ export class PrometheusDatasource {
|
|||||||
basicAuth: any;
|
basicAuth: any;
|
||||||
withCredentials: any;
|
withCredentials: any;
|
||||||
metricsNameCache: any;
|
metricsNameCache: any;
|
||||||
|
interval: string;
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
constructor(instanceSettings,
|
constructor(instanceSettings,
|
||||||
@@ -34,6 +35,7 @@ export class PrometheusDatasource {
|
|||||||
this.directUrl = instanceSettings.directUrl;
|
this.directUrl = instanceSettings.directUrl;
|
||||||
this.basicAuth = instanceSettings.basicAuth;
|
this.basicAuth = instanceSettings.basicAuth;
|
||||||
this.withCredentials = instanceSettings.withCredentials;
|
this.withCredentials = instanceSettings.withCredentials;
|
||||||
|
this.interval = instanceSettings.jsonData.timeInterval || '15s';
|
||||||
}
|
}
|
||||||
|
|
||||||
_request(method, url, requestId?) {
|
_request(method, url, requestId?) {
|
||||||
|
|||||||
@@ -1,3 +1,16 @@
|
|||||||
<datasource-http-settings current="ctrl.current" suggest-url="http://localhost:9090">
|
<datasource-http-settings current="ctrl.current" suggest-url="http://localhost:9090">
|
||||||
</datasource-http-settings>
|
</datasource-http-settings>
|
||||||
|
|
||||||
|
<div class="gf-form-group">
|
||||||
|
<div class="gf-form-inline">
|
||||||
|
<div class="gf-form">
|
||||||
|
<span class="gf-form-label">Scrape interval</span>
|
||||||
|
<input type="text" class="gf-form-input width-6" ng-model="ctrl.current.jsonData.timeInterval" spellcheck='false' placeholder="15s"></input>
|
||||||
|
<info-popover mode="right-absolute">
|
||||||
|
Set this to your global scrape interval defined in your Prometheus config file. This will be used as a lower limit for
|
||||||
|
the Prometheus step query parameter.
|
||||||
|
</info-popover>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {PrometheusDatasource} from '../datasource';
|
|||||||
|
|
||||||
describe('PrometheusDatasource', function() {
|
describe('PrometheusDatasource', function() {
|
||||||
var ctx = new helpers.ServiceTestContext();
|
var ctx = new helpers.ServiceTestContext();
|
||||||
var instanceSettings = {url: 'proxied', directUrl: 'direct', user: 'test', password: 'mupp' };
|
var instanceSettings = {url: 'proxied', directUrl: 'direct', user: 'test', password: 'mupp', jsonData: {}};
|
||||||
|
|
||||||
beforeEach(angularMocks.module('grafana.core'));
|
beforeEach(angularMocks.module('grafana.core'));
|
||||||
beforeEach(angularMocks.module('grafana.services'));
|
beforeEach(angularMocks.module('grafana.services'));
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import PrometheusMetricFindQuery from '../metric_find_query';
|
|||||||
describe('PrometheusMetricFindQuery', function() {
|
describe('PrometheusMetricFindQuery', function() {
|
||||||
|
|
||||||
var ctx = new helpers.ServiceTestContext();
|
var ctx = new helpers.ServiceTestContext();
|
||||||
var instanceSettings = {url: 'proxied', directUrl: 'direct', user: 'test', password: 'mupp' };
|
var instanceSettings = {url: 'proxied', directUrl: 'direct', user: 'test', password: 'mupp', jsonData: {}};
|
||||||
|
|
||||||
beforeEach(angularMocks.module('grafana.core'));
|
beforeEach(angularMocks.module('grafana.core'));
|
||||||
beforeEach(angularMocks.module('grafana.services'));
|
beforeEach(angularMocks.module('grafana.services'));
|
||||||
|
|||||||
Reference in New Issue
Block a user