Class: Fluent::DynamoDBStreamsInput
- Inherits:
-
Input
- Object
- Input
- Fluent::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
-
#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
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ DynamoDBStreamsInput
Returns a new instance of DynamoDBStreamsInput.
10 11 12 13 14 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 10 def initialize super require 'aws-sdk' require 'bigdecimal' end |
Instance Method Details
#configure(conf) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 25 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
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 157 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
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 138 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, Time.now.to_i, record) end |
#format_attribute_value(v) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 169 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 |
#load_sequence(shard_id) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 108 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
129 130 131 132 133 134 135 136 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 129 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
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 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 63 def run while @running sleep @fetch_interval @client.describe_stream({ stream_arn: @stream_arn }).stream_description.shards.each do |s| if s.sequence_number_range.ending_sequence_number remove_sequence(s.shard_id) next end if load_sequence(s.shard_id) iterator = @client.get_shard_iterator({ stream_arn: @stream_arn, shard_id: s.shard_id, shard_iterator_type: "AFTER_SEQUENCE_NUMBER", sequence_number: load_sequence(s.shard_id), }).shard_iterator else iterator = @client.get_shard_iterator({ stream_arn: @stream_arn, shard_id: s.shard_id, shard_iterator_type: "TRIM_HORIZON", }).shard_iterator end resp = @client.get_records({ shard_iterator: iterator, 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 end end end |
#save_sequence(shard_id, sequence) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 118 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 |
#shutdown ⇒ Object
58 59 60 61 |
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 58 def shutdown @running = false @thread.join end |
#start ⇒ Object
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 41 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() @running = true @thread = Thread.new(&method(:run)) end |