Class: Fluent::Plugin::DynamoDBStreamsInput
- Inherits:
-
Input
- Object
- Input
- Fluent::Plugin::DynamoDBStreamsInput
- Defined in:
- lib/fluent/plugin/in_dynamodb_streams.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #dynamodb_to_hash(hash) ⇒ Object
- #emit(r) ⇒ Object
- #format_attribute_value(v) ⇒ Object
- #get_shards ⇒ Object
-
#initialize ⇒ DynamoDBStreamsInput
constructor
A new instance of DynamoDBStreamsInput.
- #load_sequence(shard_id) ⇒ Object
- #remove_sequence(shard_id) ⇒ Object
- #run ⇒ Object
- #save_sequence(shard_id, sequence) ⇒ Object
- #set_iterator(shard_id) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ DynamoDBStreamsInput
Returns a new instance of DynamoDBStreamsInput.
11 12 13 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 11 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 24 def configure(conf) super if @aws_region == "ddblocal" @aws_region = "ap-northeast-1" # dummy settings @stream_endpoint = "http://localhost:8000" else @stream_endpoint = "https://streams.dynamodb.#{@aws_region}.amazonaws.com" end unless @pos_file log.warn "dynamodb-streams: 'pos_file PATH' parameter is not set to a 'dynamodb-streams' source." log.warn "dynamodb-streams: this parameter is highly recommended to save the position to resume." end end |
#dynamodb_to_hash(hash) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 180 def dynamodb_to_hash(hash) hash.each do |k, v| # delete binary attributes if v.b || v.bs hash.delete(k) else hash[k] = format_attribute_value(v) end end return hash end |
#emit(r) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 161 def emit(r) record = { "aws_region" => r.aws_region, "event_source" => r.event_source, "event_version" => r.event_version, "event_id" => r.event_id, "event_name" => r.event_name, "dynamodb" => { "stream_view_type" => r.dynamodb.stream_view_type, "sequence_number" => r.dynamodb.sequence_number, "size_bytes" => r.dynamodb.size_bytes, } } record["dynamodb"]["keys"] = dynamodb_to_hash(r.dynamodb.keys) if r.dynamodb.keys record["dynamodb"]["old_image"] = dynamodb_to_hash(r.dynamodb.old_image) if r.dynamodb.old_image record["dynamodb"]["new_image"] = dynamodb_to_hash(r.dynamodb.new_image) if r.dynamodb.new_image router.emit(@tag, Fluent::Engine.now, record) end |
#format_attribute_value(v) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 192 def format_attribute_value(v) if v.m return dynamodb_to_hash(v.m) elsif v.l return v.l.map {|i| format_attribute_value(i) } elsif v.ns return v.ns.map {|i| BigDecimal.new(i).to_i } elsif v.ss return v.ss elsif v.null return null elsif v.bool return v.bool elsif v.n return BigDecimal.new(v.n).to_i elsif v.s return v.s else log.warn "dynamodb-streams: unknown attribute value." end end |
#get_shards ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 93 def get_shards() shards = [] last_shard_id = nil begin s = @client.describe_stream({ stream_arn: @stream_arn, exclusive_start_shard_id: last_shard_id, }).stream_description shards = shards + s.shards if s.last_evaluated_shard_id == last_shard_id then break end last_shard_id = s.last_evaluated_shard_id end while last_shard_id shards end |
#load_sequence(shard_id) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 131 def load_sequence(shard_id) if @pos_file return nil unless File.exist?("#{@pos_file}.#{shard_id}") File.read("#{@pos_file}.#{shard_id}").chomp else return nil unless @pos_memory[shard_id] @pos_memory[shard_id] end end |
#remove_sequence(shard_id) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 152 def remove_sequence(shard_id) if @pos_file return unless File.exist?("#{@pos_file}.#{shard_id}") File.unlink("#{@pos_file}.#{shard_id}") else @pos_memory[shard_id] = nil end end |
#run ⇒ Object
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 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 62 def run get_shards.each do |s| if s.sequence_number_range.ending_sequence_number remove_sequence(s.shard_id) next end set_iterator(s.shard_id) unless @iterator.key? s.shard_id resp = @client.get_records({ shard_iterator: @iterator[s.shard_id], limit: @fetch_size, }) resp.records.each do |r| begin emit(r) rescue => e log.error "dynamodb-streams: error has occoured.", error: e., error_class: e.class end save_sequence(s.shard_id, r.dynamodb.sequence_number) end if resp.next_shard_iterator @iterator[s.shard_id] = resp.next_shard_iterator else @iterator.delete s.shard_id end end end |
#save_sequence(shard_id, sequence) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 141 def save_sequence(shard_id, sequence) if @pos_file open("#{@pos_file}.#{shard_id}", 'w') do |f| f.write sequence end else @pos_memory[shard_id] = sequence end sequence end |
#set_iterator(shard_id) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 114 def set_iterator(shard_id) if load_sequence(shard_id) @iterator[shard_id] = @client.get_shard_iterator({ stream_arn: @stream_arn, shard_id: shard_id, shard_iterator_type: "AFTER_SEQUENCE_NUMBER", sequence_number: load_sequence(shard_id), }).shard_iterator else @iterator[shard_id] = @client.get_shard_iterator({ stream_arn: @stream_arn, shard_id: shard_id, shard_iterator_type: "TRIM_HORIZON", }).shard_iterator end end |
#shutdown ⇒ Object
58 59 60 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 58 def shutdown super end |
#start ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 40 def start super unless @pos_file @pos_memory = {} end = {} [:region] = @aws_region if @aws_region [:credentials] = Aws::Credentials.new(@aws_key_id, @aws_sec_key) if @aws_key_id && @aws_sec_key [:endpoint] = @stream_endpoint @client = Aws::DynamoDBStreams::Client.new() @iterator = {} timer_execute(:in_dynamodb_streams_timer, @fetch_interval, &method(:run)) end |