/
sqs.rb
172 lines (155 loc) · 5.57 KB
/
sqs.rb
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
# encoding: utf-8
require "logstash/inputs/threadable"
require "logstash/namespace"
require "logstash/plugin_mixins/aws_config"
require "digest/sha2"
# Pull events from an Amazon Web Services Simple Queue Service (SQS) queue.
#
# SQS is a simple, scalable queue system that is part of the
# Amazon Web Services suite of tools.
#
# Although SQS is similar to other queuing systems like AMQP, it
# uses a custom API and requires that you have an AWS account.
# See http://aws.amazon.com/sqs/ for more details on how SQS works,
# what the pricing schedule looks like and how to setup a queue.
#
# To use this plugin, you *must*:
#
# * Have an AWS account
# * Setup an SQS queue
# * Create an identify that has access to consume messages from the queue.
#
# The "consumer" identity must have the following permissions on the queue:
#
# * sqs:ChangeMessageVisibility
# * sqs:ChangeMessageVisibilityBatch
# * sqs:DeleteMessage
# * sqs:DeleteMessageBatch
# * sqs:GetQueueAttributes
# * sqs:GetQueueUrl
# * sqs:ListQueues
# * sqs:ReceiveMessage
#
# Typically, you should setup an IAM policy, create a user and apply the IAM policy to the user.
# A sample policy is as follows:
#
# {
# "Statement": [
# {
# "Action": [
# "sqs:ChangeMessageVisibility",
# "sqs:ChangeMessageVisibilityBatch",
# "sqs:GetQueueAttributes",
# "sqs:GetQueueUrl",
# "sqs:ListQueues",
# "sqs:SendMessage",
# "sqs:SendMessageBatch"
# ],
# "Effect": "Allow",
# "Resource": [
# "arn:aws:sqs:us-east-1:123456789012:Logstash"
# ]
# }
# ]
# }
#
# See http://aws.amazon.com/iam/ for more details on setting up AWS identities.
#
class LogStash::Inputs::SQS < LogStash::Inputs::Threadable
include LogStash::PluginMixins::AwsConfig
config_name "sqs"
milestone 1
default :codec, "json"
# Name of the SQS Queue name to pull messages from. Note that this is just the name of the queue, not the URL or ARN.
config :queue, :validate => :string, :required => true
# Name of the event field in which to store the SQS message ID
config :id_field, :validate => :string
# Name of the event field in which to store the SQS message MD5 checksum
config :md5_field, :validate => :string
# Name of the event field in which to store the SQS message Sent Timestamp
config :sent_timestamp_field, :validate => :string
public
def aws_service_endpoint(region)
return {
:sqs_endpoint => "sqs.#{region}.amazonaws.com"
}
end
public
def register
@logger.info("Registering SQS input", :queue => @queue)
require "aws-sdk"
@sqs = AWS::SQS.new(aws_options_hash)
begin
@logger.debug("Connecting to AWS SQS queue", :queue => @queue)
@sqs_queue = @sqs.queues.named(@queue)
@logger.info("Connected to AWS SQS queue successfully.", :queue => @queue)
rescue Exception => e
@logger.error("Unable to access SQS queue.", :error => e.to_s, :queue => @queue)
throw e
end # begin/rescue
end # def register
public
def run(output_queue)
@logger.debug("Polling SQS queue", :queue => @queue)
receive_opts = {
:limit => 10,
:visibility_timeout => 30,
:attributes => [:sent_at]
}
continue_polling = true
while running? && continue_polling
continue_polling = run_with_backoff(60, 1) do
@sqs_queue.receive_message(receive_opts) do |message|
if message
@codec.decode(message.body) do |event|
decorate(event)
if @id_field
event[@id_field] = message.id
end
if @md5_field
event[@md5_field] = message.md5
end
if @sent_timestamp_field
event[@sent_timestamp_field] = message.sent_timestamp.utc
end
@logger.debug? && @logger.debug("Processed SQS message", :message_id => message.id, :message_md5 => message.md5, :sent_timestamp => message.sent_timestamp, :queue => @queue)
output_queue << event
message.delete
end # codec.decode
end # valid SQS message
end # receive_message
end # run_with_backoff
end # polling loop
end # def run
def teardown
@sqs_queue = nil
finished
end # def teardown
private
# Runs an AWS request inside a Ruby block with an exponential backoff in case
# we exceed the allowed AWS RequestLimit.
#
# @param [Integer] max_time maximum amount of time to sleep before giving up.
# @param [Integer] sleep_time the initial amount of time to sleep before retrying.
# @param [Block] block Ruby code block to execute.
def run_with_backoff(max_time, sleep_time, &block)
if sleep_time > max_time
@logger.error("AWS::EC2::Errors::RequestLimitExceeded ... failed.", :queue => @queue)
return false
end # retry limit exceeded
begin
block.call
rescue AWS::EC2::Errors::RequestLimitExceeded
@logger.info("AWS::EC2::Errors::RequestLimitExceeded ... retrying SQS request", :queue => @queue, :sleep_time => sleep_time)
sleep sleep_time
run_with_backoff(max_time, sleep_time * 2, &block)
rescue AWS::EC2::Errors::InstanceLimitExceeded
@logger.warn("AWS::EC2::Errors::InstanceLimitExceeded ... aborting SQS message retreival.")
return false
rescue Exception => bang
@logger.error("Error reading SQS queue.", :error => bang, :queue => @queue)
return false
end # begin/rescue
return true
end # def run_with_backoff
end # class LogStash::Inputs::SQS