Skip to content
Permalink
Browse files

prom: add support for default step param (#9866)

Alerting for prometheus have been depending on the step parameter from each query.
In #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
  • Loading branch information...
bergquist committed Nov 15, 2017
1 parent 9e6a7dc commit 5d6ed6c45fcb01005386bcfd21f8dbb3e1ce2f8c
@@ -34,6 +34,7 @@ Name | Description
*Basic Auth* | Enable basic authentication to the Prometheus data source.
*User* | Name of your Prometheus user
*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

@@ -2,9 +2,11 @@ package influxdb

import (
"strconv"
"time"

"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
)

type InfluxdbQueryParser struct{}
@@ -37,13 +39,7 @@ func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json, dsInfo *models.Data
return nil, err
}

interval := model.Get("interval").MustString("")
if interval == "" && dsInfo.JsonData != nil {
dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
if dsInterval != "" {
interval = dsInterval
}
}
parsedInterval, err := tsdb.GetIntervalFrom(dsInfo, model, time.Millisecond*1)

return &Query{
Measurement: measurement,
@@ -53,7 +49,7 @@ func (qp *InfluxdbQueryParser) Parse(model *simplejson.Json, dsInfo *models.Data
Tags: tags,
Selects: selects,
RawQuery: rawQuery,
Interval: interval,
Interval: parsedInterval,
Alias: alias,
UseRawQuery: useRawQuery,
}, nil
@@ -2,6 +2,7 @@ package influxdb

import (
"testing"
"time"

"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
@@ -115,7 +116,7 @@ func TestInfluxdbQueryParser(t *testing.T) {
So(len(res.GroupBy), ShouldEqual, 3)
So(len(res.Selects), ShouldEqual, 3)
So(len(res.Tags), ShouldEqual, 2)
So(res.Interval, ShouldEqual, ">20s")
So(res.Interval, ShouldEqual, time.Second*20)
So(res.Alias, ShouldEqual, "serie alias")
})

@@ -174,7 +175,7 @@ func TestInfluxdbQueryParser(t *testing.T) {
So(len(res.GroupBy), ShouldEqual, 2)
So(len(res.Selects), ShouldEqual, 1)
So(len(res.Tags), ShouldEqual, 0)
So(res.Interval, ShouldEqual, ">10s")
So(res.Interval, ShouldEqual, time.Second*10)
})
})
}
@@ -1,5 +1,7 @@
package influxdb

import "time"

type Query struct {
Measurement string
Policy string
@@ -10,8 +12,7 @@ type Query struct {
RawQuery string
UseRawQuery bool
Alias string

Interval string
Interval time.Duration
}

type Tag struct {
@@ -29,10 +29,8 @@ func (query *Query) Build(queryContext *tsdb.TsdbQuery) (string, error) {
res += query.renderGroupBy(queryContext)
}

interval, err := getDefinedInterval(query, queryContext)
if err != nil {
return "", err
}
calculator := tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{})
interval := calculator.Calculate(queryContext.TimeRange, query.Interval)

res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryContext), -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
}

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 {
var res []string
for i, tag := range query.Tags {
@@ -2,6 +2,7 @@ package influxdb

import (
"testing"
"time"

"strings"

@@ -38,7 +39,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
Measurement: "cpu",
Policy: "policy",
GroupBy: []*QueryPart{groupBy1, groupBy3},
Interval: "10s",
Interval: time.Second * 10,
}

rawQuery, err := query.Build(queryContext)
@@ -52,7 +53,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
Measurement: "cpu",
GroupBy: []*QueryPart{groupBy1, groupBy2, groupBy3},
Tags: []*Tag{tag1, tag2},
Interval: "5s",
Interval: time.Second * 5,
}

rawQuery, err := query.Build(queryContext)
@@ -64,7 +65,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
query := &Query{
Selects: []*Select{{*qp1, *qp2, *mathPartDivideBy100}},
Measurement: "cpu",
Interval: "5s",
Interval: time.Second * 5,
}

rawQuery, err := query.Build(queryContext)
@@ -76,7 +77,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
query := &Query{
Selects: []*Select{{*qp1, *qp2, *mathPartDivideByIntervalMs}},
Measurement: "cpu",
Interval: "5s",
Interval: time.Second * 5,
}

rawQuery, err := query.Build(queryContext)
@@ -117,7 +118,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
Measurement: "cpu",
Policy: "policy",
GroupBy: []*QueryPart{groupBy1, groupBy3},
Interval: "10s",
Interval: time.Second * 10,
RawQuery: "Raw query",
UseRawQuery: true,
}
@@ -2,29 +2,87 @@ package tsdb

