Prometheus: minStep value is affecting $__interval value (#80904)

* minStep value is affecting $__interval value

* rename
This commit is contained in:
ismail simsek 2024-01-19 21:55:27 +01:00 committed by GitHub
parent 9fc789d901
commit 777b9bdc49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View File

@ -84,7 +84,7 @@ func Parse(query backend.DataQuery, dsScrapeInterval string, intervalCalculator
}
// Final step value for prometheus
calculatedMinStep, err := calculatePrometheusInterval(model.Interval, dsScrapeInterval, model.IntervalMs, model.IntervalFactor, query, intervalCalculator)
calculatedStep, err := calculatePrometheusInterval(model.Interval, dsScrapeInterval, model.IntervalMs, model.IntervalFactor, query, intervalCalculator)
if err != nil {
return nil, err
}
@ -94,7 +94,7 @@ func Parse(query backend.DataQuery, dsScrapeInterval string, intervalCalculator
expr := interpolateVariables(
model.Expr,
query.Interval,
calculatedMinStep,
calculatedStep,
model.Interval,
dsScrapeInterval,
timeRange,
@ -126,7 +126,7 @@ func Parse(query backend.DataQuery, dsScrapeInterval string, intervalCalculator
return &Query{
Expr: expr,
Step: calculatedMinStep,
Step: calculatedStep,
LegendFormat: model.LegendFormat,
Start: query.TimeRange.From,
End: query.TimeRange.To,
@ -226,14 +226,14 @@ func calculateRateInterval(
// interpolateVariables interpolates built-in variables
// expr PromQL query
// queryInterval Requested interval in milliseconds. This value may be overridden by MinStep in query options
// calculatedMinStep Calculated final step value. It was calculated in calculatePrometheusInterval
// calculatedStep Calculated final step value. It was calculated in calculatePrometheusInterval
// requestedMinStep Requested minimum step value. QueryModel.interval
// dsScrapeInterval Data source scrape interval in the config
// timeRange Requested time range for query
func interpolateVariables(
expr string,
queryInterval time.Duration,
calculatedMinStep time.Duration,
calculatedStep time.Duration,
requestedMinStep string,
dsScrapeInterval string,
timeRange time.Duration,
@ -243,10 +243,10 @@ func interpolateVariables(
var rateInterval time.Duration
if requestedMinStep == varRateInterval || requestedMinStep == varRateIntervalAlt {
rateInterval = calculatedMinStep
rateInterval = calculatedStep
} else {
if requestedMinStep == varInterval || requestedMinStep == varIntervalAlt {
requestedMinStep = calculatedMinStep.String()
requestedMinStep = calculatedStep.String()
}
if requestedMinStep == "" {
requestedMinStep = dsScrapeInterval
@ -254,8 +254,8 @@ func interpolateVariables(
rateInterval = calculateRateInterval(queryInterval, requestedMinStep)
}
expr = strings.ReplaceAll(expr, varIntervalMs, strconv.FormatInt(int64(queryInterval/time.Millisecond), 10))
expr = strings.ReplaceAll(expr, varInterval, intervalv2.FormatDuration(queryInterval))
expr = strings.ReplaceAll(expr, varIntervalMs, strconv.FormatInt(int64(calculatedStep/time.Millisecond), 10))
expr = strings.ReplaceAll(expr, varInterval, intervalv2.FormatDuration(calculatedStep))
expr = strings.ReplaceAll(expr, varRangeMs, strconv.FormatInt(rangeMs, 10))
expr = strings.ReplaceAll(expr, varRangeS, strconv.FormatInt(rangeSRounded, 10))
expr = strings.ReplaceAll(expr, varRange, strconv.FormatInt(rangeSRounded, 10)+"s")
@ -263,8 +263,8 @@ func interpolateVariables(
expr = strings.ReplaceAll(expr, varRateInterval, rateInterval.String())
// Repetitive code, we should have functionality to unify these
expr = strings.ReplaceAll(expr, varIntervalMsAlt, strconv.FormatInt(int64(queryInterval/time.Millisecond), 10))
expr = strings.ReplaceAll(expr, varIntervalAlt, intervalv2.FormatDuration(queryInterval))
expr = strings.ReplaceAll(expr, varIntervalMsAlt, strconv.FormatInt(int64(calculatedStep/time.Millisecond), 10))
expr = strings.ReplaceAll(expr, varIntervalAlt, intervalv2.FormatDuration(calculatedStep))
expr = strings.ReplaceAll(expr, varRangeMsAlt, strconv.FormatInt(rangeMs, 10))
expr = strings.ReplaceAll(expr, varRangeSAlt, strconv.FormatInt(rangeSRounded, 10))
expr = strings.ReplaceAll(expr, varRangeAlt, strconv.FormatInt(rangeSRounded, 10)+"s")

View File

@ -147,7 +147,7 @@ func TestParse(t *testing.T) {
res, err := models.Parse(q, "15s", intervalCalculator, false)
require.NoError(t, err)
require.Equal(t, "rate(ALERTS{job=\"test\" [1m]})", res.Expr)
require.Equal(t, "rate(ALERTS{job=\"test\" [2m]})", res.Expr)
require.Equal(t, 120*time.Second, res.Step)
})
@ -168,7 +168,7 @@ func TestParse(t *testing.T) {
res, err := models.Parse(q, "15s", intervalCalculator, false)
require.NoError(t, err)
require.Equal(t, "rate(ALERTS{job=\"test\" [1m]})", res.Expr)
require.Equal(t, "rate(ALERTS{job=\"test\" [2m]})", res.Expr)
})
t.Run("parsing query model with $__interval_ms variable", func(t *testing.T) {
@ -187,7 +187,7 @@ func TestParse(t *testing.T) {
res, err := models.Parse(q, "15s", intervalCalculator, false)
require.NoError(t, err)
require.Equal(t, "rate(ALERTS{job=\"test\" [60000]})", res.Expr)
require.Equal(t, "rate(ALERTS{job=\"test\" [120000]})", res.Expr)
})
t.Run("parsing query model with $__interval_ms and $__interval variable", func(t *testing.T) {
@ -206,7 +206,7 @@ func TestParse(t *testing.T) {
res, err := models.Parse(q, "15s", intervalCalculator, false)
require.NoError(t, err)
require.Equal(t, "rate(ALERTS{job=\"test\" [60000]}) + rate(ALERTS{job=\"test\" [1m]})", res.Expr)
require.Equal(t, "rate(ALERTS{job=\"test\" [120000]}) + rate(ALERTS{job=\"test\" [2m]})", res.Expr)
})
t.Run("parsing query model with ${__interval_ms} and ${__interval} variable", func(t *testing.T) {
@ -225,7 +225,7 @@ func TestParse(t *testing.T) {
res, err := models.Parse(q, "15s", intervalCalculator, false)
require.NoError(t, err)
require.Equal(t, "rate(ALERTS{job=\"test\" [60000]}) + rate(ALERTS{job=\"test\" [1m]})", res.Expr)
require.Equal(t, "rate(ALERTS{job=\"test\" [120000]}) + rate(ALERTS{job=\"test\" [2m]})", res.Expr)
})
t.Run("parsing query model with $__range variable", func(t *testing.T) {