From b590e95682205639c57e68505b0704a406b38dc1 Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Tue, 27 Apr 2021 16:15:00 -0400 Subject: [PATCH] AlertingAPI: Change list response query prop (#33419) * Alerting: change to full []AlertQuery as json in a string and not just model. --- pkg/services/ngalert/api/api_prometheus.go | 10 +++++++++- pkg/services/ngalert/models/alert_rule.go | 12 ------------ pkg/tests/api/alerting/api_prometheus_test.go | 13 +++++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/pkg/services/ngalert/api/api_prometheus.go b/pkg/services/ngalert/api/api_prometheus.go index b5d40e32046..4a0d5f2955d 100644 --- a/pkg/services/ngalert/api/api_prometheus.go +++ b/pkg/services/ngalert/api/api_prometheus.go @@ -1,6 +1,7 @@ package api import ( + "encoding/json" "fmt" "net/http" "time" @@ -90,10 +91,17 @@ func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Res stateMap := srv.manager.GetStatesByRuleUID() for _, rule := range alertRuleQuery.Result { + var queryStr string + encodedQuery, err := json.Marshal(rule.Data) + if err != nil { + queryStr = err.Error() + } else { + queryStr = string(encodedQuery) + } alertingRule := apimodels.AlertingRule{ State: "inactive", Name: rule.Title, - Query: rule.DataToString(), // TODO: don't escape <>& etc + Query: queryStr, // TODO: don't escape <>& etc Duration: rule.For.Seconds(), Annotations: rule.Annotations, } diff --git a/pkg/services/ngalert/models/alert_rule.go b/pkg/services/ngalert/models/alert_rule.go index 52e0c72a1ab..40af4885460 100644 --- a/pkg/services/ngalert/models/alert_rule.go +++ b/pkg/services/ngalert/models/alert_rule.go @@ -70,18 +70,6 @@ type AlertRule struct { Labels map[string]string } -func (alertRule *AlertRule) DataToString() string { - response := "[" - for i, part := range alertRule.Data { - response += string(part.Model) - if i < len(alertRule.Data)-1 { - response += "," - } - } - response += "]" - return response -} - // AlertRuleKey is the alert definition identifier type AlertRuleKey struct { OrgID int64 diff --git a/pkg/tests/api/alerting/api_prometheus_test.go b/pkg/tests/api/alerting/api_prometheus_test.go index b23e8467b2e..027c531cbce 100644 --- a/pkg/tests/api/alerting/api_prometheus_test.go +++ b/pkg/tests/api/alerting/api_prometheus_test.go @@ -71,8 +71,8 @@ func TestPrometheusRules(t *testing.T) { From: ngmodels.Duration(time.Duration(5) * time.Hour), To: ngmodels.Duration(time.Duration(3) * time.Hour), }, + DatasourceUID: "-100", Model: json.RawMessage(`{ - "datasourceUid": "-100", "type": "math", "expression": "2 + 3 > 1" }`), @@ -91,8 +91,8 @@ func TestPrometheusRules(t *testing.T) { From: ngmodels.Duration(time.Duration(5) * time.Hour), To: ngmodels.Duration(time.Duration(3) * time.Hour), }, + DatasourceUID: "-100", Model: json.RawMessage(`{ - "datasourceUid": "-100", "type": "math", "expression": "2 + 3 > 1" }`), @@ -137,6 +137,7 @@ func TestPrometheusRules(t *testing.T) { b, err := ioutil.ReadAll(resp.Body) require.NoError(t, err) require.Equal(t, 200, resp.StatusCode) + require.JSONEq(t, ` { "status": "success", @@ -147,7 +148,7 @@ func TestPrometheusRules(t *testing.T) { "rules": [{ "state": "inactive", "name": "AlwaysFiring", - "query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]", + "query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]", "duration": 10, "annotations": { "annotation1": "val1" @@ -163,7 +164,7 @@ func TestPrometheusRules(t *testing.T) { }, { "state": "inactive", "name": "AlwaysFiringButSilenced", - "query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]", + "query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]", "labels": null, "health": "ok", "lastError": "", @@ -202,7 +203,7 @@ func TestPrometheusRules(t *testing.T) { "rules": [{ "state": "inactive", "name": "AlwaysFiring", - "query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]", + "query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]", "duration": 10, "annotations": { "annotation1": "val1" @@ -218,7 +219,7 @@ func TestPrometheusRules(t *testing.T) { }, { "state": "inactive", "name": "AlwaysFiringButSilenced", - "query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]", + "query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]", "labels": null, "health": "ok", "lastError": "",