Alerting: Use consts from alerting package (#61241)

This commit is contained in:
George Robinson 2023-01-10 19:59:13 +00:00 committed by GitHub
parent 2505f112f5
commit 2a291afbae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 35 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
@ -525,9 +526,9 @@ func generateRuleAndInstanceWithQuery(t *testing.T, orgID int64, fakeAIM *fakeAl
fakeAIM.GenerateAlertInstances(orgID, rules[0].UID, 1, func(s *state.State) *state.State {
s.Labels = data.Labels{
"job": "prometheus",
ngmodels.NamespaceUIDLabel: "test_namespace_uid",
ngmodels.RuleUIDLabel: "test_alert_rule_uid_0",
"job": "prometheus",
alertingModels.NamespaceUIDLabel: "test_namespace_uid",
alertingModels.RuleUIDLabel: "test_alert_rule_uid_0",
}
s.Annotations = data.Labels{"severity": "critical"}
return s
@ -548,7 +549,7 @@ func asFixture() func(r *ngmodels.AlertRule) {
r.UID = "RuleUID"
r.Labels = map[string]string{
"__a_private_label_on_the_rule__": "a_value",
ngmodels.RuleUIDLabel: "RuleUID",
alertingModels.RuleUIDLabel: "RuleUID",
}
r.Annotations = nil
r.IntervalSeconds = 60

View File

@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/services/quota"
"github.com/grafana/grafana/pkg/util/cmputil"
)
@ -89,17 +90,9 @@ const (
)
const (
RuleUIDLabel = "__alert_rule_uid__"
NamespaceUIDLabel = "__alert_rule_namespace_uid__"
// Annotations are actually a set of labels, so technically this is the label name of an annotation.
DashboardUIDAnnotation = "__dashboardUid__"
PanelIDAnnotation = "__panelId__"
OrgIDAnnotation = "__orgId__"
// This isn't a hard-coded secret token, hence the nolint.
//nolint:gosec
ImageTokenAnnotation = "__alertImageToken__"
// GrafanaReservedLabelPrefix contains the prefix for Grafana reserved labels. These differ from "__<label>__" labels
// in that they are not meant for internal-use only and will be passed-through to AMs and available to users in the same
@ -111,9 +104,6 @@ const (
// StateReasonAnnotation is the name of the annotation that explains the difference between evaluation state and alert state (i.e. changing state when NoData or Error).
StateReasonAnnotation = GrafanaReservedLabelPrefix + "state_reason"
ValuesAnnotation = "__values__"
ValueStringAnnotation = "__value_string__"
)
const (
@ -124,13 +114,13 @@ const (
var (
// InternalLabelNameSet are labels that grafana automatically include as part of the labelset.
InternalLabelNameSet = map[string]struct{}{
RuleUIDLabel: {},
NamespaceUIDLabel: {},
alertingModels.RuleUIDLabel: {},
alertingModels.NamespaceUIDLabel: {},
}
InternalAnnotationNameSet = map[string]struct{}{
DashboardUIDAnnotation: {},
PanelIDAnnotation: {},
ImageTokenAnnotation: {},
DashboardUIDAnnotation: {},
PanelIDAnnotation: {},
alertingModels.ImageTokenAnnotation: {},
}
)

View File

@ -34,6 +34,7 @@ import (
pb "github.com/prometheus/alertmanager/silence/silencepb"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/infra/kvstore"
"github.com/grafana/grafana/pkg/infra/log"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
@ -557,7 +558,7 @@ func (am *Alertmanager) PutAlerts(postableAlerts apimodels.PostableAlerts) error
}
for k, v := range a.Labels {
if len(v) == 0 || k == ngmodels.NamespaceUIDLabel { // Skip empty and namespace UID labels.
if len(v) == 0 || k == alertingModels.NamespaceUIDLabel { // Skip empty and namespace UID labels.
continue
}
alert.Alert.Labels[model.LabelName(k)] = model.LabelValue(v)

View File

@ -14,6 +14,7 @@ import (
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/common/model"
alertingModels "github.com/grafana/alerting/alerting/models"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
ngModels "github.com/grafana/grafana/pkg/services/ngalert/models"
@ -40,28 +41,28 @@ func stateToPostableAlert(alertState *state.State, appURL *url.URL) *models.Post
// encode the values as JSON where it will be expanded later
if len(alertState.Values) > 0 {
if b, err := json.Marshal(alertState.Values); err == nil {
nA[ngModels.ValuesAnnotation] = string(b)
nA[alertingModels.ValuesAnnotation] = string(b)
}
}
if alertState.LastEvaluationString != "" {
nA[ngModels.ValueStringAnnotation] = alertState.LastEvaluationString
nA[alertingModels.ValueStringAnnotation] = alertState.LastEvaluationString
}
if alertState.Image != nil {
nA[ngModels.ImageTokenAnnotation] = alertState.Image.Token
nA[alertingModels.ImageTokenAnnotation] = alertState.Image.Token
}
if alertState.StateReason != "" {
nA[ngModels.StateReasonAnnotation] = alertState.StateReason
nA[alertingModels.StateReasonAnnotation] = alertState.StateReason
}
if alertState.OrgID != 0 {
nA[ngModels.OrgIDAnnotation] = strconv.FormatInt(alertState.OrgID, 10)
nA[alertingModels.OrgIDAnnotation] = strconv.FormatInt(alertState.OrgID, 10)
}
var urlStr string
if uid := nL[ngModels.RuleUIDLabel]; len(uid) > 0 && appURL != nil {
if uid := nL[alertingModels.RuleUIDLabel]; len(uid) > 0 && appURL != nil {
u := *appURL
u.Path = path.Join(u.Path, fmt.Sprintf("/alerting/grafana/%s/view", uid))
urlStr = u.String()

View File

@ -13,6 +13,7 @@ import (
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
ngModels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/state"
@ -57,7 +58,7 @@ func Test_stateToPostableAlert(t *testing.T) {
t.Run("it generates proper URL", func(t *testing.T) {
t.Run("to alert rule", func(t *testing.T) {
alertState := randomState(tc.state)
alertState.Labels[ngModels.RuleUIDLabel] = alertState.AlertRuleUID
alertState.Labels[alertingModels.RuleUIDLabel] = alertState.AlertRuleUID
result := stateToPostableAlert(alertState, appURL)
u := *appURL
u.Path = u.Path + "/alerting/grafana/" + alertState.AlertRuleUID + "/view"
@ -66,18 +67,18 @@ func Test_stateToPostableAlert(t *testing.T) {
t.Run("app URL as is if rule UID is not specified", func(t *testing.T) {
alertState := randomState(tc.state)
alertState.Labels[ngModels.RuleUIDLabel] = ""
alertState.Labels[alertingModels.RuleUIDLabel] = ""
result := stateToPostableAlert(alertState, appURL)
require.Equal(t, appURL.String(), result.Alert.GeneratorURL.String())
delete(alertState.Labels, ngModels.RuleUIDLabel)
delete(alertState.Labels, alertingModels.RuleUIDLabel)
result = stateToPostableAlert(alertState, appURL)
require.Equal(t, appURL.String(), result.Alert.GeneratorURL.String())
})
t.Run("empty string if app URL is not provided", func(t *testing.T) {
alertState := randomState(tc.state)
alertState.Labels[ngModels.RuleUIDLabel] = alertState.AlertRuleUID
alertState.Labels[alertingModels.RuleUIDLabel] = alertState.AlertRuleUID
result := stateToPostableAlert(alertState, nil)
require.Equal(t, "", result.Alert.GeneratorURL.String())
})

View File

@ -11,6 +11,7 @@ import (
prometheusModel "github.com/prometheus/common/model"
"go.opentelemetry.io/otel/attribute"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/datasources"
@ -517,9 +518,9 @@ func (sch *schedule) stopApplied(alertDefKey ngmodels.AlertRuleKey) {
func (sch *schedule) getRuleExtraLabels(evalCtx *evaluation) map[string]string {
extraLabels := make(map[string]string, 4)
extraLabels[ngmodels.NamespaceUIDLabel] = evalCtx.rule.NamespaceUID
extraLabels[alertingModels.NamespaceUIDLabel] = evalCtx.rule.NamespaceUID
extraLabels[prometheusModel.AlertNameLabel] = evalCtx.rule.Title
extraLabels[ngmodels.RuleUIDLabel] = evalCtx.rule.UID
extraLabels[alertingModels.RuleUIDLabel] = evalCtx.rule.UID
if !sch.disableGrafanaFolder {
extraLabels[ngmodels.FolderTitleLabel] = evalCtx.folderTitle

View File

@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
alertingModels "github.com/grafana/alerting/alerting/models"
"github.com/grafana/grafana/pkg/expr"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/plugins"
@ -243,8 +244,8 @@ func TestSchedule_ruleRoutine(t *testing.T) {
t.Run("it should add extra labels", func(t *testing.T) {
states := sch.stateManager.GetStatesForRuleUID(rule.OrgID, rule.UID)
for _, s := range states {
assert.Equal(t, rule.UID, s.Labels[models.RuleUIDLabel])
assert.Equal(t, rule.NamespaceUID, s.Labels[models.NamespaceUIDLabel])
assert.Equal(t, rule.UID, s.Labels[alertingModels.RuleUIDLabel])
assert.Equal(t, rule.NamespaceUID, s.Labels[alertingModels.NamespaceUIDLabel])
assert.Equal(t, rule.Title, s.Labels[prometheusModel.AlertNameLabel])
assert.Equal(t, folderTitle, s.Labels[models.FolderTitleLabel])
}