AlertingAPI: Change list response query prop (#33419)

* Alerting: change to full []AlertQuery as json in a string and not just model.
This commit is contained in:
Kyle Brandt 2021-04-27 16:15:00 -04:00 committed by GitHub
parent a13c5380a7
commit b590e95682
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View File

@ -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,
}

View File

@ -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

View File

@ -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": "",