import (
"fmt"
"strings"
"time"

"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
)

var (
defaultRes int64 = 1500
minInterval time.Duration = 1 * time.Millisecond
year time.Duration = time.Hour * 24 * 365
day time.Duration = time.Hour * 24 * 365
defaultRes int64 = 1500
defaultMinInterval time.Duration = 1 * time.Millisecond
year time.Duration = time.Hour * 24 * 365
day time.Duration = time.Hour * 24
)

type Interval struct {
Text string
Value time.Duration
}

func CalculateInterval(timerange *TimeRange) Interval {
interval := time.Duration((timerange.MustGetTo().UnixNano() - timerange.MustGetFrom().UnixNano()) / defaultRes)
type intervalCalculator struct {
minInterval time.Duration
}

type IntervalCalculator interface {
Calculate(timeRange *TimeRange, minInterval time.Duration) Interval
}

type IntervalOptions struct {
MinInterval time.Duration
}

func NewIntervalCalculator(opt *IntervalOptions) *intervalCalculator {
if opt == nil {
opt = &IntervalOptions{}
}

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: interval}
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 Interval{Text: formatDuration(roundInterval(interval)), Value: interval}
return parsedInterval, nil
}

func formatDuration(inter time.Duration) string {
@@ -14,31 +14,33 @@ func TestInterval(t *testing.T) {
HomePath: "../../",
})

calculator := NewIntervalCalculator(&IntervalOptions{})

Convey("for 5min", func() {
tr := NewTimeRange("5m", "now")

interval := CalculateInterval(tr)
interval := calculator.Calculate(tr, time.Millisecond*1)
So(interval.Text, ShouldEqual, "200ms")
})

Convey("for 15min", func() {
tr := NewTimeRange("15m", "now")

interval := CalculateInterval(tr)
interval := calculator.Calculate(tr, time.Millisecond*1)
So(interval.Text, ShouldEqual, "500ms")
})

Convey("for 30min", func() {
tr := NewTimeRange("30m", "now")

interval := CalculateInterval(tr)
interval := calculator.Calculate(tr, time.Millisecond*1)
So(interval.Text, ShouldEqual, "1s")
})

Convey("for 1h", func() {
tr := NewTimeRange("1h", "now")

interval := CalculateInterval(tr)
interval := calculator.Calculate(tr, time.Millisecond*1)
So(interval.Text, ShouldEqual, "2s")
})

@@ -51,6 +53,7 @@ func TestInterval(t *testing.T) {
So(formatDuration(time.Second*61), ShouldEqual, "1m")
So(formatDuration(time.Millisecond*30), ShouldEqual, "30ms")
So(formatDuration(time.Hour*23), ShouldEqual, "23h")
So(formatDuration(time.Hour*24), ShouldEqual, "1d")
So(formatDuration(time.Hour*24*367), ShouldEqual, "1y")
})
})
@@ -48,14 +48,16 @@ func NewPrometheusExecutor(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, e
}

var (
plog log.Logger
legendFormat *regexp.Regexp
plog log.Logger
legendFormat *regexp.Regexp
intervalCalculator tsdb.IntervalCalculator
)

func init() {
plog = log.New("tsdb.prometheus")
tsdb.RegisterTsdbQueryEndpoint("prometheus", NewPrometheusExecutor)
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
intervalCalculator = tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{MinInterval: time.Second * 1})
}

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
}

query, err := parseQuery(tsdbQuery.Queries, tsdbQuery)
query, err := parseQuery(dsInfo, tsdbQuery.Queries, tsdbQuery)
if err != nil {
return nil, err
}
@@ -138,19 +140,14 @@ func formatLegend(metric model.Metric, query *PrometheusQuery) string {
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]

expr, err := queryModel.Model.Get("expr").String()
if err != nil {
return nil, err
}

step, err := queryModel.Model.Get("step").Int64()
if err != nil {
return nil, err
}

format := queryModel.Model.Get("legendFormat").MustString("")

start, err := queryContext.TimeRange.ParseFrom()
@@ -163,9 +160,18 @@ func parseQuery(queries []*tsdb.Query, queryContext *tsdb.TsdbQuery) (*Prometheu
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{
Expr: expr,
Step: time.Second * time.Duration(step),
Step: step,
LegendFormat: format,
Start: start,
End: end,

0 comments on commit 5d6ed6c

Please sign in to comment.
You can’t perform that action at this time.