Skip to content

Commit 7fc1516

Browse files
gotjoshTheMeier
authored andcommitted
Revert "Return error when TmplText errors in sns notifier (prometheus#3531)" (prometheus#3876)
This reverts commit 603a728.
1 parent 79ea1fc commit 7fc1516

File tree

2 files changed

+8
-136
lines changed

2 files changed

+8
-136
lines changed

notify/sns/sns.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/aws/aws-sdk-go/service/sns"
3030
"github.com/go-kit/log"
3131
"github.com/go-kit/log/level"
32-
"github.com/pkg/errors"
3332
commoncfg "github.com/prometheus/common/config"
3433

3534
"github.com/prometheus/alertmanager/config"
@@ -64,12 +63,12 @@ func New(c *config.SNSConfig, t *template.Template, l log.Logger, httpOpts ...co
6463

6564
func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, error) {
6665
var (
67-
tmplErr error
68-
data = notify.GetTemplateData(ctx, n.tmpl, alert, n.logger)
69-
tmpl = notify.TmplText(n.tmpl, data, &tmplErr)
66+
err error
67+
data = notify.GetTemplateData(ctx, n.tmpl, alert, n.logger)
68+
tmpl = notify.TmplText(n.tmpl, data, &err)
7069
)
7170

72-
client, err := n.createSNSClient(tmpl, &tmplErr)
71+
client, err := n.createSNSClient(tmpl)
7372
if err != nil {
7473
var e awserr.RequestFailure
7574
if errors.As(err, &e) {
@@ -78,7 +77,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
7877
return true, err
7978
}
8079

81-
publishInput, err := n.createPublishInput(ctx, tmpl, &tmplErr)
80+
publishInput, err := n.createPublishInput(ctx, tmpl)
8281
if err != nil {
8382
return true, err
8483
}
@@ -100,7 +99,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
10099
return false, nil
101100
}
102101

103-
func (n *Notifier) createSNSClient(tmpl func(string) string, tmplErr *error) (*sns.SNS, error) {
102+
func (n *Notifier) createSNSClient(tmpl func(string) string) (*sns.SNS, error) {
104103
var creds *credentials.Credentials
105104
// If there are provided sigV4 credentials we want to use those to create a session.
106105
if n.conf.Sigv4.AccessKey != "" && n.conf.Sigv4.SecretKey != "" {
@@ -116,9 +115,6 @@ func (n *Notifier) createSNSClient(tmpl func(string) string, tmplErr *error) (*s
116115
if err != nil {
117116
return nil, err
118117
}
119-
if *tmplErr != nil {
120-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'api_url' template"))
121-
}
122118

123119
if n.conf.Sigv4.RoleARN != "" {
124120
var stsSess *session.Session
@@ -148,19 +144,13 @@ func (n *Notifier) createSNSClient(tmpl func(string) string, tmplErr *error) (*s
148144
return client, nil
149145
}
150146

151-
func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) string, tmplErr *error) (*sns.PublishInput, error) {
147+
func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) string) (*sns.PublishInput, error) {
152148
publishInput := &sns.PublishInput{}
153149
messageAttributes := n.createMessageAttributes(tmpl)
154-
if *tmplErr != nil {
155-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'attributes' template"))
156-
}
157150
// Max message size for a message in a SNS publish request is 256KB, except for SMS messages where the limit is 1600 characters/runes.
158151
messageSizeLimit := 256 * 1024
159152
if n.conf.TopicARN != "" {
160153
topicARN := tmpl(n.conf.TopicARN)
161-
if *tmplErr != nil {
162-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'topic_arn' template"))
163-
}
164154
publishInput.SetTopicArn(topicARN)
165155
// If we are using a topic ARN, it could be a FIFO topic specified by the topic's suffix ".fifo".
166156
if strings.HasSuffix(topicARN, ".fifo") {
@@ -175,24 +165,14 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
175165
}
176166
if n.conf.PhoneNumber != "" {
177167
publishInput.SetPhoneNumber(tmpl(n.conf.PhoneNumber))
178-
if *tmplErr != nil {
179-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'phone_number' template"))
180-
}
181168
// If we have an SMS message, we need to truncate to 1600 characters/runes.
182169
messageSizeLimit = 1600
183170
}
184171
if n.conf.TargetARN != "" {
185172
publishInput.SetTargetArn(tmpl(n.conf.TargetARN))
186-
if *tmplErr != nil {
187-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'target_arn' template"))
188-
}
189173
}
190174

191-
tmplMessage := tmpl(n.conf.Message)
192-
if *tmplErr != nil {
193-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'message' template"))
194-
}
195-
messageToSend, isTrunc, err := validateAndTruncateMessage(tmplMessage, messageSizeLimit)
175+
messageToSend, isTrunc, err := validateAndTruncateMessage(tmpl(n.conf.Message), messageSizeLimit)
196176
if err != nil {
197177
return nil, err
198178
}
@@ -206,9 +186,6 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
206186

207187
if n.conf.Subject != "" {
208188
publishInput.SetSubject(tmpl(n.conf.Subject))
209-
if *tmplErr != nil {
210-
return nil, notify.NewErrorWithReason(notify.ClientErrorReason, errors.Wrap(*tmplErr, "execute 'subject' template"))
211-
}
212189
}
213190

214191
return publishInput, nil

notify/sns/sns_test.go

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,11 @@
1414
package sns
1515

1616
import (
17-
"context"
18-
"net/url"
19-
"strings"
2017
"testing"
2118

22-
"github.com/go-kit/log"
23-
commoncfg "github.com/prometheus/common/config"
24-
"github.com/prometheus/common/sigv4"
2519
"github.com/stretchr/testify/require"
26-
27-
"github.com/prometheus/alertmanager/config"
28-
"github.com/prometheus/alertmanager/template"
29-
"github.com/prometheus/alertmanager/types"
3020
)
3121

32-
var logger = log.NewNopLogger()
33-
3422
func TestValidateAndTruncateMessage(t *testing.T) {
3523
sBuff := make([]byte, 257*1024)
3624
for i := range sBuff {
@@ -55,96 +43,3 @@ func TestValidateAndTruncateMessage(t *testing.T) {
5543
_, _, err = validateAndTruncateMessage(invalidUtf8String, 100)
5644
require.Error(t, err)
5745
}
58-
59-
func TestNotifyWithInvalidTemplate(t *testing.T) {
60-
for _, tc := range []struct {
61-
title string
62-
errMsg string
63-
updateCfg func(*config.SNSConfig)
64-
}{
65-
{
66-
title: "with invalid Attribute template",
67-
errMsg: "execute 'attributes' template",
68-
updateCfg: func(cfg *config.SNSConfig) {
69-
cfg.Attributes = map[string]string{
70-
"attribName1": "{{ template \"unknown_template\" . }}",
71-
}
72-
},
73-
},
74-
{
75-
title: "with invalid TopicArn template",
76-
errMsg: "execute 'topic_arn' template",
77-
updateCfg: func(cfg *config.SNSConfig) {
78-
cfg.TopicARN = "{{ template \"unknown_template\" . }}"
79-
},
80-
},
81-
{
82-
title: "with invalid PhoneNumber template",
83-
errMsg: "execute 'phone_number' template",
84-
updateCfg: func(cfg *config.SNSConfig) {
85-
cfg.PhoneNumber = "{{ template \"unknown_template\" . }}"
86-
},
87-
},
88-
{
89-
title: "with invalid Message template",
90-
errMsg: "execute 'message' template",
91-
updateCfg: func(cfg *config.SNSConfig) {
92-
cfg.Message = "{{ template \"unknown_template\" . }}"
93-
},
94-
},
95-
{
96-
title: "with invalid Subject template",
97-
errMsg: "execute 'subject' template",
98-
updateCfg: func(cfg *config.SNSConfig) {
99-
cfg.Subject = "{{ template \"unknown_template\" . }}"
100-
},
101-
},
102-
{
103-
title: "with invalid APIUrl template",
104-
errMsg: "execute 'api_url' template",
105-
updateCfg: func(cfg *config.SNSConfig) {
106-
cfg.APIUrl = "{{ template \"unknown_template\" . }}"
107-
},
108-
},
109-
{
110-
title: "with invalid TargetARN template",
111-
errMsg: "execute 'target_arn' template",
112-
updateCfg: func(cfg *config.SNSConfig) {
113-
cfg.TargetARN = "{{ template \"unknown_template\" . }}"
114-
},
115-
},
116-
} {
117-
tc := tc
118-
t.Run(tc.title, func(t *testing.T) {
119-
snsCfg := &config.SNSConfig{
120-
HTTPConfig: &commoncfg.HTTPClientConfig{},
121-
TopicARN: "TestTopic",
122-
Sigv4: sigv4.SigV4Config{
123-
Region: "us-west-2",
124-
},
125-
}
126-
if tc.updateCfg != nil {
127-
tc.updateCfg(snsCfg)
128-
}
129-
notifier, err := New(
130-
snsCfg,
131-
createTmpl(t),
132-
logger,
133-
)
134-
require.NoError(t, err)
135-
var alerts []*types.Alert
136-
_, err = notifier.Notify(context.Background(), alerts...)
137-
require.Error(t, err)
138-
require.True(t, strings.Contains(err.Error(), "template \"unknown_template\" not defined"))
139-
require.True(t, strings.Contains(err.Error(), tc.errMsg))
140-
})
141-
}
142-
}
143-
144-
// CreateTmpl returns a ready-to-use template.
145-
func createTmpl(t *testing.T) *template.Template {
146-
tmpl, err := template.FromGlobs([]string{})
147-
require.NoError(t, err)
148-
tmpl.ExternalURL, _ = url.Parse("http://am")
149-
return tmpl
150-
}

0 commit comments

Comments
 (0)