Skip to content

Commit fbd25c2

Browse files
refactor(jira): use grouplabels to calculate hash instead of ExtractGroupKey function
Signed-off-by: Holger Waschke <[email protected]>
1 parent 9f65044 commit fbd25c2

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

notify/jira/jira.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ func New(c *config.JiraConfig, t *template.Template, l *slog.Logger, httpOpts ..
6666

6767
// Notify implements the Notifier interface.
6868
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
69-
key, err := notify.ExtractGroupKey(ctx)
70-
if err != nil {
71-
return false, err
69+
groupLabels, ok := notify.GroupLabels(ctx)
70+
if !ok {
71+
return false, fmt.Errorf("group labels missing")
7272
}
73+
groupID := notify.HashGroupLabels(groupLabels)
74+
75+
logger := n.logger.With("group_key", groupLabels.String())
7376

74-
logger := n.logger.With("group_key", key.String())
77+
n.logger.Debug("computed JIRA group hash", "group_labels", groupLabels, "group_id", groupID)
7578

7679
var (
7780
alerts = types.Alerts(as...)
@@ -87,7 +90,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
8790
method = http.MethodPost
8891
)
8992

90-
existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, key.Hash(), alerts.HasFiring(), tmplTextFunc)
93+
existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, groupID, alerts.HasFiring(), tmplTextFunc)
9194
if err != nil {
9295
return shouldRetry, fmt.Errorf("failed to look up existing issues: %w", err)
9396
}
@@ -106,7 +109,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
106109
logger.Debug("updating existing issue", "issue_key", existingIssue.Key)
107110
}
108111

109-
requestBody, err := n.prepareIssueRequestBody(ctx, logger, key.Hash(), tmplTextFunc)
112+
requestBody, err := n.prepareIssueRequestBody(ctx, logger, groupID, tmplTextFunc)
110113
if err != nil {
111114
return false, err
112115
}

notify/jira/jira_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func TestJiraNotify(t *testing.T) {
472472
Summary: "[FIRING:1] test (vm1 critical)",
473473
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n - severity = critical\n\nAnnotations:\n\nSource: \n\n\n\n\n",
474474
Issuetype: &idNameValue{Name: "Incident"},
475-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
475+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
476476
Project: &issueProject{Key: "OPS"},
477477
Priority: &idNameValue{Name: "High"},
478478
},
@@ -516,7 +516,7 @@ func TestJiraNotify(t *testing.T) {
516516
Summary: "[FIRING:1] test (vm1 MINOR MONITORING critical)",
517517
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n - issue_type = MINOR\n - project = MONITORING\n - severity = critical\n\nAnnotations:\n\nSource: \n\n\n\n\n",
518518
Issuetype: &idNameValue{Name: "MINOR"},
519-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
519+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
520520
Project: &issueProject{Key: "MONITORING"},
521521
Priority: &idNameValue{Name: "High"},
522522
},
@@ -568,7 +568,7 @@ func TestJiraNotify(t *testing.T) {
568568
Summary: strings.Repeat("A", maxSummaryLenRunes-1) + "…",
569569
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
570570
Issuetype: &idNameValue{Name: "Incident"},
571-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
571+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
572572
Project: &issueProject{Key: "OPS"},
573573
},
574574
},
@@ -631,7 +631,7 @@ func TestJiraNotify(t *testing.T) {
631631
Summary: "[FIRING:1] test (vm1)",
632632
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
633633
Issuetype: &idNameValue{Name: "Incident"},
634-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
634+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
635635
Project: &issueProject{Key: "OPS"},
636636
Priority: &idNameValue{Name: "High"},
637637
},
@@ -686,7 +686,7 @@ func TestJiraNotify(t *testing.T) {
686686
Summary: "[RESOLVED] test (vm1)",
687687
Description: "\n\n\n# Alerts Resolved:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n",
688688
Issuetype: &idNameValue{Name: "Incident"},
689-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
689+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
690690
Project: &issueProject{Key: "OPS"},
691691
},
692692
},
@@ -740,7 +740,7 @@ func TestJiraNotify(t *testing.T) {
740740
Summary: "[FIRING:1] test (vm1)",
741741
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
742742
Issuetype: &idNameValue{Name: "Incident"},
743-
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
743+
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
744744
Project: &issueProject{Key: "OPS"},
745745
},
746746
},

notify/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package notify
1616
import (
1717
"context"
1818
"crypto/sha256"
19+
"encoding/hex"
1920
"errors"
2021
"fmt"
2122
"io"
@@ -25,6 +26,7 @@ import (
2526
"slices"
2627
"strings"
2728

29+
"github.com/prometheus/common/model"
2830
"github.com/prometheus/common/version"
2931

3032
"github.com/prometheus/alertmanager/template"
@@ -297,3 +299,10 @@ func GetFailureReasonFromStatusCode(statusCode int) Reason {
297299

298300
return DefaultReason
299301
}
302+
303+
// HashGroupLabels returns the sha256 hash for a group's labels as integrations
304+
// may have maximum length requirements on deduplication keys.
305+
func HashGroupLabels(labels model.LabelSet) string {
306+
sum := sha256.Sum256([]byte(labels.String()))
307+
return hex.EncodeToString(sum[:])
308+
}

0 commit comments

Comments
 (0)