From 6ada9a634dc7acb24db462a973feaebb82205cce Mon Sep 17 00:00:00 2001 From: Tyler Reid Date: Wed, 16 Jun 2021 09:30:54 -0500 Subject: [PATCH] Cache fifo bool in the notifier Signed-off-by: Tyler Reid --- notify/sns/sns.go | 28 +++++++++++++++------------- notify/sns/sns_test.go | 4 ++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/notify/sns/sns.go b/notify/sns/sns.go index 7cfba6e5..63e40252 100644 --- a/notify/sns/sns.go +++ b/notify/sns/sns.go @@ -41,6 +41,7 @@ type Notifier struct { logger log.Logger client *http.Client retrier *notify.Retrier + isFifo *bool } // New returns a new SNS notification handler. @@ -49,7 +50,6 @@ func New(c *config.SNSConfig, t *template.Template, l log.Logger, httpOpts ...co if err != nil { return nil, err } - return &Notifier{ conf: c, tmpl: t, @@ -59,7 +59,7 @@ func New(c *config.SNSConfig, t *template.Template, l log.Logger, httpOpts ...co }, nil } -func (n Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, error) { +func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, error) { var ( err error data = notify.GetTemplateData(ctx, n.tmpl, alert, n.logger) @@ -100,16 +100,19 @@ func (n Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, erro n.conf.Attributes["truncated"] = "true" } - isFifo, err := checkTopicFifoAttribute(client, n.conf.TopicARN) - if err != nil { - if e, ok := err.(awserr.RequestFailure); ok { - return n.retrier.Check(e.StatusCode(), strings.NewReader(e.Message())) - } else { - return true, err + if n.isFifo == nil { + checkFifo, err := checkTopicFifoAttribute(client, n.conf.TopicARN) + if err != nil { + if e, ok := err.(awserr.RequestFailure); ok { + return n.retrier.Check(e.StatusCode(), strings.NewReader(e.Message())) + } else { + return true, err + } } + n.isFifo = &checkFifo } - // Deduplication key and Message Group ID are only added if it's a FIFO SNS Topic. - if isFifo { + if *n.isFifo { + // Deduplication key and Message Group ID are only added if it's a FIFO SNS Topic. key, err := notify.ExtractGroupKey(ctx) if err != nil { return false, err @@ -172,7 +175,6 @@ func (n Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, erro } func checkTopicFifoAttribute(client *sns.SNS, topicARN string) (bool, error) { - fmt.Println("Checking Attributes") topicAttributes, err := client.GetTopicAttributes(&sns.GetTopicAttributesInput{TopicArn: aws.String(topicARN)}) if err != nil { return false, err @@ -187,8 +189,8 @@ func checkTopicFifoAttribute(client *sns.SNS, topicARN string) (bool, error) { func validateAndTruncateMessage(message string) (string, bool, error) { if utf8.ValidString(message) { // if the message is larger than 256KB we have to truncate. - if len(message) > 256 * 1024 { - truncated := make([]byte, 256 * 1024, 256 * 1024) + if len(message) > 256*1024 { + truncated := make([]byte, 256*1024, 256*1024) copy(truncated, message) return string(truncated), true, nil } diff --git a/notify/sns/sns_test.go b/notify/sns/sns_test.go index 6d85d143..9e36e5a4 100644 --- a/notify/sns/sns_test.go +++ b/notify/sns/sns_test.go @@ -20,7 +20,7 @@ import ( ) func TestValidateAndTruncateMessage(t *testing.T) { - sBuff := make([]byte, 257 * 1024) + sBuff := make([]byte, 257*1024) for i := range sBuff { sBuff[i] = byte(33) } @@ -28,7 +28,7 @@ func TestValidateAndTruncateMessage(t *testing.T) { require.True(t, isTruncated) require.NoError(t, err) require.NotEqual(t, sBuff, truncatedMessage) - require.Equal(t, len(truncatedMessage), 256 * 1024) + require.Equal(t, len(truncatedMessage), 256*1024) sBuff = make([]byte, 100) for i := range sBuff {