2023-10-17 04:41:25 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/expr"
|
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
grafanaCloudProm = "grafanacloud-prom"
|
|
|
|
grafanaCloudUsage = "grafanacloud-usage"
|
|
|
|
)
|
|
|
|
|
2023-11-28 18:44:28 -06:00
|
|
|
// OptimizeAlertQueries was added to mitigate the high load that could be created by loki range queries.
|
|
|
|
// In previous versions of Grafana, Loki datasources would default to range queries
|
|
|
|
// instead of instant queries, sometimes creating unnecessary load. This is only
|
|
|
|
// done for Grafana Cloud.
|
|
|
|
func OptimizeAlertQueries(queries []models.AlertQuery) ([]Optimization, error) {
|
|
|
|
if optimizations, migratable := canBeInstant(queries); migratable {
|
|
|
|
err := migrateToInstant(queries, optimizations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return optimizations, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-10-17 04:41:25 -05:00
|
|
|
// DSType can be used to check the datasource type if it's set in the model.
|
|
|
|
type dsType struct {
|
|
|
|
DS struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
} `json:"datasource"`
|
|
|
|
Range bool `json:"range"`
|
|
|
|
}
|
|
|
|
|
2023-11-28 18:44:28 -06:00
|
|
|
type Optimization struct {
|
|
|
|
// RefID of the query that can be optimized
|
|
|
|
RefID string
|
2023-10-17 04:41:25 -05:00
|
|
|
// Index of the query that can be optimized
|
|
|
|
i int
|
|
|
|
// Type of the query that ca be optimized (loki,prometheus)
|
|
|
|
t string
|
|
|
|
}
|
|
|
|
|
|
|
|
// canBeInstant checks if any of the query nodes that are loki or prometheus range queries can be migrated to instant queries.
|
|
|
|
// If any are migratable, those indices are returned.
|
2023-11-28 18:44:28 -06:00
|
|
|
func canBeInstant(queries []models.AlertQuery) ([]Optimization, bool) {
|
|
|
|
if len(queries) < 2 {
|
2023-10-17 04:41:25 -05:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
var (
|
2023-11-28 18:44:28 -06:00
|
|
|
optimizableIndices []Optimization
|
2023-10-17 04:41:25 -05:00
|
|
|
canBeOptimized = false
|
|
|
|
)
|
|
|
|
// Loop over query nodes to find all range queries.
|
2023-11-28 18:44:28 -06:00
|
|
|
for i := range queries {
|
2023-10-17 04:41:25 -05:00
|
|
|
var t dsType
|
|
|
|
// We can ignore the error here, the query just won't be optimized.
|
2023-11-28 18:44:28 -06:00
|
|
|
_ = json.Unmarshal(queries[i].Model, &t)
|
2023-10-17 04:41:25 -05:00
|
|
|
|
|
|
|
switch t.DS.Type {
|
|
|
|
case datasources.DS_PROMETHEUS:
|
|
|
|
if !t.Range {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case datasources.DS_LOKI:
|
2023-11-28 18:44:28 -06:00
|
|
|
if queries[i].QueryType != "range" {
|
2023-10-17 04:41:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// The default datasource is not saved as datasource, this is why we need to check for the datasource name.
|
|
|
|
// Here we check the well-known grafana cloud datasources.
|
2023-11-28 18:44:28 -06:00
|
|
|
if queries[i].DatasourceUID != grafanaCloudProm && queries[i].DatasourceUID != grafanaCloudUsage {
|
2023-10-17 04:41:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !t.Range {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.DS.Type = datasources.DS_PROMETHEUS
|
|
|
|
}
|
|
|
|
|
|
|
|
var validReducers bool
|
|
|
|
// Loop over all query nodes to find the reduce node.
|
2023-11-28 18:44:28 -06:00
|
|
|
for ii := range queries {
|
2023-10-17 04:41:25 -05:00
|
|
|
// Second query part should be and expression.
|
2023-11-28 18:44:28 -06:00
|
|
|
if !expr.IsDataSource(queries[ii].DatasourceUID) {
|
2023-10-17 04:41:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
exprRaw := make(map[string]any)
|
2023-11-28 18:44:28 -06:00
|
|
|
if err := json.Unmarshal(queries[ii].Model, &exprRaw); err != nil {
|
2023-10-17 04:41:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Second query part should use first query part as expression.
|
2023-11-28 18:44:28 -06:00
|
|
|
if ref, ok := exprRaw["expression"].(string); !ok || ref != queries[i].RefID {
|
2023-10-17 04:41:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Second query part should be "last()"
|
|
|
|
if val, ok := exprRaw["reducer"].(string); !ok || val != "last" {
|
|
|
|
validReducers = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
validReducers = true
|
|
|
|
}
|
|
|
|
// If we found a reduce node that uses last, we can add the query to the optimizations.
|
|
|
|
if validReducers {
|
|
|
|
canBeOptimized = true
|
2023-11-28 18:44:28 -06:00
|
|
|
optimizableIndices = append(optimizableIndices, Optimization{
|
|
|
|
RefID: queries[i].RefID,
|
|
|
|
i: i,
|
|
|
|
t: t.DS.Type,
|
2023-10-17 04:41:25 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optimizableIndices, canBeOptimized
|
|
|
|
}
|
|
|
|
|
|
|
|
// migrateToInstant will move the provided indices from a range-query to an instant query.
|
2023-11-28 18:44:28 -06:00
|
|
|
func migrateToInstant(queries []models.AlertQuery, optimizations []Optimization) error {
|
2023-10-17 04:41:25 -05:00
|
|
|
for _, opti := range optimizations {
|
|
|
|
modelRaw := make(map[string]any)
|
2023-11-28 18:44:28 -06:00
|
|
|
if err := json.Unmarshal(queries[opti.i].Model, &modelRaw); err != nil {
|
2023-10-17 04:41:25 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch opti.t {
|
|
|
|
case datasources.DS_PROMETHEUS:
|
|
|
|
modelRaw["instant"] = true
|
|
|
|
modelRaw["range"] = false
|
|
|
|
model, err := json.Marshal(modelRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-28 18:44:28 -06:00
|
|
|
queries[opti.i].Model = model
|
2023-10-17 04:41:25 -05:00
|
|
|
case datasources.DS_LOKI:
|
|
|
|
modelRaw["queryType"] = "instant"
|
|
|
|
model, err := json.Marshal(modelRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-28 18:44:28 -06:00
|
|
|
queries[opti.i].Model = model
|
|
|
|
queries[opti.i].QueryType = "instant"
|
2023-10-17 04:41:25 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("optimization for datasource of type %s not possible", opti.t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|