empty 'query' field, stop sending full query JSON

This commit is contained in:
Santiago Hernández
2025-12-15 12:06:35 +01:00
parent 099bf1dab4
commit 2733bac3c2
2 changed files with 9 additions and 12 deletions
@@ -1201,7 +1201,7 @@ func extractDatasourceUIDs(rule *ngmodels.AlertRule) []string {
} }
// ruleToQuery attempts to extract the datasource queries from the alert query model. // ruleToQuery attempts to extract the datasource queries from the alert query model.
// Returns the whole JSON model as a string if it fails to extract a minimum of 1 query. // Returns an empty string if it fails to extract a minimum of 1 query.
func ruleToQuery(logger log.Logger, rule *ngmodels.AlertRule) string { func ruleToQuery(logger log.Logger, rule *ngmodels.AlertRule) string {
var queryErr error var queryErr error
@@ -1229,17 +1229,8 @@ func ruleToQuery(logger log.Logger, rule *ngmodels.AlertRule) string {
return strings.Join(queries, " | ") return strings.Join(queries, " | ")
} }
return encodedQueriesOrError(rule.Data) // No queries extracted, return an empty string.
} return ""
// encodedQueriesOrError tries to encode rule query data into JSON if it fails returns the encoding error as a string.
func encodedQueriesOrError(rules []ngmodels.AlertQuery) string {
encodedQueries, err := json.Marshal(rules)
if err == nil {
return string(encodedQueries)
}
return err.Error()
} }
func errorOrEmpty(err error) string { func errorOrEmpty(err error) string {
@@ -110,6 +110,12 @@ func (aq *AlertQuery) String() string {
} }
func (aq *AlertQuery) setModelProps() error { func (aq *AlertQuery) setModelProps() error {
if aq.Model == nil {
// No data to extract, use an empty map.
aq.modelProps = map[string]any{}
return nil
}
aq.modelProps = make(map[string]any) aq.modelProps = make(map[string]any)
err := json.Unmarshal(aq.Model, &aq.modelProps) err := json.Unmarshal(aq.Model, &aq.modelProps)
if err != nil { if err != nil {