@@ -28,6 +28,7 @@ import (
28
28
"github.com/aws/aws-sdk-go/service/sns"
29
29
"github.com/go-kit/log"
30
30
"github.com/go-kit/log/level"
31
+ "github.com/pkg/errors"
31
32
commoncfg "github.com/prometheus/common/config"
32
33
33
34
"github.com/prometheus/alertmanager/config"
@@ -62,20 +63,20 @@ func New(c *config.SNSConfig, t *template.Template, l log.Logger, httpOpts ...co
62
63
63
64
func (n * Notifier ) Notify (ctx context.Context , alert ... * types.Alert ) (bool , error ) {
64
65
var (
65
- err error
66
- data = notify .GetTemplateData (ctx , n .tmpl , alert , n .logger )
67
- tmpl = notify .TmplText (n .tmpl , data , & err )
66
+ tmplErr error
67
+ data = notify .GetTemplateData (ctx , n .tmpl , alert , n .logger )
68
+ tmpl = notify .TmplText (n .tmpl , data , & tmplErr )
68
69
)
69
70
70
- client , err := n .createSNSClient (tmpl )
71
+ client , err := n .createSNSClient (tmpl , & tmplErr )
71
72
if err != nil {
72
73
if e , ok := err .(awserr.RequestFailure ); ok {
73
74
return n .retrier .Check (e .StatusCode (), strings .NewReader (e .Message ()))
74
75
}
75
76
return true , err
76
77
}
77
78
78
- publishInput , err := n .createPublishInput (ctx , tmpl )
79
+ publishInput , err := n .createPublishInput (ctx , tmpl , & tmplErr )
79
80
if err != nil {
80
81
return true , err
81
82
}
@@ -96,7 +97,7 @@ func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, err
96
97
return false , nil
97
98
}
98
99
99
- func (n * Notifier ) createSNSClient (tmpl func (string ) string ) (* sns.SNS , error ) {
100
+ func (n * Notifier ) createSNSClient (tmpl func (string ) string , tmplErr * error ) (* sns.SNS , error ) {
100
101
var creds * credentials.Credentials
101
102
// If there are provided sigV4 credentials we want to use those to create a session.
102
103
if n .conf .Sigv4 .AccessKey != "" && n .conf .Sigv4 .SecretKey != "" {
@@ -112,6 +113,9 @@ func (n *Notifier) createSNSClient(tmpl func(string) string) (*sns.SNS, error) {
112
113
if err != nil {
113
114
return nil , err
114
115
}
116
+ if * tmplErr != nil {
117
+ return nil , errors .Wrap (* tmplErr , "execute 'api_url' template" )
118
+ }
115
119
116
120
if n .conf .Sigv4 .RoleARN != "" {
117
121
var stsSess * session.Session
@@ -141,13 +145,19 @@ func (n *Notifier) createSNSClient(tmpl func(string) string) (*sns.SNS, error) {
141
145
return client , nil
142
146
}
143
147
144
- func (n * Notifier ) createPublishInput (ctx context.Context , tmpl func (string ) string ) (* sns.PublishInput , error ) {
148
+ func (n * Notifier ) createPublishInput (ctx context.Context , tmpl func (string ) string , tmplErr * error ) (* sns.PublishInput , error ) {
145
149
publishInput := & sns.PublishInput {}
146
150
messageAttributes := n .createMessageAttributes (tmpl )
151
+ if * tmplErr != nil {
152
+ return nil , errors .Wrap (* tmplErr , "execute 'attributes' template" )
153
+ }
147
154
// Max message size for a message in a SNS publish request is 256KB, except for SMS messages where the limit is 1600 characters/runes.
148
155
messageSizeLimit := 256 * 1024
149
156
if n .conf .TopicARN != "" {
150
157
topicARN := tmpl (n .conf .TopicARN )
158
+ if * tmplErr != nil {
159
+ return nil , errors .Wrap (* tmplErr , "execute 'topic_arn' template" )
160
+ }
151
161
publishInput .SetTopicArn (topicARN )
152
162
// If we are using a topic ARN, it could be a FIFO topic specified by the topic's suffix ".fifo".
153
163
if strings .HasSuffix (topicARN , ".fifo" ) {
@@ -162,14 +172,24 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
162
172
}
163
173
if n .conf .PhoneNumber != "" {
164
174
publishInput .SetPhoneNumber (tmpl (n .conf .PhoneNumber ))
175
+ if * tmplErr != nil {
176
+ return nil , errors .Wrap (* tmplErr , "execute 'phone_number' template" )
177
+ }
165
178
// If we have an SMS message, we need to truncate to 1600 characters/runes.
166
179
messageSizeLimit = 1600
167
180
}
168
181
if n .conf .TargetARN != "" {
169
182
publishInput .SetTargetArn (tmpl (n .conf .TargetARN ))
183
+ if * tmplErr != nil {
184
+ return nil , errors .Wrap (* tmplErr , "execute 'target_arn' template" )
185
+ }
170
186
}
171
187
172
- messageToSend , isTrunc , err := validateAndTruncateMessage (tmpl (n .conf .Message ), messageSizeLimit )
188
+ tmplMessage := tmpl (n .conf .Message )
189
+ if * tmplErr != nil {
190
+ return nil , errors .Wrap (* tmplErr , "execute 'message' template" )
191
+ }
192
+ messageToSend , isTrunc , err := validateAndTruncateMessage (tmplMessage , messageSizeLimit )
173
193
if err != nil {
174
194
return nil , err
175
195
}
@@ -183,6 +203,9 @@ func (n *Notifier) createPublishInput(ctx context.Context, tmpl func(string) str
183
203
184
204
if n .conf .Subject != "" {
185
205
publishInput .SetSubject (tmpl (n .conf .Subject ))
206
+ if * tmplErr != nil {
207
+ return nil , errors .Wrap (* tmplErr , "execute 'subject' template" )
208
+ }
186
209
}
187
210
188
211
return publishInput , nil
0 commit comments