-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from aianus/aianus/kinesis_device
Add support for logging to AWS Kinesis (basically a hosted Kafka)
- Loading branch information
Showing
6 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'aws-sdk' | ||
|
||
module LogStashLogger | ||
module Device | ||
class Kinesis < Connectable | ||
|
||
DEFAULT_REGION = 'us-east-1' | ||
DEFAULT_STREAM = 'logstash' | ||
RECOVERABLE_ERROR_CODES = [ | ||
"ServiceUnavailable", | ||
"Throttling", | ||
"RequestExpired", | ||
"ProvisionedThroughputExceededException" | ||
] | ||
|
||
attr_accessor :aws_region, :stream | ||
|
||
def initialize(opts) | ||
super | ||
@access_key_id = opts[:aws_access_key_id] || ENV['AWS_ACCESS_KEY_ID'] | ||
@secret_access_key = opts[:aws_secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY'] | ||
@aws_region = opts[:aws_region] || DEFAULT_REGION | ||
@stream = opts[:stream] || DEFAULT_STREAM | ||
end | ||
|
||
def connect | ||
@io = ::Aws::Kinesis::Client.new( | ||
region: @aws_region, | ||
credentials: ::Aws::Credentials.new(@access_key_id, @secret_access_key) | ||
) | ||
end | ||
|
||
def with_connection | ||
connect unless connected? | ||
yield | ||
rescue => e | ||
log_error(e) | ||
log_warning("giving up") | ||
end | ||
|
||
def write_batch(messages, group = nil) | ||
kinesis_records = messages.map do |message| | ||
{ | ||
data: message, | ||
partition_key: SecureRandom.uuid | ||
} | ||
end | ||
|
||
with_connection do | ||
resp = @io.put_records({ | ||
records: kinesis_records, | ||
stream_name: @stream | ||
}) | ||
|
||
# Put any failed records back into the buffer | ||
if resp.failed_record_count != 0 | ||
resp.records.each_with_index do |record, index| | ||
if RECOVERABLE_ERROR_CODES.include?(record.error_code) | ||
log_warning("Failed to post record to kinesis with error: #{record.error_code} #{record.error_message}") | ||
log_warning("Retrying") | ||
write(kinesis_records[index][:data]) | ||
elsif !record.error_code.nil? && record.error_code != '' | ||
log_error("Failed to post record to kinesis with error: #{record.error_code} #{record.error_message}") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def write_one(message) | ||
write_batch([message]) | ||
end | ||
|
||
def close! | ||
@io = nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'logstash-logger' | ||
|
||
describe LogStashLogger::Device::Kinesis do | ||
include_context 'device' | ||
|
||
let(:client) { double("Aws::Kinesis::Client") } | ||
|
||
before(:each) do | ||
allow(Aws::Kinesis::Client).to receive(:new) { client } | ||
end | ||
|
||
it "writes to a Kinesis stream" do | ||
response = ::Aws::Kinesis::Types::PutRecordsOutput.new | ||
response.failed_record_count = 0 | ||
response.records = [] | ||
expect(client).to receive(:put_records) { response } | ||
kinesis_device.write "foo" | ||
|
||
expect(kinesis_device).to be_connected | ||
kinesis_device.close! | ||
expect(kinesis_device).not_to be_connected | ||
end | ||
|
||
it "it puts records with recoverable errors back in the buffer" do | ||
failed_record = ::Aws::Kinesis::Types::PutRecordsResultEntry.new | ||
failed_record.error_code = "ProvisionedThroughputExceededException" | ||
failed_record.error_message = "ProvisionedThroughputExceededException" | ||
response = ::Aws::Kinesis::Types::PutRecordsOutput.new | ||
response.failed_record_count = 1 | ||
response.records = [failed_record] | ||
|
||
expect(client).to receive(:put_records) { response } | ||
expect(kinesis_device).to receive(:write).with("foo") | ||
|
||
kinesis_device.write_one "foo" | ||
end | ||
|
||
it "defaults the AWS region to us-east-1" do | ||
expect(kinesis_device.aws_region).to eq('us-east-1') | ||
end | ||
|
||
it "defaults the kinesis stream to logstash" do | ||
expect(kinesis_device.stream).to eq('logstash') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters