Skip to content

Commit

Permalink
truncate descriptions that exceed -max-description-length (default 32…
Browse files Browse the repository at this point in the history
…KB) (#165)

* truncate descriptions that exceed -max-description-length (default 32,768)

Signed-off-by: Jason Wells <[email protected]>

* Update main.go

size was off by 1

Signed-off-by: Jason Wells <[email protected]>

---------

Signed-off-by: Jason Wells <[email protected]>
  • Loading branch information
twotired authored Jul 10, 2023
1 parent 8fdd24e commit 005d6c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
16 changes: 9 additions & 7 deletions cmd/jiralert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ import (
)

const (
unknownReceiver = "<unknown>"
logFormatLogfmt = "logfmt"
logFormatJSON = "json"
unknownReceiver = "<unknown>"
logFormatLogfmt = "logfmt"
logFormatJSON = "json"
defaultMaxDescriptionLength = 32767 // https://jira.atlassian.com/browse/JRASERVER-64351
)

var (
Expand All @@ -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 = "<local build>"
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 005d6c2

Please sign in to comment.