From 8045df8c550082f4a76d041da4da161d2606ab09 Mon Sep 17 00:00:00 2001 From: Rufina Talalaeva Date: Mon, 13 May 2024 17:17:40 +0300 Subject: [PATCH] feat: add exclude label param (#4) * feat: add reopen_enabled flag - fixes #120 Signed-off-by: Alik Khilazhev * chore: update docs and example Signed-off-by: Alik Khilazhev * fix: nil pointer deref in tests Signed-off-by: Alik Khilazhev * fix: issue resolution Signed-off-by: Alik Khilazhev * feat: add exclude label param * chore: add examples * chore: rename param * revert: Alik's changes --------- Signed-off-by: Alik Khilazhev Signed-off-by: Rufina Talalaeva Co-authored-by: Alik Khilazhev Co-authored-by: sagdeevrr --- examples/jiralert.yml | 8 ++++++++ pkg/config/config.go | 8 ++++++++ pkg/notify/notify.go | 7 ++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/jiralert.yml b/examples/jiralert.yml index 46263c3..6ad7857 100644 --- a/examples/jiralert.yml +++ b/examples/jiralert.yml @@ -8,6 +8,9 @@ defaults: # Alternatively to user and password use a Personal Access Token # personal_access_token: "Your Personal Access Token". See https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html + # Exclude labels in JIRA issue. + exclude_keys: [] + # The type of JIRA issue to create. Required. issue_type: Bug # Issue priority. Optional. @@ -42,6 +45,8 @@ receivers: project: AB # Copy all Prometheus labels into separate JIRA labels. Optional (default: false). add_group_labels: false + exclude_keys: + - pod # Include ticket update as comment too. Optional (default: false). update_in_comment: false # Will be merged with the static_labels from the default map @@ -56,6 +61,9 @@ receivers: # Standard or custom field values to set on created issue. Optional. # # See https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#setting-custom-field-data-for-other-field-types for further examples. + exclude_keys: + - pod + - job fields: # TextField customfield_10001: "Random text" diff --git a/pkg/config/config.go b/pkg/config/config.go index 7fc63c4..4e5c2fd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -147,6 +147,9 @@ type ReceiverConfig struct { Components []string `yaml:"components" json:"components"` StaticLabels []string `yaml:"static_labels" json:"static_labels"` + // ExcludeKeys settings + ExcludeKeys []string `yaml:"exclude_keys" json:"exclude_keys"` + // Label copy settings AddGroupLabels *bool `yaml:"add_group_labels" json:"add_group_labels"` @@ -255,6 +258,11 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { } } + // Check ExcludeKeys + if len(rc.ExcludeKeys) == 0 { + rc.ExcludeKeys = c.Defaults.ExcludeKeys + } + // Check required issue fields. if rc.Project == "" { if c.Defaults.Project == "" { diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index e4b96a2..7441373 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -67,7 +67,8 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum return false, errors.Wrap(err, "generate project from template") } - issueGroupLabel := toGroupTicketLabel(data.GroupLabels, hashJiraLabel) + excludeKeys := r.conf.ExcludeKeys + issueGroupLabel := toGroupTicketLabel(data.GroupLabels, hashJiraLabel, excludeKeys) issue, retry, err := r.findIssueToReuse(project, issueGroupLabel) if err != nil { @@ -289,7 +290,7 @@ func deepCopyWithTemplate(value interface{}, tmpl *template.Template, data inter // hashing ensures that JIRA validation still accepts the output even // if the combined length of all groupLabel key-value pairs would be // longer than 255 chars -func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool) string { +func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool, excludeKeys []string) string { // new opt in behavior if hashJiraLabel { hash := sha512.New() @@ -302,7 +303,7 @@ func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool) string // old default behavior buf := bytes.NewBufferString("ALERT{") - for _, p := range groupLabels.SortedPairs() { + for _, p := range groupLabels.Remove(excludeKeys).SortedPairs() { buf.WriteString(p.Name) buf.WriteString(fmt.Sprintf("=%q,", p.Value)) }