Alerting: Store instance annotations in alert rule state (#114975)

Alerting: Store annotations in alert instance state
This commit is contained in:
Alexander Akhmetov
2025-12-09 13:52:42 +01:00
committed by GitHub
parent 6746c978b4
commit c59d5d1c8e
17 changed files with 258 additions and 109 deletions
@@ -19,6 +19,7 @@ func TestAlertInstanceModelToProto(t *testing.T) {
lastSentAt := currentStateSince.Add(-2 * time.Minute)
firedAt := currentStateSince.Add(-2 * time.Minute)
resolvedAt := currentStateSince.Add(-3 * time.Minute)
annotations := map[string]string{"summary": "value", "team": "alerting"}
tests := []struct {
name string
@@ -28,7 +29,8 @@ func TestAlertInstanceModelToProto(t *testing.T) {
{
name: "valid instance",
input: models.AlertInstance{
Labels: map[string]string{"key": "value"},
Labels: map[string]string{"key": "value"},
Annotations: annotations,
AlertInstanceKey: models.AlertInstanceKey{
RuleUID: "rule-uid-1",
RuleOrgID: 1,
@@ -46,6 +48,7 @@ func TestAlertInstanceModelToProto(t *testing.T) {
},
expected: &pb.AlertInstance{
Labels: map[string]string{"key": "value"},
Annotations: annotations,
LabelsHash: "hash123",
CurrentState: "Alerting",
CurrentStateSince: timestamppb.New(currentStateSince),
@@ -75,6 +78,7 @@ func TestAlertInstanceProtoToModel(t *testing.T) {
lastSentAt := currentStateSince.Add(-2 * time.Minute).UTC()
firedAt := currentStateSince.Add(-2 * time.Minute).UTC()
resolvedAt := currentStateSince.Add(-3 * time.Minute).UTC()
annotations := map[string]string{"summary": "value", "team": "alerting"}
ruleUID := "rule-uid-1"
orgID := int64(1)
@@ -87,6 +91,7 @@ func TestAlertInstanceProtoToModel(t *testing.T) {
name: "valid instance",
input: &pb.AlertInstance{
Labels: map[string]string{"key": "value"},
Annotations: annotations,
LabelsHash: "hash123",
CurrentState: "Alerting",
CurrentStateSince: timestamppb.New(currentStateSince),
@@ -98,7 +103,8 @@ func TestAlertInstanceProtoToModel(t *testing.T) {
ResultFingerprint: "fingerprint",
},
expected: &models.AlertInstance{
Labels: map[string]string{"key": "value"},
Labels: map[string]string{"key": "value"},
Annotations: annotations,
AlertInstanceKey: models.AlertInstanceKey{
RuleUID: ruleUID,
RuleOrgID: orgID,
@@ -132,7 +138,7 @@ func TestModelAlertInstanceMatchesProtobuf(t *testing.T) {
// and update them accordingly.
t.Run("when AlertInstance model changes", func(t *testing.T) {
modelType := reflect.TypeOf(models.AlertInstance{})
require.Equal(t, 11, modelType.NumField(), "AlertInstance model has changed, update the protobuf")
require.Equal(t, 12, modelType.NumField(), "AlertInstance model has changed, update the protobuf")
})
}
@@ -142,6 +148,7 @@ func TestCompressAndDecompressAlertInstances(t *testing.T) {
alertInstances := []*pb.AlertInstance{
{
Labels: map[string]string{"label-1": "value-1"},
Annotations: map[string]string{"anno-1": "value-1"},
LabelsHash: "hash-1",
CurrentState: "normal",
CurrentStateSince: timestamppb.New(now),
@@ -154,6 +161,7 @@ func TestCompressAndDecompressAlertInstances(t *testing.T) {
},
{
Labels: map[string]string{"label-2": "value-2"},
Annotations: map[string]string{"anno-2": "value-2"},
LabelsHash: "hash-2",
CurrentState: "firing",
CurrentStateSince: timestamppb.New(now),
@@ -185,6 +193,7 @@ func TestConvertAndCompressAlertInstances(t *testing.T) {
LabelsHash: "hash-1",
},
Labels: map[string]string{"label-1": "value-1"},
Annotations: map[string]string{"anno-1": "value-1"},
CurrentState: models.InstanceStateFiring,
CurrentStateSince: now,
CurrentStateEnd: now.Add(time.Hour),
@@ -202,6 +211,7 @@ func TestConvertAndCompressAlertInstances(t *testing.T) {
LabelsHash: "hash-2",
},
Labels: map[string]string{"label-2": "value-2"},
Annotations: map[string]string{"anno-2": "value-2"},
CurrentState: models.InstanceStateNormal,
CurrentStateSince: now,
CurrentStateEnd: now.Add(time.Hour),
@@ -227,6 +237,7 @@ func TestConvertAndCompressAlertInstances(t *testing.T) {
for i, protoInstance := range decompressedInstances {
modelInstance := alertInstanceProtoToModel("rule-uid-1", 1, protoInstance)
require.Equal(t, modelInstances[i].Labels, modelInstance.Labels)
require.Equal(t, modelInstances[i].Annotations, modelInstance.Annotations)
require.Equal(t, modelInstances[i].CurrentState, modelInstance.CurrentState)
require.Equal(t, modelInstances[i].LabelsHash, modelInstance.LabelsHash)
require.Equal(t, modelInstances[i].ResultFingerprint, modelInstance.ResultFingerprint)