|
| 1 | +require "logstash/outputs/base" |
| 2 | +require "logstash/namespace" |
| 3 | +require 'logstash/plugin_mixins/aws_config' |
| 4 | + |
| 5 | +require "aws-sdk-firehose" |
| 6 | + |
| 7 | +# Push events to an Amazon Web Services (AWS) Data Firehose. |
| 8 | +# |
| 9 | +# Amazon Data Firehose is a fully managed service for delivering real-time streaming data to destinations |
| 10 | +# such as Amazon services or HTTP endpoints owned by supported third-party service providers. |
| 11 | +# See : https://docs.aws.amazon.com/firehose/latest/dev/what-is-this-service.html |
| 12 | +# |
| 13 | +# This plugin use the AWS SDK to send data to the Firehose stream. |
| 14 | +# See https://docs.aws.amazon.com/firehose/latest/dev/basic-write.html#writing-with-sdk |
| 15 | +# |
| 16 | +# Your identity must have the following permissions on the stream: |
| 17 | +# * `firehose:PutRecordBatch` |
| 18 | +# |
| 19 | +# ==== Batch Publishing |
| 20 | +# This output publishes messages to Firehose in batches in order to optimize event throughput and increase performance. |
| 21 | +# This is done using the `PutRecordBatch` API. |
| 22 | +# See https://docs.aws.amazon.com/firehose/latest/APIReference/API_PutRecordBatch.html |
| 23 | +# |
| 24 | +# When publishing messages to Firehose in batches, the following service limits must be respected : |
| 25 | +# * Each PutRecordBatch request supports up to 500 records. |
| 26 | +# * Each record in the request can be as large as 1,000 KB. |
| 27 | +# * All records in the request can be as large as 4 MB. |
| 28 | +# |
| 29 | +# This plugin will dynamically adjust the size of the batch published to Firehose in |
| 30 | +# order to ensure that the total payload size does not exceed the limits. |
| 31 | +# |
| 32 | +class LogStash::Outputs::Firehose < LogStash::Outputs::Base |
| 33 | + include LogStash::PluginMixins::AwsConfig::V2 |
| 34 | + |
| 35 | + RECORDS_MAX_BATCH_COUNT = 500 |
| 36 | + RECORD_MAX_SIZE_BYTES = 1_000_000 |
| 37 | + RECORD_TOTAL_MAX_SIZE_BYTES = 4_000_000 |
| 38 | + REQUEST_RETRY_INTERVAL_SECONDS = 2 |
| 39 | + |
| 40 | + config_name "firehose" |
| 41 | + concurrency :shared |
| 42 | + default :codec, 'json' |
| 43 | + |
| 44 | + # The name of the delivery stream. |
| 45 | + # Note that this is just the name of the stream, not the URL or ARN. |
| 46 | + config :delivery_stream_name, :validate => :string, :required => true |
| 47 | + |
| 48 | + # The maximum number of records to be sent in each batch. |
| 49 | + config :batch_max_count, :validate => :number, :default => RECORDS_MAX_BATCH_COUNT |
| 50 | + |
| 51 | + # The maximum number of bytes for any record sent to Firehose. |
| 52 | + # Messages exceeding this size will be dropped. |
| 53 | + # See https://docs.aws.amazon.com/firehose/latest/APIReference/API_PutRecordBatch.html |
| 54 | + config :record_max_size_bytes, :validate => :bytes, :default => RECORD_MAX_SIZE_BYTES |
| 55 | + |
| 56 | + # The maximum number of bytes for all records sent to Firehose. |
| 57 | + # See https://docs.aws.amazon.com/firehose/latest/APIReference/API_PutRecordBatch.html |
| 58 | + config :record_total_max_size_bytes, :validate => :bytes, :default => RECORD_TOTAL_MAX_SIZE_BYTES |
| 59 | + |
| 60 | + def register |
| 61 | + if @batch_max_count > RECORDS_MAX_BATCH_COUNT |
| 62 | + raise LogStash::ConfigurationError, "The maximum batch size is #{RECORDS_MAX_BATCH_COUNT} records" |
| 63 | + elsif @batch_max_count < 1 |
| 64 | + raise LogStash::ConfigurationError, 'The batch size must be greater than 0' |
| 65 | + end |
| 66 | + |
| 67 | + if @record_max_size_bytes > RECORD_MAX_SIZE_BYTES |
| 68 | + raise LogStash::ConfigurationError, "The maximum record size is #{RECORD_MAX_SIZE_BYTES}" |
| 69 | + elsif @record_max_size_bytes < 1 |
| 70 | + raise LogStash::ConfigurationError, 'The record size must be greater than 0' |
| 71 | + end |
| 72 | + |
| 73 | + if @record_total_max_size_bytes > RECORD_TOTAL_MAX_SIZE_BYTES |
| 74 | + raise LogStash::ConfigurationError, "The maximum message size is #{RECORD_TOTAL_MAX_SIZE_BYTES}" |
| 75 | + elsif @record_total_max_size_bytes < 1 |
| 76 | + raise LogStash::ConfigurationError, 'The message size must be greater than 0' |
| 77 | + end |
| 78 | + |
| 79 | + @logger.info("New Firehose output", :delivery_stream_name => @delivery_stream_name, |
| 80 | + :batch_max_count => @batch_max_count, |
| 81 | + :record_max_size_bytes => @record_max_size_bytes, |
| 82 | + :record_total_max_size_bytes => @record_total_max_size_bytes) |
| 83 | + @firehose = Aws::Firehose::Client.new(aws_options_hash) |
| 84 | + end |
| 85 | + |
| 86 | + public def multi_receive_encoded(encoded_events) |
| 87 | + return if encoded_events.empty? |
| 88 | + |
| 89 | + @logger.debug("Multi receive encoded", :encoded_events => encoded_events) |
| 90 | + |
| 91 | + records_bytes = 0 |
| 92 | + records = [] |
| 93 | + |
| 94 | + encoded_events.each do |_, encoded| |
| 95 | + |
| 96 | + if encoded.bytesize > @record_max_size_bytes |
| 97 | + @logger.warn('Record exceeds maximum length and will be dropped', :record => encoded, :size => encoded.bytesize) |
| 98 | + next |
| 99 | + end |
| 100 | + |
| 101 | + if records.size >= @batch_max_count or (records_bytes + encoded.bytesize) > @record_total_max_size_bytes |
| 102 | + put_record_batch(records) |
| 103 | + records_bytes = 0 |
| 104 | + records = [] |
| 105 | + end |
| 106 | + |
| 107 | + records_bytes += encoded.bytesize |
| 108 | + records << { :data => encoded } |
| 109 | + end |
| 110 | + |
| 111 | + put_record_batch(records) unless records.empty? |
| 112 | + end |
| 113 | + |
| 114 | + def put_record_batch(records) |
| 115 | + return if records.nil? or records.empty? |
| 116 | + |
| 117 | + @logger.debug("Publishing records", :batch => records.size) |
| 118 | + |
| 119 | + begin |
| 120 | + put_response = @firehose.put_record_batch({ |
| 121 | + delivery_stream_name: @delivery_stream_name, |
| 122 | + records: records |
| 123 | + }) |
| 124 | + rescue => e |
| 125 | + @logger.error("Encountered an unexpected error submitting a batch request, will retry", |
| 126 | + message: e.message, exception: e.class, backtrace: e.backtrace) |
| 127 | + Stud.stoppable_sleep(REQUEST_RETRY_INTERVAL_SECONDS) |
| 128 | + retry |
| 129 | + end |
| 130 | + |
| 131 | + if put_response.failed_put_count == 0 |
| 132 | + @logger.debug("Published records successfully", :batch => records.size) |
| 133 | + return |
| 134 | + end |
| 135 | + |
| 136 | + put_response.request_responses |
| 137 | + .filter { |r| !r.error_code.nil? } |
| 138 | + .each do |response| |
| 139 | + @logger.warn('Record publish error, will be dropped', :response => response) |
| 140 | + end unless put_response.request_responses.nil? |
| 141 | + |
| 142 | + end |
| 143 | +end |
0 commit comments