Alerting: Apply query optimization to eval endpoints (#78566)
* Alerting: Apply query optimization to eval endpoints Previously, query optimization was applied to alert queries when scheduled but not when ran through `api/v1/eval` or `/api/v1/rule/test/grafana`. This could lead to discrepancies between preview and scheduled alert results.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
@@ -448,6 +449,68 @@ func CreateClassicConditionExpression(refID string, inputRefID string, reducer s
|
||||
}
|
||||
}
|
||||
|
||||
func CreateReduceExpression(refID string, inputRefID string, reducer string) AlertQuery {
|
||||
return AlertQuery{
|
||||
RefID: refID,
|
||||
QueryType: expr.DatasourceType,
|
||||
DatasourceUID: expr.DatasourceUID,
|
||||
Model: json.RawMessage(fmt.Sprintf(`
|
||||
{
|
||||
"refId": "%[1]s",
|
||||
"hide": false,
|
||||
"type": "reduce",
|
||||
"expression": "%[2]s",
|
||||
"reducer": "%[3]s",
|
||||
"datasource": {
|
||||
"uid": "%[4]s",
|
||||
"type": "%[5]s"
|
||||
}
|
||||
}`, refID, inputRefID, reducer, expr.DatasourceUID, expr.DatasourceType)),
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePrometheusQuery(refID string, expr string, intervalMs int64, maxDataPoints int64, isInstant bool, datasourceUID string) AlertQuery {
|
||||
return AlertQuery{
|
||||
RefID: refID,
|
||||
QueryType: "",
|
||||
DatasourceUID: datasourceUID,
|
||||
Model: json.RawMessage(fmt.Sprintf(`
|
||||
{
|
||||
"refId": "%[1]s",
|
||||
"expr": "%[2]s",
|
||||
"intervalMs": %[3]d,
|
||||
"maxDataPoints": %[4]d,
|
||||
"exemplar": false,
|
||||
"instant": %[5]t,
|
||||
"range": %[6]t,
|
||||
"datasource": {
|
||||
"uid": "%[7]s",
|
||||
"type": "%[8]s"
|
||||
}
|
||||
}`, refID, expr, intervalMs, maxDataPoints, isInstant, !isInstant, datasourceUID, datasources.DS_PROMETHEUS)),
|
||||
}
|
||||
}
|
||||
|
||||
func CreateLokiQuery(refID string, expr string, intervalMs int64, maxDataPoints int64, queryType string, datasourceUID string) AlertQuery {
|
||||
return AlertQuery{
|
||||
RefID: refID,
|
||||
QueryType: queryType,
|
||||
DatasourceUID: datasourceUID,
|
||||
Model: json.RawMessage(fmt.Sprintf(`
|
||||
{
|
||||
"refId": "%[1]s",
|
||||
"expr": "%[2]s",
|
||||
"intervalMs": %[3]d,
|
||||
"maxDataPoints": %[4]d,
|
||||
"queryType": "%[5]s",
|
||||
"datasource": {
|
||||
"uid": "%[6]s",
|
||||
"type": "%[7]s"
|
||||
}
|
||||
}`, refID, expr, intervalMs, maxDataPoints, queryType, datasourceUID, datasources.DS_LOKI)),
|
||||
}
|
||||
}
|
||||
|
||||
type AlertInstanceMutator func(*AlertInstance)
|
||||
|
||||
// AlertInstanceGen provides a factory function that generates a random AlertInstance.
|
||||
|
||||
Reference in New Issue
Block a user