2021-04-05 17:05:39 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-04-13 16:38:09 -05:00
|
|
|
"fmt"
|
2021-04-05 17:05:39 -05:00
|
|
|
"net/http"
|
2021-04-13 16:38:09 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
|
|
|
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2021-04-05 17:05:39 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-04-05 17:05:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PrometheusSrv struct {
|
|
|
|
log log.Logger
|
|
|
|
stateTracker *state.StateTracker
|
2021-04-13 16:38:09 -05:00
|
|
|
store store.RuleStore
|
2021-04-05 17:05:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv PrometheusSrv) RouteGetAlertStatuses(c *models.ReqContext) response.Response {
|
|
|
|
alertResponse := apimodels.AlertResponse{
|
|
|
|
DiscoveryBase: apimodels.DiscoveryBase{
|
|
|
|
Status: "success",
|
|
|
|
},
|
|
|
|
Data: apimodels.AlertDiscovery{
|
|
|
|
Alerts: []*apimodels.Alert{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, alertState := range srv.stateTracker.GetAll() {
|
|
|
|
startsAt := alertState.StartsAt
|
|
|
|
alertResponse.Data.Alerts = append(alertResponse.Data.Alerts, &apimodels.Alert{
|
|
|
|
Labels: map[string]string(alertState.Labels),
|
|
|
|
Annotations: map[string]string{}, //TODO: Once annotations are added to the evaluation result, set them here
|
|
|
|
State: alertState.State.String(),
|
|
|
|
ActiveAt: &startsAt,
|
|
|
|
Value: "", //TODO: once the result of the evaluation is added to the evaluation result, set it here
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, alertResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Response {
|
2021-04-13 16:38:09 -05:00
|
|
|
ruleResponse := apimodels.RuleResponse{
|
|
|
|
DiscoveryBase: apimodels.DiscoveryBase{
|
|
|
|
Status: "success",
|
|
|
|
},
|
|
|
|
Data: apimodels.RuleDiscovery{},
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleGroupQuery := ngmodels.ListOrgRuleGroupsQuery{
|
|
|
|
OrgID: c.SignedInUser.OrgId,
|
|
|
|
}
|
|
|
|
if err := srv.store.GetOrgRuleGroups(&ruleGroupQuery); err != nil {
|
|
|
|
ruleResponse.DiscoveryBase.Status = "error"
|
|
|
|
ruleResponse.DiscoveryBase.Error = fmt.Sprintf("failure getting rule groups: %s", err.Error())
|
|
|
|
ruleResponse.DiscoveryBase.ErrorType = apiv1.ErrServer
|
|
|
|
return response.JSON(http.StatusInternalServerError, ruleResponse)
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:49:26 -05:00
|
|
|
for _, r := range ruleGroupQuery.Result {
|
|
|
|
if len(r) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupId, namespaceUID, namespace := r[0], r[1], r[2]
|
|
|
|
alertRuleQuery := ngmodels.ListRuleGroupAlertRulesQuery{OrgID: c.SignedInUser.OrgId, NamespaceUID: namespaceUID, RuleGroup: groupId}
|
2021-04-13 16:38:09 -05:00
|
|
|
if err := srv.store.GetRuleGroupAlertRules(&alertRuleQuery); err != nil {
|
|
|
|
ruleResponse.DiscoveryBase.Status = "error"
|
|
|
|
ruleResponse.DiscoveryBase.Error = fmt.Sprintf("failure getting rules for group %s: %s", groupId, err.Error())
|
|
|
|
ruleResponse.DiscoveryBase.ErrorType = apiv1.ErrServer
|
|
|
|
return response.JSON(http.StatusInternalServerError, ruleResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
newGroup := &apimodels.RuleGroup{
|
2021-04-14 12:49:26 -05:00
|
|
|
Name: groupId,
|
|
|
|
// This doesn't make sense in our architecture
|
2021-04-15 15:23:16 -05:00
|
|
|
// so we use this field for passing to the frontend the namespace
|
2021-04-14 12:49:26 -05:00
|
|
|
File: namespace,
|
2021-04-13 16:38:09 -05:00
|
|
|
LastEvaluation: time.Time{},
|
|
|
|
EvaluationTime: 0, // TODO: see if we are able to pass this along with evaluation results
|
|
|
|
}
|
|
|
|
for _, rule := range alertRuleQuery.Result {
|
|
|
|
instanceQuery := ngmodels.ListAlertInstancesQuery{
|
|
|
|
DefinitionOrgID: c.SignedInUser.OrgId,
|
|
|
|
DefinitionUID: rule.UID,
|
|
|
|
}
|
|
|
|
if err := srv.store.ListAlertInstances(&instanceQuery); err != nil {
|
|
|
|
ruleResponse.DiscoveryBase.Status = "error"
|
|
|
|
ruleResponse.DiscoveryBase.Error = fmt.Sprintf("failure getting alerts for rule %s: %s", rule.UID, err.Error())
|
|
|
|
ruleResponse.DiscoveryBase.ErrorType = apiv1.ErrServer
|
|
|
|
return response.JSON(http.StatusInternalServerError, ruleResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
alertingRule := apimodels.AlertingRule{
|
|
|
|
State: "inactive",
|
|
|
|
Name: rule.Title,
|
2021-04-15 15:23:16 -05:00
|
|
|
Query: rule.DataToString(), // TODO: don't escape <>& etc
|
2021-04-15 07:54:37 -05:00
|
|
|
Duration: rule.For.Seconds(),
|
2021-04-13 16:38:09 -05:00
|
|
|
Annotations: rule.Annotations,
|
|
|
|
}
|
|
|
|
|
|
|
|
newRule := apimodels.Rule{
|
|
|
|
Name: rule.Title,
|
|
|
|
Labels: nil, // TODO: NG AlertRule does not have labels but does have annotations
|
|
|
|
Health: "ok", // TODO: update this in the future when error and noData states are being evaluated and set
|
|
|
|
Type: apiv1.RuleTypeAlerting,
|
|
|
|
LastEvaluation: time.Time{}, // TODO: set this to be rule evaluation time once it is being set
|
|
|
|
EvaluationTime: 0, // TODO: set this once we are saving it or adding it to evaluation results
|
|
|
|
}
|
|
|
|
for _, instance := range instanceQuery.Result {
|
|
|
|
activeAt := instance.CurrentStateSince
|
|
|
|
alert := &apimodels.Alert{
|
|
|
|
Labels: map[string]string(instance.Labels),
|
|
|
|
Annotations: nil, // TODO: set these once they are added to evaluation results
|
|
|
|
State: translateInstanceState(instance.CurrentState),
|
|
|
|
ActiveAt: &activeAt,
|
|
|
|
Value: "", // TODO: set this once it is added to the evaluation results
|
|
|
|
}
|
|
|
|
if instance.LastEvalTime.After(newRule.LastEvaluation) {
|
|
|
|
newRule.LastEvaluation = instance.LastEvalTime
|
|
|
|
newGroup.LastEvaluation = instance.LastEvalTime
|
|
|
|
}
|
|
|
|
switch alert.State {
|
|
|
|
case "pending":
|
|
|
|
if alertingRule.State == "inactive" {
|
|
|
|
alertingRule.State = "pending"
|
|
|
|
}
|
|
|
|
case "firing":
|
|
|
|
alertingRule.State = "firing"
|
|
|
|
}
|
|
|
|
|
|
|
|
alertingRule.Alerts = append(alertingRule.Alerts, alert)
|
|
|
|
}
|
|
|
|
alertingRule.Rule = newRule
|
|
|
|
newGroup.Rules = append(newGroup.Rules, alertingRule)
|
|
|
|
newGroup.Interval = float64(rule.IntervalSeconds)
|
|
|
|
}
|
|
|
|
ruleResponse.Data.RuleGroups = append(ruleResponse.Data.RuleGroups, newGroup)
|
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, ruleResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateInstanceState(state ngmodels.InstanceStateType) string {
|
|
|
|
switch {
|
|
|
|
case state == ngmodels.InstanceStateFiring:
|
|
|
|
return "firing"
|
|
|
|
case state == ngmodels.InstanceStateNormal:
|
|
|
|
return "inactive"
|
|
|
|
case state == ngmodels.InstanceStatePending:
|
|
|
|
return "pending"
|
|
|
|
default:
|
|
|
|
return "inactive"
|
|
|
|
}
|
2021-04-05 17:05:39 -05:00
|
|
|
}
|