diff --git a/cmd/jiralert/main.go b/cmd/jiralert/main.go index 86be130..6f8ddec 100644 --- a/cmd/jiralert/main.go +++ b/cmd/jiralert/main.go @@ -36,9 +36,10 @@ import ( ) const ( - unknownReceiver = "" - logFormatLogfmt = "logfmt" - logFormatJSON = "json" + unknownReceiver = "" + logFormatLogfmt = "logfmt" + logFormatJSON = "json" + defaultMaxDescriptionLength = 32767 // https://jira.atlassian.com/browse/JRASERVER-64351 ) var ( @@ -48,9 +49,10 @@ var ( logFormat = flag.String("log.format", logFormatLogfmt, "Log format to use ("+logFormatLogfmt+", "+logFormatJSON+")") hashJiraLabel = flag.Bool("hash-jira-label", false, "if enabled: renames ALERT{...} to JIRALERT{...}; also hashes the key-value pairs inside of JIRALERT{...} in the created jira issue labels"+ "- this ensures that the label text does not overflow the allowed length in jira (255)") - updateSummary = flag.Bool("update-summary", true, "When false, jiralert does not update the summary of the existing jira issue, even when changes are spotted.") - updateDescription = flag.Bool("update-description", true, "When false, jiralert does not update the description of the existing jira issue, even when changes are spotted.") - reopenTickets = flag.Bool("reopen-tickets", true, "When false, jiralert does not reopen tickets.") + updateSummary = flag.Bool("update-summary", true, "When false, jiralert does not update the summary of the existing jira issue, even when changes are spotted.") + updateDescription = flag.Bool("update-description", true, "When false, jiralert does not update the description of the existing jira issue, even when changes are spotted.") + reopenTickets = flag.Bool("reopen-tickets", true, "When false, jiralert does not reopen tickets.") + maxDescriptionLength = flag.Int("max-description-length", defaultMaxDescriptionLength, "Maximum length of Descriptions. Truncate to this size avoid server errors.") // Version is the build version, set by make to latest git tag/hash via `-ldflags "-X main.Version=$(VERSION)"`. Version = "" @@ -124,7 +126,7 @@ func main() { return } - if retry, err := notify.NewReceiver(logger, conf, tmpl, client.Issue).Notify(&data, *hashJiraLabel, *updateSummary, *updateDescription, *reopenTickets); err != nil { + if retry, err := notify.NewReceiver(logger, conf, tmpl, client.Issue).Notify(&data, *hashJiraLabel, *updateSummary, *updateDescription, *reopenTickets, *maxDescriptionLength); err != nil { var status int if retry { // Instruct Alertmanager to retry. diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index c4a5c46..b929e4b 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -61,7 +61,7 @@ func NewReceiver(logger log.Logger, c *config.ReceiverConfig, t *template.Templa } // Notify manages JIRA issues based on alertmanager webhook notify message. -func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSummary bool, updateDescription bool, reopenTickets bool) (bool, error) { +func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSummary bool, updateDescription bool, reopenTickets bool, maxDescriptionLength int) (bool, error) { project, err := r.tmpl.Execute(r.conf.Project, data) if err != nil { return false, errors.Wrap(err, "generate project from template") @@ -86,6 +86,11 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum return false, errors.Wrap(err, "render issue description") } + if len(issueDesc) > maxDescriptionLength { + level.Warn(r.logger).Log("msg", "truncating description", "original", len(issueDesc), "limit", maxDescriptionLength) + issueDesc = issueDesc[:maxDescriptionLength] + } + if issue != nil { // Update summary if needed. diff --git a/pkg/notify/notify_test.go b/pkg/notify/notify_test.go index cb4b0f3..bd3d022 100644 --- a/pkg/notify/notify_test.go +++ b/pkg/notify/notify_test.go @@ -597,7 +597,7 @@ func TestNotify_JIRAInteraction(t *testing.T) { return testNowTime } - _, err := receiver.Notify(tcase.inputAlert, true, true, true, true) + _, err := receiver.Notify(tcase.inputAlert, true, true, true, true, 32768) require.NoError(t, err) require.Equal(t, tcase.expectedJiraIssues, fakeJira.issuesByKey) }); !ok {