You can now get alert rules for a dashboard or a panel using /api/v1/rules endpoints. (#39476)

Get alert rules for a dashboard and panel in /api/v1/rules
This commit is contained in:
George Robinson
2021-10-04 16:33:55 +01:00
committed by GitHub
parent a3783a4eb7
commit 2a4c1b1aa6
14 changed files with 918 additions and 17 deletions
+24 -1
View File
@@ -2,8 +2,11 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
@@ -51,6 +54,13 @@ func (srv PrometheusSrv) RouteGetAlertStatuses(c *models.ReqContext) response.Re
return response.JSON(http.StatusOK, alertResponse)
}
func getPanelIDFromRequest(r *http.Request) (int64, error) {
if s := strings.TrimSpace(r.URL.Query().Get("panel_id")); s != "" {
return strconv.ParseInt(s, 10, 64)
}
return 0, nil
}
func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Response {
ruleResponse := apimodels.RuleResponse{
DiscoveryBase: apimodels.DiscoveryBase{
@@ -71,9 +81,20 @@ func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Res
namespaceUIDs = append(namespaceUIDs, k)
}
dashboardUID := c.Query("dashboard_uid")
panelID, err := getPanelIDFromRequest(c.Req)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "invalid panel_id")
}
if dashboardUID == "" && panelID != 0 {
return ErrResp(http.StatusBadRequest, errors.New("panel_id must be set with dashboard_uid"), "")
}
ruleGroupQuery := ngmodels.ListOrgRuleGroupsQuery{
OrgID: c.SignedInUser.OrgId,
NamespaceUIDs: namespaceUIDs,
DashboardUID: dashboardUID,
PanelID: panelID,
}
if err := srv.store.GetOrgRuleGroups(&ruleGroupQuery); err != nil {
ruleResponse.DiscoveryBase.Status = "error"
@@ -83,7 +104,9 @@ func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Res
}
alertRuleQuery := ngmodels.ListAlertRulesQuery{
OrgID: c.SignedInUser.OrgId,
OrgID: c.SignedInUser.OrgId,
DashboardUID: dashboardUID,
PanelID: panelID,
}
if err := srv.store.GetOrgAlertRules(&alertRuleQuery); err != nil {
ruleResponse.DiscoveryBase.Status = "error"
+12 -1
View File
@@ -158,9 +158,20 @@ func (srv RulerSrv) RouteGetRulesConfig(c *models.ReqContext) response.Response
namespaceUIDs = append(namespaceUIDs, k)
}
dashboardUID := c.Query("dashboard_uid")
panelID, err := getPanelIDFromRequest(c.Req)
if err != nil {
return ErrResp(http.StatusBadRequest, err, "invalid panel_id")
}
if dashboardUID == "" && panelID != 0 {
return ErrResp(http.StatusBadRequest, errors.New("panel_id must be set with dashboard_uid"), "")
}
q := ngmodels.ListAlertRulesQuery{
OrgID: c.SignedInUser.OrgId,
NamespaceUIDs: namespaceUIDs,
DashboardUID: dashboardUID,
PanelID: panelID,
}
if err := srv.store.GetOrgAlertRules(&q); err != nil {
@@ -210,7 +221,7 @@ func (srv RulerSrv) RouteGetRulesConfig(c *models.ReqContext) response.Response
result[namespace] = append(result[namespace], ruleGroupConfig)
}
}
return response.JSON(http.StatusAccepted, result)
return response.JSON(http.StatusOK, result)
}
func (srv RulerSrv) RoutePostNameRulesConfig(c *models.ReqContext, ruleGroupConfig apimodels.PostableRuleGroupConfig) response.Response {
@@ -183,6 +183,14 @@ type GetSilencesParams struct {
Filter []string `json:"filter"`
}
// swagger:parameters RouteGetRuleStatuses
type GetRuleStatusesParams struct {
// in: query
DashboardUID string
// in: query
PanelID int64
}
// swagger:model
type GettableStatus struct {
// cluster
@@ -86,6 +86,14 @@ type PathRouleGroupConfig struct {
Groupname string
}
// swagger:parameters RouteGetRulesConfig
type PathGetRulesParams struct {
// in: query
DashboardUID string
// in: query
PanelID int64
}
// swagger:model
type RuleGroupConfigResponse struct {
GettableRuleGroupConfig
+24 -3
View File
@@ -2869,6 +2869,7 @@
"type": "array"
},
"gettableSilence": {
"description": "GettableSilence gettable silence",
"properties": {
"comment": {
"description": "comment",
@@ -2920,9 +2921,7 @@
"status",
"updatedAt"
],
"type": "object",
"x-go-name": "GettableSilence",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models"
"type": "object"
},
"gettableSilences": {
"description": "GettableSilences gettable silences",
@@ -3800,6 +3799,17 @@
"description": "gets the evaluation statuses of all rules",
"operationId": "RouteGetRuleStatuses",
"parameters": [
{
"in": "query",
"name": "DashboardUID",
"type": "string"
},
{
"format": "int64",
"in": "query",
"name": "PanelID",
"type": "integer"
},
{
"description": "Recipient should be \"grafana\" for requests to be handled by grafana\nand the numeric datasource id for requests to be forwarded to a datasource",
"in": "path",
@@ -3832,6 +3842,17 @@
"name": "Recipient",
"required": true,
"type": "string"
},
{
"in": "query",
"name": "DashboardUID",
"type": "string"
},
{
"format": "int64",
"in": "query",
"name": "PanelID",
"type": "integer"
}
],
"produces": [
+23 -2
View File
@@ -592,6 +592,17 @@
],
"operationId": "RouteGetRuleStatuses",
"parameters": [
{
"type": "string",
"name": "DashboardUID",
"in": "query"
},
{
"type": "integer",
"format": "int64",
"name": "PanelID",
"in": "query"
},
{
"type": "string",
"description": "Recipient should be \"grafana\" for requests to be handled by grafana\nand the numeric datasource id for requests to be forwarded to a datasource",
@@ -627,6 +638,17 @@
"name": "Recipient",
"in": "path",
"required": true
},
{
"type": "string",
"name": "DashboardUID",
"in": "query"
},
{
"type": "integer",
"format": "int64",
"name": "PanelID",
"in": "query"
}
],
"responses": {
@@ -3877,6 +3899,7 @@
"$ref": "#/definitions/gettableAlerts"
},
"gettableSilence": {
"description": "GettableSilence gettable silence",
"type": "object",
"required": [
"comment",
@@ -3929,8 +3952,6 @@
"x-go-name": "UpdatedAt"
}
},
"x-go-name": "GettableSilence",
"x-go-package": "github.com/prometheus/alertmanager/api/v2/models",
"$ref": "#/definitions/gettableSilence"
},
"gettableSilences": {