Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions notify/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ func New(c *config.JiraConfig, t *template.Template, l *slog.Logger, httpOpts ..

// Notify implements the Notifier interface.
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return false, err
groupLabels, ok := notify.GroupLabels(ctx)
if !ok {
return false, fmt.Errorf("group labels missing")
}
groupID := notify.HashGroupLabels(groupLabels)

logger := n.logger.With("group_key", groupLabels.String())

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

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

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

requestBody, err := n.prepareIssueRequestBody(ctx, logger, key.Hash(), tmplTextFunc)
requestBody, err := n.prepareIssueRequestBody(ctx, logger, groupID, tmplTextFunc)
if err != nil {
return false, err
}
Expand Down
16 changes: 9 additions & 7 deletions notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ func TestJiraTemplating(t *testing.T) {
require.NoError(t, err)

ctx := context.Background()
ctx = notify.WithGroupKey(ctx, "1")
ctx = notify.WithGroupLabels(ctx, model.LabelSet{
"lbl1": "val1",
})

ok, err := pd.Notify(ctx, []*types.Alert{
{
Expand Down Expand Up @@ -472,7 +474,7 @@ func TestJiraNotify(t *testing.T) {
Summary: "[FIRING:1] test (vm1 critical)",
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",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
Priority: &idNameValue{Name: "High"},
},
Expand Down Expand Up @@ -516,7 +518,7 @@ func TestJiraNotify(t *testing.T) {
Summary: "[FIRING:1] test (vm1 MINOR MONITORING critical)",
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",
Issuetype: &idNameValue{Name: "MINOR"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "MONITORING"},
Priority: &idNameValue{Name: "High"},
},
Expand Down Expand Up @@ -568,7 +570,7 @@ func TestJiraNotify(t *testing.T) {
Summary: strings.Repeat("A", maxSummaryLenRunes-1) + "…",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
},
},
Expand Down Expand Up @@ -631,7 +633,7 @@ func TestJiraNotify(t *testing.T) {
Summary: "[FIRING:1] test (vm1)",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
Priority: &idNameValue{Name: "High"},
},
Expand Down Expand Up @@ -686,7 +688,7 @@ func TestJiraNotify(t *testing.T) {
Summary: "[RESOLVED] test (vm1)",
Description: "\n\n\n# Alerts Resolved:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
},
},
Expand Down Expand Up @@ -740,7 +742,7 @@ func TestJiraNotify(t *testing.T) {
Summary: "[FIRING:1] test (vm1)",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Labels: []string{"ALERT{2619b3472463c746278b332eae12b978ff97478bd1cdaa911b06cd2c134e0382}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
},
},
Expand Down
9 changes: 9 additions & 0 deletions notify/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package notify
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
Expand All @@ -25,6 +26,7 @@ import (
"slices"
"strings"

"github.com/prometheus/common/model"
"github.com/prometheus/common/version"

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

return DefaultReason
}

// HashGroupLabels returns the sha256 hash for a group's labels as integrations
// may have maximum length requirements on deduplication keys.
func HashGroupLabels(labels model.LabelSet) string {
sum := sha256.Sum256([]byte(labels.String()))
return hex.EncodeToString(sum[:])
}
Loading