Skip to content

Commit

Permalink
truncate too long summary title
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke committed Jun 25, 2024
1 parent 23628b3 commit ca08adb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 15 additions & 2 deletions notify/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import (
"github.com/prometheus/alertmanager/types"
)

const (
maxSummaryLenRunes = 255
)

// Notifier implements a Notifier for JIRA notifications.
type Notifier struct {
conf *config.JiraConfig
Expand Down Expand Up @@ -105,7 +109,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
method = http.MethodPut
}

requestBody, err := n.prepareIssueRequestBody(tmplTextFunc)
requestBody, err := n.prepareIssueRequestBody(ctx, tmplTextFunc)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -134,7 +138,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, nil
}

func (n *Notifier) prepareIssueRequestBody(tmplTextFunc templateFunc) (issue, error) {
func (n *Notifier) prepareIssueRequestBody(ctx context.Context, tmplTextFunc templateFunc) (issue, error) {
summary, err := tmplTextFunc(n.conf.Summary)
if err != nil {
return issue{}, fmt.Errorf("template error: %w", err)
Expand All @@ -147,6 +151,15 @@ func (n *Notifier) prepareIssueRequestBody(tmplTextFunc templateFunc) (issue, er
return issue{}, fmt.Errorf("convertToMarshalMap error: %w", err)
}

summary, truncated := notify.TruncateInRunes(summary, maxSummaryLenRunes)
if truncated {
key, err := notify.ExtractGroupKey(ctx)
if err != nil {
return issue{}, err
}
level.Warn(n.logger).Log("msg", "Truncated summary", "key", key, "max_runes", maxSummaryLenRunes)
}

requestBody := issue{Fields: &issueFields{
Project: &issueProject{Key: n.conf.Project},
Issuetype: &idNameValue{Name: n.conf.IssueType},
Expand Down
7 changes: 4 additions & 3 deletions notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -210,9 +211,9 @@ func TestJiraNotify(t *testing.T) {
errMsg: "",
},
{
title: "create new issue with custom field",
title: "create new issue with custom field and too long summary",
cfg: &config.JiraConfig{
Summary: `{{ template "jira.default.summary" . }}`,
Summary: strings.Repeat("A", maxSummaryLenRunes+10),
Description: `{{ template "jira.default.description" . }}`,
IssueType: "Incident",
Project: "OPS",
Expand Down Expand Up @@ -252,7 +253,7 @@ func TestJiraNotify(t *testing.T) {
issue: issue{
Key: "",
Fields: &issueFields{
Summary: "[FIRING:1] test (vm1)",
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{"alertmanager", "ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "test"},
Expand Down

0 comments on commit ca08adb

Please sign in to comment.