|
1 | 1 | require 'aws-sdk-dynamodb'
|
| 2 | +require 'fluent/plugin/output' |
| 3 | + |
| 4 | +class Fluent::Plugin::DynamodbAdd < Fluent::Plugin::Output |
| 5 | + Fluent::Plugin.register_output('dynamodb_add', self) |
| 6 | + |
| 7 | + helpers :event_emitter |
| 8 | + helpers :compat_parameters |
| 9 | + |
| 10 | + config_param :count_key, :string |
| 11 | + config_param :dynamo_count_key, :string |
| 12 | + config_param :table_name, :string |
| 13 | + config_param :use_iam_role, :bool, :default => false |
| 14 | + config_param :aws_key_id, :string, :default => nil |
| 15 | + config_param :aws_sec_key, :string, :default => nil |
| 16 | + config_param :region, :string, :default => nil |
| 17 | + config_param :endpoint, :string, :default => nil |
| 18 | + config_param :hash_key, :string, :default => nil |
| 19 | + config_param :hash_key_delimiter, :string, :default => ":" |
| 20 | + config_param :add_hash_key_prefix, :string, :default => nil |
| 21 | + config_param :range_key, :string, :default => nil |
| 22 | + config_param :set_timestamp, :string, :default => nil |
| 23 | + |
| 24 | + def initialize |
| 25 | + super |
| 26 | + end |
2 | 27 |
|
3 |
| -module Fluent |
4 |
| - class DynamodbAdd < Fluent::Output |
5 |
| - Fluent::Plugin.register_output('dynamodb_add', self) |
6 |
| - |
7 |
| - unless method_defined?(:log) |
8 |
| - define_method(:log) { $log } |
9 |
| - end |
10 |
| - |
11 |
| - config_param :count_key, :string |
12 |
| - config_param :dynamo_count_key, :string |
13 |
| - config_param :table_name, :string |
14 |
| - config_param :use_iam_role, :bool, :default => false |
15 |
| - config_param :aws_key_id, :string, :default => nil |
16 |
| - config_param :aws_sec_key, :string, :default => nil |
17 |
| - config_param :region, :string, :default => nil |
18 |
| - config_param :endpoint, :string, :default => nil |
19 |
| - config_param :hash_key, :string, :default => nil |
20 |
| - config_param :hash_key_delimiter, :string, :default => ":" |
21 |
| - config_param :add_hash_key_prefix, :string, :default => nil |
22 |
| - config_param :range_key, :string, :default => nil |
23 |
| - config_param :set_timestamp, :string, :default => nil |
24 |
| - |
25 |
| - def initialize |
26 |
| - super |
27 |
| - end |
| 28 | + def configure(conf) |
| 29 | + compat_parameters_convert(conf) |
28 | 30 |
|
29 |
| - def configure(conf) |
30 |
| - super |
| 31 | + super |
31 | 32 |
|
32 |
| - unless use_iam_role |
33 |
| - [:aws_key_id, :aws_sec_key].each do |name| |
34 |
| - unless self.instance_variable_get("@#{name}") |
35 |
| - raise ConfigError, "'#{name}' is required" |
36 |
| - end |
| 33 | + unless use_iam_role |
| 34 | + [:aws_key_id, :aws_sec_key].each do |name| |
| 35 | + unless self.instance_variable_get("@#{name}") |
| 36 | + raise ConfigError, "'#{name}' is required" |
37 | 37 | end
|
38 | 38 | end
|
39 |
| - @hash_key = hash_key.split(/\s*,\s*/) |
40 | 39 | end
|
| 40 | + @hash_key = hash_key.split(/\s*,\s*/) |
| 41 | + end |
41 | 42 |
|
42 |
| - def start |
43 |
| - super |
| 43 | + def start |
| 44 | + super |
44 | 45 |
|
45 |
| - options = {} |
| 46 | + options = {} |
46 | 47 |
|
47 |
| - unless use_iam_role |
48 |
| - options[:access_key_id] = @aws_key_id |
49 |
| - options[:secret_access_key] = @aws_sec_key |
50 |
| - end |
| 48 | + unless use_iam_role |
| 49 | + options[:access_key_id] = @aws_key_id |
| 50 | + options[:secret_access_key] = @aws_sec_key |
| 51 | + end |
51 | 52 |
|
52 |
| - options[:region] = region |
53 |
| - options[:endpoint] = endpoint |
| 53 | + options[:region] = region |
| 54 | + options[:endpoint] = endpoint |
54 | 55 |
|
55 |
| - client = Aws::DynamoDB::Client.new(options) |
| 56 | + client = Aws::DynamoDB::Client.new(options) |
56 | 57 |
|
57 |
| - resource = Aws::DynamoDB::Resource.new(client: client) |
58 |
| - @table = resource.table(table_name) |
| 58 | + resource = Aws::DynamoDB::Resource.new(client: client) |
| 59 | + @table = resource.table(table_name) |
59 | 60 |
|
60 |
| - @dynamo_hash_key = @table.key_schema.find{|e| e.key_type == "HASH" }.attribute_name |
61 |
| - @dynamo_range_key = @table.key_schema.find{|e| e.key_type == "RANGE" }&.attribute_name |
62 |
| - end |
| 61 | + @dynamo_hash_key = @table.key_schema.find{|e| e.key_type == "HASH" }.attribute_name |
| 62 | + @dynamo_range_key = @table.key_schema.find{|e| e.key_type == "RANGE" }&.attribute_name |
| 63 | + end |
63 | 64 |
|
64 |
| - def emit(tag, es, chain) |
65 |
| - chain.next |
66 |
| - es.each do |time, record| |
67 |
| - hash_key = create_key(record) |
68 |
| - next unless hash_key || record[@count_key] |
| 65 | + def process(tag, es) |
| 66 | + es.each do |time, record| |
| 67 | + hash_key = create_key(record) |
| 68 | + next unless hash_key || record[@count_key] |
69 | 69 |
|
70 |
| - key = { @dynamo_hash_key => hash_key } |
| 70 | + key = { @dynamo_hash_key => hash_key } |
71 | 71 |
|
72 |
| - if @range_key |
73 |
| - next unless record[@range_key] |
74 |
| - key[@dynamo_range_key] = record[@range_key] |
75 |
| - end |
| 72 | + if @range_key |
| 73 | + next unless record[@range_key] |
| 74 | + key[@dynamo_range_key] = record[@range_key] |
| 75 | + end |
76 | 76 |
|
77 |
| - @table.update_item({ |
78 |
| - key: key, |
79 |
| - attribute_updates: { |
80 |
| - @dynamo_count_key => { |
81 |
| - value: record[@count_key], |
82 |
| - action: "ADD" |
83 |
| - }, |
| 77 | + @table.update_item({ |
| 78 | + key: key, |
| 79 | + attribute_updates: { |
| 80 | + @dynamo_count_key => { |
| 81 | + value: record[@count_key], |
| 82 | + action: "ADD" |
84 | 83 | },
|
85 |
| - }) |
86 |
| - end |
| 84 | + }, |
| 85 | + }) |
87 | 86 | end
|
| 87 | + end |
88 | 88 |
|
89 |
| - private |
| 89 | + private |
90 | 90 |
|
91 |
| - def create_key(record) |
92 |
| - key_array = [] |
93 |
| - key_array << @add_hash_key_prefix if @add_hash_key_prefix |
94 |
| - @hash_key.each do |h| |
95 |
| - return nil unless record[h] |
96 |
| - key_array << record[h] |
97 |
| - end |
98 |
| - key_array.join(@hash_key_delimiter) |
| 91 | + def create_key(record) |
| 92 | + key_array = [] |
| 93 | + key_array << @add_hash_key_prefix if @add_hash_key_prefix |
| 94 | + @hash_key.each do |h| |
| 95 | + return nil unless record[h] |
| 96 | + key_array << record[h] |
99 | 97 | end
|
| 98 | + key_array.join(@hash_key_delimiter) |
100 | 99 | end
|
101 | 100 | end
|
0 commit comments