From 005d6c2858822eff3dbe935bd7bc49db94076dbe Mon Sep 17 00:00:00 2001 From: Jason Wells Date: Mon, 10 Jul 2023 04:21:36 -0700 Subject: [PATCH] truncate descriptions that exceed -max-description-length (default 32KB) (#165) * truncate descriptions that exceed -max-description-length (default 32,768) Signed-off-by: Jason Wells * Update main.go size was off by 1 Signed-off-by: Jason Wells --------- Signed-off-by: Jason Wells --- cmd/jiralert/main.go | 16 +++++++++------- pkg/notify/notify.go | 7 ++++++- pkg/notify/notify_test.go | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) 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 ffcc6d8..ca98486 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -60,7 +60,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") @@ -85,6 +85,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 8e73f4a..3390f56 100644 --- a/pkg/notify/notify_test.go +++ b/pkg/notify/notify_test.go @@ -592,7 +592,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 {