-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathout_kinesis_streams.rb
72 lines (63 loc) · 2.05 KB
/
out_kinesis_streams.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
#
# Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file 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.
require 'fluent/plugin/kinesis'
module Fluent
module Plugin
class KinesisStreamsOutput < KinesisOutput
Fluent::Plugin.register_output('kinesis_streams', self)
RequestType = :streams
BatchRequestLimitCount = 500
BatchRequestLimitSize = 5 * 1024 * 1024
include KinesisHelper::API::BatchRequest
config_param :stream_name, :string
config_param :partition_key, :string, default: nil
def configure(conf)
super
@key_formatter = key_formatter_create
end
def format(tag, time, record)
format_for_api do
data = @data_formatter.call(tag, time, record)
key = @key_formatter.call(record)
[data, key]
end
end
def write(chunk)
stream_name = extract_placeholders(@stream_name, chunk)
write_records_batch(chunk, stream_name) do |batch|
records = batch.map{|(data, partition_key)|
{ data: data, partition_key: partition_key }
}
client.put_records(
stream_name: stream_name,
records: records,
)
end
end
private
def key_formatter_create
if @partition_key.nil?
->(record) { SecureRandom.hex(16) }
else
->(record) {
if !record.key?(@partition_key)
raise KeyNotFoundError.new(@partition_key, record)
end
record[@partition_key]
}
end
end
end
end
end