Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alerting: Encode query model map to string in rule export to avoid html escape sequences #87663

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion pkg/services/ngalert/api/api_ruler_export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"context"
"embed"
"encoding/json"
Expand Down Expand Up @@ -42,8 +43,12 @@ func TestExportFromPayload(t *testing.T) {
requestFile := "post-rulegroup-101.json"
rawBody, err := testData.ReadFile(path.Join("test-data", requestFile))
require.NoError(t, err)
// compact the json to remove any extra whitespace
var buf bytes.Buffer
require.NoError(t, json.Compact(&buf, rawBody))
// unmarshal the compacted json
var body apimodels.PostableRuleGroupConfig
require.NoError(t, json.Unmarshal(rawBody, &body))
require.NoError(t, json.Unmarshal(buf.Bytes(), &body))

createRequest := func() *contextmodel.ReqContext {
return createRequestContextWithPerms(orgID, map[int64]map[string][]string{}, nil)
Expand Down Expand Up @@ -212,6 +217,13 @@ func TestExportRules(t *testing.T) {
gen := ngmodels.RuleGen
accessQuery := gen.GenerateQuery()
noAccessQuery := gen.GenerateQuery()
mdl := map[string]any{
"foo": "bar",
"baz": "a <=> b", // explicitly check greater/less than characters
}
model, err := json.Marshal(mdl)
require.NoError(t, err)
accessQuery.Model = model

hasAccess1 := gen.With(gen.WithGroupKey(hasAccessKey1), gen.WithQuery(accessQuery), gen.WithUniqueGroupIndex()).GenerateManyRef(5)
ruleStore.PutRule(context.Background(), hasAccess1...)
Expand Down
20 changes: 19 additions & 1 deletion pkg/services/ngalert/api/compat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"encoding/json"
"time"

Expand Down Expand Up @@ -204,6 +205,17 @@ func AlertRuleExportFromAlertRule(rule models.AlertRule) (definitions.AlertRuleE
return result, nil
}

func encodeQueryModel(m map[string]any) (string, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(m)
if err != nil {
return "", err
}
return string(bytes.TrimRight(buf.Bytes(), "\n")), nil
}

// AlertQueryExportFromAlertQuery creates a definitions.AlertQueryExport DTO from models.AlertQuery.
func AlertQueryExportFromAlertQuery(query models.AlertQuery) (definitions.AlertQueryExport, error) {
// We unmarshal the json.RawMessage model into a map in order to facilitate yaml marshalling.
Expand All @@ -216,6 +228,12 @@ func AlertQueryExportFromAlertQuery(query models.AlertQuery) (definitions.AlertQ
if query.QueryType != "" {
queryType = &query.QueryType
}

modelString, err := encodeQueryModel(mdl)
if err != nil {
return definitions.AlertQueryExport{}, err
}

return definitions.AlertQueryExport{
RefID: query.RefID,
QueryType: queryType,
Expand All @@ -225,7 +243,7 @@ func AlertQueryExportFromAlertQuery(query models.AlertQuery) (definitions.AlertQ
},
DatasourceUID: query.DatasourceUID,
Model: mdl,
ModelString: string(query.Model),
ModelString: modelString,
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ resource "grafana_rule_group" "rule_group_0000" {
}

datasource_uid = "000000002"
model = "{\n \"expr\": \"http_request_duration_microseconds_count\",\n \"hide\": false,\n \"interval\": \"\",\n \"intervalMs\": 1000,\n \"legendFormat\": \"\",\n \"maxDataPoints\": 100,\n \"refId\": \"query\"\n }"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's possible to keep the result the same. Changes in export could cause diff downstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this also removes the noise coming from the extra whitespace and newlines, I'm of the opinion that it's not worth spending time working to keep the current output. WDYT?

That being said if we feel like consistency is more important I would have to imagine there's a way to maintain the formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was determined this was a quirk of the test. I've pushed up a change that compacts the json to make the behavior clearer, but in any case rules previously exported like this should not contain those whitespace characters.

model = "{\"expr\":\"http_request_duration_microseconds_count\",\"hide\":false,\"interval\":\"\",\"intervalMs\":1000,\"legendFormat\":\"\",\"maxDataPoints\":100,\"refId\":\"query\"}"
}
data {
ref_id = "reduced"
Expand All @@ -28,7 +28,7 @@ resource "grafana_rule_group" "rule_group_0000" {
}

datasource_uid = "__expr__"
model = "{\n \"expression\": \"query\",\n \"hide\": false,\n \"intervalMs\": 1000,\n \"maxDataPoints\": 100,\n \"reducer\": \"mean\",\n \"refId\": \"reduced\",\n \"type\": \"reduce\"\n }"
model = "{\"expression\":\"query\",\"hide\":false,\"intervalMs\":1000,\"maxDataPoints\":100,\"reducer\":\"mean\",\"refId\":\"reduced\",\"type\":\"reduce\"}"
}
data {
ref_id = "condition"
Expand All @@ -39,7 +39,7 @@ resource "grafana_rule_group" "rule_group_0000" {
}

datasource_uid = "__expr__"
model = "{\n \"expression\": \"$reduced > 10\",\n \"hide\": false,\n \"intervalMs\": 1000,\n \"maxDataPoints\": 100,\n \"refId\": \"condition\",\n \"type\": \"math\"\n }"
model = "{\"expression\":\"$reduced > 10\",\"hide\":false,\"intervalMs\":1000,\"maxDataPoints\":100,\"refId\":\"condition\",\"type\":\"math\"}"
}

no_data_state = "NoData"
Expand All @@ -60,7 +60,7 @@ resource "grafana_rule_group" "rule_group_0000" {
}

datasource_uid = "000000004"
model = "{\n \"alias\": \"just-testing\",\n \"intervalMs\": 1000,\n \"maxDataPoints\": 100,\n \"orgId\": 0,\n \"refId\": \"A\",\n \"scenarioId\": \"csv_metric_values\",\n \"stringInput\": \"1,20,90,30,5,0\"\n }"
model = "{\"alias\":\"just-testing\",\"intervalMs\":1000,\"maxDataPoints\":100,\"orgId\":0,\"refId\":\"A\",\"scenarioId\":\"csv_metric_values\",\"stringInput\":\"1,20,90,30,5,0\"}"
}
data {
ref_id = "B"
Expand All @@ -71,7 +71,7 @@ resource "grafana_rule_group" "rule_group_0000" {
}

datasource_uid = "__expr__"
model = "{\n \"expression\": \"$A\",\n \"intervalMs\": 2000,\n \"maxDataPoints\": 200,\n \"orgId\": 0,\n \"reducer\": \"mean\",\n \"refId\": \"B\",\n \"type\": \"reduce\"\n }"
model = "{\"expression\":\"$A\",\"intervalMs\":2000,\"maxDataPoints\":200,\"orgId\":0,\"reducer\":\"mean\",\"refId\":\"B\",\"type\":\"reduce\"}"
}

no_data_state = "NoData"
Expand Down