-
Notifications
You must be signed in to change notification settings - Fork 741
/
start.go
224 lines (199 loc) · 7.16 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright 2018 BlackRock, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package awssqs
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
sqslib "github.com/aws/aws-sdk-go/service/sqs"
"go.uber.org/zap"
"github.com/argoproj/argo-events/common/logging"
eventsourcecommon "github.com/argoproj/argo-events/eventsources/common"
awscommon "github.com/argoproj/argo-events/eventsources/common/aws"
"github.com/argoproj/argo-events/eventsources/sources"
metrics "github.com/argoproj/argo-events/metrics"
apicommon "github.com/argoproj/argo-events/pkg/apis/common"
"github.com/argoproj/argo-events/pkg/apis/events"
"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
)
// EventListener implements Eventing for aws sqs event source
type EventListener struct {
EventSourceName string
EventName string
SQSEventSource v1alpha1.SQSEventSource
Metrics *metrics.Metrics
}
// GetEventSourceName returns name of event source
func (el *EventListener) GetEventSourceName() string {
return el.EventSourceName
}
// GetEventName returns name of event
func (el *EventListener) GetEventName() string {
return el.EventName
}
// GetEventSourceType return type of event server
func (el *EventListener) GetEventSourceType() apicommon.EventSourceType {
return apicommon.SQSEvent
}
// StartListening starts listening events
func (el *EventListener) StartListening(ctx context.Context, dispatch func([]byte, ...eventsourcecommon.Option) error) error {
log := logging.FromContext(ctx).
With(logging.LabelEventSourceType, el.GetEventSourceType(), logging.LabelEventName, el.GetEventName())
log.Info("started processing the AWS SQS event source...")
defer sources.Recover(el.GetEventName())
sqsEventSource := &el.SQSEventSource
sqsClient, err := el.createSqsClient()
if err != nil {
return err
}
log.Info("fetching queue url...")
getQueueURLInput := &sqslib.GetQueueUrlInput{
QueueName: &sqsEventSource.Queue,
}
if sqsEventSource.QueueAccountID != "" {
getQueueURLInput = getQueueURLInput.SetQueueOwnerAWSAccountId(sqsEventSource.QueueAccountID)
}
queueURL, err := sqsClient.GetQueueUrl(getQueueURLInput)
if err != nil {
log.Errorw("Error getting SQS Queue URL", zap.Error(err))
return fmt.Errorf("failed to get the queue url for %s, %w", el.GetEventName(), err)
}
if sqsEventSource.JSONBody {
log.Info("assuming all events have a json body...")
}
log.Info("listening for messages on the queue...")
for {
select {
case <-ctx.Done():
log.Info("exiting SQS event listener...")
return nil
default:
}
messages, err := fetchMessages(ctx, sqsClient, *queueURL.QueueUrl, 10, sqsEventSource.WaitTimeSeconds)
if err != nil {
log.Errorw("failed to get messages from SQS", zap.Error(err))
awsError, ok := err.(awserr.Error)
if ok && awsError.Code() == "ExpiredToken" && el.SQSEventSource.SessionToken != nil {
log.Info("credentials expired, reading credentials again")
newSqsClient, err := el.createSqsClient()
if err != nil {
log.Errorw("Error creating SQS client", zap.Error(err))
} else if newSqsClient != nil {
sqsClient = newSqsClient
}
}
time.Sleep(2 * time.Second)
continue
}
for _, m := range messages {
el.processMessage(m, dispatch, func() {
_, err = sqsClient.DeleteMessage(&sqslib.DeleteMessageInput{
QueueUrl: queueURL.QueueUrl,
ReceiptHandle: m.ReceiptHandle,
})
if err != nil {
log.Errorw("Failed to delete message", zap.Error(err))
awsError, ok := err.(awserr.Error)
if ok && awsError.Code() == "ExpiredToken" && el.SQSEventSource.SessionToken != nil {
log.Info("credentials expired, reading credentials again")
newSqsClient, err := el.createSqsClient()
if err != nil {
log.Errorw("Error creating SQS client", zap.Error(err))
} else if newSqsClient != nil {
sqsClient = newSqsClient
}
}
}
}, log)
}
}
}
func (el *EventListener) processMessage(message *sqslib.Message, dispatch func([]byte, ...eventsourcecommon.Option) error, ack func(), log *zap.SugaredLogger) {
defer func(start time.Time) {
el.Metrics.EventProcessingDuration(el.GetEventSourceName(), el.GetEventName(), float64(time.Since(start)/time.Millisecond))
}(time.Now())
data := &events.SQSEventData{
MessageId: *message.MessageId,
MessageAttributes: message.MessageAttributes,
Metadata: el.SQSEventSource.Metadata,
}
if el.SQSEventSource.JSONBody {
body := []byte(*message.Body)
data.Body = (*json.RawMessage)(&body)
} else {
data.Body = []byte(*message.Body)
}
eventBytes, err := json.Marshal(data)
if err != nil {
log.Errorw("failed to marshal event data, will process next message...", zap.Error(err))
el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName())
// Don't ack if a DLQ is configured to allow to forward the message to the DLQ
if !el.SQSEventSource.DLQ {
ack()
}
return
}
if err = dispatch(eventBytes); err != nil {
log.Errorw("failed to dispatch SQS event", zap.Error(err))
el.Metrics.EventProcessingFailed(el.GetEventSourceName(), el.GetEventName())
} else {
ack()
}
}
func fetchMessages(ctx context.Context, q *sqslib.SQS, url string, maxSize, waitSeconds int64) ([]*sqslib.Message, error) {
if waitSeconds == 0 {
// Defaults to 3 seconds
waitSeconds = 3
}
result, err := q.ReceiveMessageWithContext(ctx, &sqslib.ReceiveMessageInput{
AttributeNames: []*string{
aws.String(sqslib.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqslib.QueueAttributeNameAll),
},
QueueUrl: &url,
MaxNumberOfMessages: aws.Int64(maxSize),
VisibilityTimeout: aws.Int64(120), // 120 seconds
WaitTimeSeconds: aws.Int64(waitSeconds),
})
if err != nil {
return nil, err
}
return result.Messages, nil
}
func (el *EventListener) createAWSSession() (*session.Session, error) {
sqsEventSource := &el.SQSEventSource
awsSession, err := awscommon.CreateAWSSessionWithCredsInVolume(sqsEventSource.Region, sqsEventSource.RoleARN, sqsEventSource.AccessKey, sqsEventSource.SecretKey, sqsEventSource.SessionToken)
if err != nil {
return nil, fmt.Errorf("failed to create aws session for %s, %w", el.GetEventName(), err)
}
return awsSession, nil
}
func (el *EventListener) createSqsClient() (*sqslib.SQS, error) {
awsSession, err := el.createAWSSession()
if err != nil {
return nil, err
}
var sqsClient *sqslib.SQS
if el.SQSEventSource.Endpoint == "" {
sqsClient = sqslib.New(awsSession)
} else {
sqsClient = sqslib.New(awsSession, &aws.Config{Endpoint: &el.SQSEventSource.Endpoint, Region: &el.SQSEventSource.Region})
}
return sqsClient, nil
}