Skip to content

Commit

Permalink
feat: add exclude label param (#4)
Browse files Browse the repository at this point in the history
* feat: add reopen_enabled flag

- fixes prometheus-community#120

Signed-off-by: Alik Khilazhev <[email protected]>

* chore: update docs and example

Signed-off-by: Alik Khilazhev <[email protected]>

* fix: nil pointer deref in tests

Signed-off-by: Alik Khilazhev <[email protected]>

* fix: issue resolution

Signed-off-by: Alik Khilazhev <[email protected]>

* feat: add exclude label param

* chore: add examples

* chore: rename param

* revert: Alik's changes

---------

Signed-off-by: Alik Khilazhev <[email protected]>
Signed-off-by: Rufina Talalaeva <[email protected]>
Co-authored-by: Alik Khilazhev <[email protected]>
Co-authored-by: sagdeevrr <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent d43cbab commit 8045df8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/jiralert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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 == "" {
Expand Down
7 changes: 4 additions & 3 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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))
}
Expand Down

0 comments on commit 8045df8

Please sign in to comment.