This repository was archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdynamodb.rb
362 lines (301 loc) · 13.5 KB
/
dynamodb.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# encoding: utf-8
#
#Copyright 2015 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.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License 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 "logstash/inputs/base"
require "logstash/namespace"
require "securerandom"
require "thread"
require "socket"
require_relative "LogStashRecordProcessorFactory"
require_relative "DynamoDBLogParser"
require "logstash-input-dynamodb_jars"
require 'java'
java_import "com.amazonaws.AmazonClientException"
java_import "org.apache.log4j.LogManager"
java_import "org.apache.log4j.Level"
java_import "com.fasterxml.jackson.annotation.JsonInclude"
java_import "com.amazonaws.regions.RegionUtils"
module AmazonDynamoDB
include_package "com.amazonaws"
include_package "com.amazonaws.services.dynamodbv2"
include_package "com.amazonaws.services.dynamodbv2.streamsadapter"
include_package "com.amazonaws.services.dynamodbv2.model"
end
module AmazonCredentials
include_package "com.amazonaws.auth"
include_package "com.amazonaws.internal"
end
module DynamoDBBootstrap
include_package "com.amazonaws.dynamodb.bootstrap"
end
module CloudWatch
include_package "com.amazonaws.services.cloudwatch"
end
module KCL
include_package "com.amazonaws.services.kinesis.clientlibrary.lib.worker"
end
#DynamoDBStreams plugin that will first scan the DynamoDB table
#and then consume streams and push those records into Logstash
class LogStash::Inputs::DynamoDB < LogStash::Inputs::Base
config_name "dynamodb"
USER_AGENT = " logstash-input-dynamodb/1.0.0".freeze
LF_DYNAMODB = "dymamodb".freeze
LF_JSON_NO_BIN = "json_drop_binary".freeze
LF_PLAIN = "plain".freeze
LF_JSON_BIN_AS_TEXT = "json_binary_as_text".freeze
VT_KEYS_ONLY = "keys_only".freeze
VT_OLD_IMAGE = "old_image".freeze
VT_NEW_IMAGE = "new_image".freeze
VT_ALL_IMAGES = "new_and_old_images".freeze
default :codec, 'json'
# The name of the table to copy and stream through Logstash
config :table_name, :validate => :string, :required => true
# Configuration for what information from the scan and streams to include in the log.
# keys_only will return the hash and range keys along with the values for each entry
# new_image will return the entire new entry and keys
# old_image will return the entire entry before modification and keys (NOTE: Cannot perform scan when using this option)
# new_and_old_images will return the old entry before modification along with the new entry and keys
config :view_type, :validate => [VT_KEYS_ONLY, VT_OLD_IMAGE, VT_NEW_IMAGE, VT_ALL_IMAGES], :required => true
# Endpoint from which the table is located. Example: dynamodb.us-east-1.amazonaws.com
config :endpoint, :validate => :string, :required => true
# Endpoint from which streams should read. Example: streams.dynamodb.us-east-1.amazonaws.com
config :streams_endpoint, :validate => :string
# AWS credentials access key.
config :aws_access_key_id, :validate => :string, :default => ""
# AWS credentials secret access key.
config :aws_secret_access_key, :validate => :string, :default => ""
# A flag to indicate whether or not the plugin should scan the entire table before streaming new records.
# Streams will only push records that are less than 24 hours old, so in order to get the entire table
# an initial scan must be done.
config :perform_scan, :validate => :boolean, :default => true
# A string that uniquely identifies the KCL checkpointer name and cloudwatch metrics name.
# This is used when one worker leaves a shard so that another worker knows where to start again.
config :checkpointer, :validate => :string, :default => "logstash_input_dynamodb_cptr"
# Option to publish metrics to Cloudwatch using the checkpointer name.
config :publish_metrics, :validate => :boolean, :default => false
# Option to not automatically stream new data into logstash from DynamoDB streams.
config :perform_stream, :validate => :boolean, :default => true
# Number of read operations per second to perform when scanning the specified table.
config :read_ops, :validate => :number, :default => 1
# Number of threads to use when scanning the specified table
config :number_of_scan_threads, :validate => :number, :default => 1
# Number of threads to write to the logstash queue when scanning the table
config :number_of_write_threads, :validate => :number, :default => 1
# Configuation for how the logs will be transferred.
# plain is simply pass the message along without editing it.
# dynamodb will return just the data specified in the view_format in dynamodb format.
# For more information see: docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataFormat.html
# json_drop_binary will return just the data specified in the view_format in JSON while not including any binary values that were present.
# json_binary_as_text will return just the data specified in the view_format in JSON while including binary values as base64-encoded text.
config :log_format, :validate => [LF_PLAIN, LF_DYNAMODB, LF_JSON_NO_BIN, LF_JSON_BIN_AS_TEXT], :default => "plain"
public
def build_credentials
if !@aws_access_key_id.to_s.empty? and !@aws_secret_access_key.to_s.empty?
@logger.info("Using static credentials: " + @aws_access_key_id + ", " + @aws_secret_access_key)
basic = AmazonCredentials::BasicAWSCredentials.new(@aws_access_key_id, @aws_secret_access_key)
return AmazonCredentials::StaticCredentialsProvider.new(basic)
else
@logger.info("Using default provider chain")
return AmazonCredentials::DefaultAWSCredentialsProviderChain.new()
end # if neither aws access keys
end # def build_credentials
public
def register
LogStash::Logger.setup_log4j(@logger)
@host = Socket.gethostname
@logger.info("Tablename: " + @table_name)
@queue = SizedQueue.new(20)
@credentials = build_credentials()
@logger.info("Checkpointer: " + @checkpointer)
if @perform_scan and @view_type == VT_OLD_IMAGE
raise(LogStash::ConfigurationError, "Cannot perform scan with view type: " + @view_type + " configuration")
end
if @view_type == VT_ALL_IMAGES and !(@log_format == LF_PLAIN)
raise(LogStash::ConfigurationError, "Cannot show view_type: " + @view_type + ", with log_format: " + @log_format)
end
#Create DynamoDB Client
@client_configuration = AmazonDynamoDB::ClientConfiguration.new()
@client_configuration.setUserAgent(@client_configuration.getUserAgent() + USER_AGENT)
@dynamodb_client = AmazonDynamoDB::AmazonDynamoDBClient.new(@credentials, @client_configuration)
@logger.info(@dynamodb_client.to_s)
@dynamodb_client.setEndpoint(@endpoint)
@logger.info("DynamoDB endpoint: " + @endpoint)
@key_schema = Array.new
@table_description = @dynamodb_client.describeTable(@table_name).getTable()
key_iterator = @table_description.getKeySchema().iterator()
while(key_iterator.hasNext())
@key_schema.push(key_iterator.next().getAttributeName().to_s)
end
region = RegionUtils.getRegionByEndpoint(@endpoint)
@parser ||= Logstash::Inputs::DynamoDB::DynamoDBLogParser.new(@view_type, @log_format, @key_schema, region)
if @perform_stream
setup_stream
end # unless @perform_stream
end # def register
public
def run(logstash_queue)
$exit = false;
$logstash_queue = logstash_queue
run_with_catch(logstash_queue)
end
public
def stop
$exit = true
until @scan_queue.empty?
end
until @queue.empty?
@logger.info("Flushing rest of events in logstash queue")
event = @queue.pop()
queue_event(@parser.parse_stream(event), $logstash_queue, @host)
end # until [email protected]?
exit_threads
end
# Starts KCL app in a background thread
# Starts parallel scan if need be in a background thread
private
def run_with_catch(logstash_queue)
if @perform_scan
scan(logstash_queue)
end # if @perform_scan
# Once scan is finished, start kcl thread to read from streams
if @perform_stream
stream(logstash_queue)
end # unless @perform_stream
end # def run
private
def setup_stream
worker_id = SecureRandom.uuid()
@logger.info("WorkerId: " + worker_id)
dynamodb_streams_client = AmazonDynamoDB::AmazonDynamoDBStreamsClient.new(@credentials, @client_configuration)
adapter = Java::ComAmazonawsServicesDynamodbv2Streamsadapter::AmazonDynamoDBStreamsAdapterClient.new(@credentials)
if !@streams_endpoint.nil?
adapter.setEndpoint(@streams_endpoint)
dynamodb_streams_client.setEndpoint(@streams_endpoint)
@logger.info("DynamoDB Streams endpoint: " + @streams_endpoint)
else
raise(LogStash::ConfigurationError, "Cannot stream without a configured streams endpoint")
end # if not @streams_endpoint.to_s.empty?
stream_arn = nil
begin
stream_arn = @table_description.getLatestStreamArn()
stream_description = dynamodb_streams_client.describeStream(AmazonDynamoDB::DescribeStreamRequest.new() \
.withStreamArn(stream_arn)).getStreamDescription()
stream_status = stream_description.getStreamStatus()
stream_view_type = stream_description.getStreamViewType().to_s.downcase
unless (stream_view_type == @view_type or @view_type == VT_KEYS_ONLY or stream_view_type == VT_ALL_IMAGES)
raise(LogStash::ConfigurationError, "Cannot stream " + @view_type + " when stream is setup for " + stream_view_type)
end
while stream_status == "ENABLING"
if(stream_status == "ENABLING")
@logger.info("Sleeping until stream is enabled")
sleep(1)
end # if stream_status == "ENABLING"
stream_description = dynamodb_streams_client.describeStream(AmazonDynamoDB::DescribeStreamRequest.new() \
.withStreamArn(stream_arn)).getStreamDescription()
stream_status = stream_description.getStreamStatus()
end # while not active
if !(stream_status == "ENABLED")
raise(LogStash::PluginLoadingError, "No streams are enabled")
end # if not active
@logger.info("Stream Id: " + stream_arn)
rescue AmazonDynamoDB::ResourceNotFoundException => rnfe
raise(LogStash::PluginLoadingError, rnfe.message)
rescue AmazonClientException => ace
raise(LogStash::ConfigurationError, "AWS credentials invalid or not found in the provider chain\n" + ace.message)
end # begin
kcl_config = KCL::KinesisClientLibConfiguration.new(@checkpointer, stream_arn, @credentials, worker_id) \
.withInitialPositionInStream(KCL::InitialPositionInStream::TRIM_HORIZON)
cloudwatch_client = nil
if @publish_metrics
cloudwatch_client = CloudWatch::AmazonCloudWatchClient.new(@credentials)
else
kclMetricsLogger = LogManager.getLogger("com.amazonaws.services.kinesis.metrics")
kclMetricsLogger.setAdditivity(false)
kclMetricsLogger.setLevel(Level::OFF)
end # if @publish_metrics
@worker = KCL::Worker.new(Logstash::Inputs::DynamoDB::LogStashRecordProcessorFactory.new(@queue), kcl_config, adapter, @dynamodb_client, cloudwatch_client)
end # def setup_stream
private
def scan(logstash_queue)
@logger.info("Starting scan...")
@logstash_writer = DynamoDBBootstrap::BlockingQueueConsumer.new(@number_of_write_threads)
@connector = DynamoDBBootstrap::DynamoDBBootstrapWorker.new(@dynamodb_client, @read_ops, @table_name, @number_of_scan_threads)
start_table_copy_thread
@scan_queue = @logstash_writer.getQueue()
while true
if !@scan_queue.empty?
event = @scan_queue.take()
if event.getEntry().nil? and event.getSize() == -1
break
end # if event.isEmpty()
queue_event(@parser.parse_scan(event.getEntry(), event.getSize()), logstash_queue, @host)
else
sleep(0.01)
end
end # while true
end
private
def stream(logstash_queue)
@logger.info("Starting stream...")
start_kcl_thread
while !$exit
event = @queue.pop()
queue_event(@parser.parse_stream(event), logstash_queue, @host)
else
sleep(0.01)
end
end # while true
end
private
def exit_threads
unless @worker.nil?
@worker.shutdown()
end # unless @worker.nil?
unless @dynamodb_scan_thread.nil?
@dynamodb_scan_thread.exit
end # unless @dynamodb_scan_thread.nil?
unless @kcl_thread.nil?
@kcl_thread.exit
end # unless @kcl_thread.nil?
end # def exit_threads
public
def queue_event(event, logstash_queue, event_host)
logstash_event = LogStash::Event.new("message" => event, "host" => event_host)
decorate(logstash_event)
logstash_queue << logstash_event
end # def queue_event
private
def start_table_copy_thread
@dynamodb_scan_thread = Thread.new(@connector, @logstash_writer) {
begin
@connector.pipe(@logstash_writer)
rescue Exception => e
abort("Scanning the table caused an error.\n" + e.message)
end # begin
}
end # def start_table_copy_thread()
private
def start_kcl_thread
@kcl_thread = Thread.new(@worker) {
begin
@worker.run()
rescue Exception => e
abort("KCL worker encountered an error.\n" + e.message)
end # begin
}
end # def start_kcl_thread
end # class Logstash::Inputs::DynamoDB