Skip to content

Commit e3729d8

Browse files
feat(jira): add hash identifier support for custom issue correlation
Signed-off-by: Holger Waschke <holger.waschke@dvag.com>
1 parent 5a1d7e3 commit e3729d8

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

config/notifiers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ type JiraConfig struct {
980980
ResolveTransition string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty"`
981981
WontFixResolution string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty"`
982982
ReopenDuration model.Duration `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty"`
983+
HashIdentifier string `yaml:"hash_identifier,omitempty" json:"hash_identifier,omitempty"`
983984

984985
Fields map[string]any `yaml:"fields,omitempty" json:"custom_fields,omitempty"`
985986
}

docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,10 @@ project: <string>
10761076
# Issue description template.
10771077
[ description: <tmpl_string> | default = '{{ template "jira.default.description" . }}' ]
10781078
1079+
# overrides the default ALERT{<group-hash>} jira label used to correlate issues.
1080+
# Example: {{ .GroupLabels }}
1081+
[ hash_identifier: <tmpl_string> ]
1082+
10791083
# Labels to be added to the issue.
10801084
labels:
10811085
[ - <tmpl_string> ... ]

notify/jira/jira.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,21 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
8787
method = http.MethodPost
8888
)
8989

90-
existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, key.Hash(), alerts.HasFiring(), tmplTextFunc)
90+
jiraHash := key.Hash()
91+
// If a custom identifier is configured, use its hash instead.
92+
if n.conf.HashIdentifier != "" {
93+
identifier, err := tmplTextFunc(n.conf.HashIdentifier)
94+
if err != nil {
95+
return false, fmt.Errorf("hash_identifier: %w", err)
96+
}
97+
98+
identifier = strings.TrimSpace(identifier)
99+
if identifier != "" {
100+
jiraHash = notify.Key(identifier).Hash()
101+
}
102+
}
103+
104+
existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, jiraHash, alerts.HasFiring(), tmplTextFunc)
91105
if err != nil {
92106
return shouldRetry, fmt.Errorf("failed to look up existing issues: %w", err)
93107
}
@@ -106,7 +120,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
106120
logger.Debug("updating existing issue", "issue_key", existingIssue.Key)
107121
}
108122

109-
requestBody, err := n.prepareIssueRequestBody(ctx, logger, key.Hash(), tmplTextFunc)
123+
requestBody, err := n.prepareIssueRequestBody(ctx, logger, jiraHash, tmplTextFunc)
110124
if err != nil {
111125
return false, err
112126
}

notify/jira/jira_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,53 @@ func TestJiraNotify(t *testing.T) {
438438
issue issue
439439
errMsg string
440440
}{
441+
{
442+
title: "create new issue with group labels as hash identifier",
443+
cfg: &config.JiraConfig{
444+
Summary: `{{ template "jira.default.summary" . }}`,
445+
Description: `{{ template "jira.default.description" . }}`,
446+
IssueType: "Incident",
447+
Project: "OPS",
448+
Priority: `{{ template "jira.default.priority" . }}`,
449+
Labels: []string{"alertmanager", "{{ .GroupLabels.alertname }}"},
450+
ReopenDuration: model.Duration(1 * time.Hour),
451+
ReopenTransition: "REOPEN",
452+
ResolveTransition: "CLOSE",
453+
WontFixResolution: "WONTFIX",
454+
HashIdentifier: `{{ .GroupLabels }}`,
455+
},
456+
alert: &types.Alert{
457+
Alert: model.Alert{
458+
Labels: model.LabelSet{
459+
"alertname": "test",
460+
"instance": "vm1",
461+
"severity": "critical",
462+
},
463+
StartsAt: time.Now(),
464+
EndsAt: time.Now().Add(time.Hour),
465+
},
466+
},
467+
searchResponse: issueSearchResult{
468+
Issues: []issue{},
469+
},
470+
issue: issue{
471+
Key: "",
472+
Fields: &issueFields{
473+
Summary: "[FIRING:1] test (vm1 critical)",
474+
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",
475+
Issuetype: &idNameValue{Name: "Incident"},
476+
Labels: []string{
477+
"ALERT{602a8de61908ae3ce97961992ec1ed4ea9500c331b79baaf6f565ccdc1750c0c}",
478+
"alertmanager",
479+
"test",
480+
},
481+
Project: &issueProject{Key: "OPS"},
482+
Priority: &idNameValue{Name: "High"},
483+
},
484+
},
485+
customFieldAssetFn: func(t *testing.T, issue map[string]any) {},
486+
errMsg: "",
487+
},
441488
{
442489
title: "create new issue",
443490
cfg: &config.JiraConfig{

0 commit comments

Comments
 (0)