Class: Fluent::DynamoDBStreamsInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_dynamodb_streams.rb

Instance Method Summary collapse

Constructor Details

#initializeDynamoDBStreamsInput

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



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 169

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 150

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 181

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



120
121
122
123
124
125
126
127
128
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 120

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



141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 141

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

#runObject



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
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 65

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

      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.message, 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
end

#save_sequence(shard_id, sequence) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 130

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 103

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

#shutdownObject



60
61
62
63
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 60

def shutdown
  @running = false
  @thread.join
end

#startObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fluent/plugin/in_dynamodb_streams.rb', line 41

def start
  super

  unless @pos_file
    @pos_memory = {}
  end
    
  options = {}
  options[:region] = @aws_region if @aws_region
  options[:credentials] = Aws::Credentials.new(@aws_key_id, @aws_sec_key) if @aws_key_id && @aws_sec_key
  options[:endpoint] = @stream_endpoint
  @client = Aws::DynamoDBStreams::Client.new(options)

  @iterator = {}

  @running = true
  @thread = Thread.new(&method(:run))
end