fix: skip first record only for rate metrics (#3609)

This commit is contained in:
Srikanth Chekuri 2023-09-22 15:43:21 +05:30 committed by GitHub
parent 5c437dd8f9
commit 043e5ca880
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -138,6 +138,24 @@ func (a AggregateOperator) RequireAttribute(dataSource DataSource) bool {
} }
} }
func (a AggregateOperator) IsRateOperator() bool {
switch a {
case AggregateOperatorRate,
AggregateOperatorSumRate,
AggregateOperatorAvgRate,
AggregateOperatorMinRate,
AggregateOperatorMaxRate,
AggregateOperatorRateSum,
AggregateOperatorRateAvg,
AggregateOperatorRateMin,
AggregateOperatorRateMax:
return true
default:
return false
}
}
type ReduceToOperator string type ReduceToOperator string
const ( const (

View File

@ -406,6 +406,16 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 {
} }
} }
func (r *ThresholdRule) shouldSkipFirstRecord() bool {
shouldSkip := false
for _, q := range r.ruleCondition.CompositeQuery.BuilderQueries {
if q.DataSource == v3.DataSourceMetrics && q.AggregateOperator.IsRateOperator() {
shouldSkip = true
}
}
return shouldSkip
}
// queryClickhouse runs actual query against clickhouse // queryClickhouse runs actual query against clickhouse
func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, query string) (Vector, error) { func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, query string) (Vector, error) {
rows, err := db.Query(ctx, query) rows, err := db.Query(ctx, query)
@ -553,7 +563,7 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer
// we skip the first record to support rate cases correctly // we skip the first record to support rate cases correctly
// improvement(amol): explore approaches to limit this only for // improvement(amol): explore approaches to limit this only for
// rate uses cases // rate uses cases
if exists := skipFirstRecord[labelHash]; exists { if exists := skipFirstRecord[labelHash]; exists || !r.shouldSkipFirstRecord() {
resultMap[labelHash] = sample resultMap[labelHash] = sample
} else { } else {
// looks like the first record for this label combo, skip it // looks like the first record for this label combo, skip it