fluent-plugin-mysql-fetch-and-emit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a703eef6955132e07052f56cfbab2e60c8d8cca4c751412208e21204f292ac7a
4
+ data.tar.gz: 6ddc5374fcbaf7f0e633f406c5e50d099e5fb43f405a5f6ae2fc0f420f7a089e
5
+ SHA512:
6
+ metadata.gz: 406424be8b299c036ebe6e814943fea5dee118154f2a27a45f3e61d2f996b26fe5c4d7de8d811c4c1a2829a83bc0a8ff48a809d3510dd96db8d3679933ab2075
7
+ data.tar.gz: 419361782102a6efb70f7cd0e1dcf03ae60a0506ea1b5f4dc4166acc612f6905a4f69a8f828ca02419aacc03d74802b3c0ea8ff07e00a1482e493c48bf48c269
@@ -0,0 +1,58 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.5.0
11
+ - image: circleci/mysql:5.7
12
+ environment:
13
+ MYSQL_ALLOW_EMPTY_PASSWORD: yes
14
+ MYSQL_DATABASE: sample
15
+ MYSQL_HOST: "127.0.0.1"
16
+ MYSQL_ROOT_HOST: "%"
17
+ MYSQL_USER: root
18
+
19
+ working_directory: ~/repo
20
+
21
+ steps:
22
+ - checkout
23
+
24
+ # Download and cache dependencies
25
+ - restore_cache:
26
+ keys:
27
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
28
+ # fallback to using the latest cache if no exact match is found
29
+ - v1-dependencies-
30
+
31
+ - run:
32
+ name: install dependencies
33
+ command: |
34
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
35
+
36
+ - save_cache:
37
+ paths:
38
+ - ./vendor/bundle
39
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
40
+
41
+ - run:
42
+ name: install dockerize
43
+ command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
44
+ environment:
45
+ DOCKERIZE_VERSION: v0.3.0
46
+
47
+ - run:
48
+ name: Wait for db
49
+ command: dockerize -wait tcp://localhost:3306 -timeout 1m
50
+
51
+ # Database setup
52
+ - run: bundle exec rake circleci:db:create
53
+
54
+ # run tests!
55
+ - run:
56
+ name: run tests
57
+ command: |
58
+ bundle exec rake test
@@ -0,0 +1 @@
1
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fluent-plugin-mysql-fetch-and-emit (0.1.0)
5
+ fluentd (>= 0.14.10, < 2)
6
+ mysql2
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ cool.io (1.5.3)
12
+ dig_rb (1.0.1)
13
+ fluentd (1.1.3)
14
+ cool.io (>= 1.4.5, < 2.0.0)
15
+ dig_rb (~> 1.0.0)
16
+ http_parser.rb (>= 0.5.1, < 0.7.0)
17
+ msgpack (>= 0.7.0, < 2.0.0)
18
+ serverengine (>= 2.0.4, < 3.0.0)
19
+ sigdump (~> 0.2.2)
20
+ strptime (>= 0.2.2, < 1.0.0)
21
+ tzinfo (~> 1.0)
22
+ tzinfo-data (~> 1.0)
23
+ yajl-ruby (~> 1.0)
24
+ http_parser.rb (0.6.0)
25
+ msgpack (1.2.4)
26
+ mysql2 (0.5.0)
27
+ power_assert (1.1.1)
28
+ rake (12.3.1)
29
+ serverengine (2.0.6)
30
+ sigdump (~> 0.2.2)
31
+ sigdump (0.2.4)
32
+ strptime (0.2.3)
33
+ test-unit (3.2.7)
34
+ power_assert
35
+ thread_safe (0.3.6)
36
+ tzinfo (1.2.5)
37
+ thread_safe (~> 0.1)
38
+ tzinfo-data (1.2018.4)
39
+ tzinfo (>= 1.0.0)
40
+ yajl-ruby (1.3.1)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ bundler (~> 1.14)
47
+ fluent-plugin-mysql-fetch-and-emit!
48
+ rake (~> 12.0)
49
+ test-unit (~> 3.0)
50
+
51
+ BUNDLED WITH
52
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,212 @@
1
+ # fluent-plugin-mysql-fetch-and-emit
2
+ [![CircleCI](https://circleci.com/gh/joker1007/fluent-plugin-mysql-fetch-and-emit.svg?style=svg)](https://circleci.com/gh/joker1007/fluent-plugin-mysql-fetch-and-emit)
3
+
4
+ [Fluentd](https://fluentd.org/) output plugin to fetch from mysql by fluentd record and re-emit fetched record.
5
+
6
+ ## Installation
7
+
8
+ ### RubyGems
9
+
10
+ ```
11
+ $ gem install fluent-plugin-mysql-fetch-and-emit
12
+ ```
13
+
14
+ ### Bundler
15
+
16
+ Add following line to your Gemfile:
17
+
18
+ ```ruby
19
+ gem "fluent-plugin-mysql-fetch-and-emit"
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```
25
+ $ bundle
26
+ ```
27
+
28
+ ## Example
29
+
30
+ ```
31
+ <match tag>
32
+ @type mysql_fetch_and_emit
33
+
34
+ host 127.0.0.1
35
+ username root
36
+ password password
37
+ database db
38
+ table users
39
+
40
+ record_key id
41
+
42
+ tag new_tag
43
+ </match>
44
+
45
+ <match new_tag>
46
+ @type stdout
47
+ </match>
48
+ ```
49
+
50
+ ## Record Matching
51
+
52
+ ```
53
+ <match tag>
54
+ @type mysql_fetch_and_emit
55
+
56
+ host 127.0.0.1
57
+ username root
58
+ password password
59
+ database db
60
+ table users
61
+
62
+ record_key id
63
+
64
+ tag new_tag
65
+
66
+ <record_matching_key>
67
+ fluentd_record_key email
68
+ mysql_record_key email
69
+ </record_matching_key>
70
+ </match>
71
+
72
+ <match new_tag>
73
+ @type stdout
74
+ </match>
75
+ ```
76
+
77
+ If fluentd record has email and match email value of mysql record,
78
+ plugin merges them.
79
+ You can set priority of merged records by `merge_priority`.
80
+
81
+ If `remove_keys` has some values,
82
+ plugin remove key from fluentd record before merging.
83
+
84
+ ## Plugin helpers
85
+
86
+ * [event_emitter](https://docs.fluentd.org/v1.0/articles/api-plugin-helper-event_emitter)
87
+ * [record_accessor](https://docs.fluentd.org/v1.0/articles/api-plugin-helper-record_accessor)
88
+
89
+ * See also: [Output Plugin Overview](https://docs.fluentd.org/v1.0/articles/output-plugin-overview)
90
+
91
+ ## Configuration
92
+
93
+ ### host (string) (optional)
94
+
95
+ Database host.
96
+
97
+ Default value: `127.0.0.1`.
98
+
99
+ ### port (integer) (optional)
100
+
101
+ Database port.
102
+
103
+ Default value: `3306`.
104
+
105
+ ### database (string) (required)
106
+
107
+ Database name.
108
+
109
+ ### username (string) (required)
110
+
111
+ Database user.
112
+
113
+ ### password (string) (optional)
114
+
115
+ Database password.
116
+
117
+ Default value: ``.
118
+
119
+ ### sslkey (string) (optional)
120
+
121
+ SSL key.
122
+
123
+ ### sslcert (string) (optional)
124
+
125
+ SSL cert.
126
+
127
+ ### sslca (string) (optional)
128
+
129
+ SSL CA.
130
+
131
+ ### sslcapath (string) (optional)
132
+
133
+ SSL CA path.
134
+
135
+ ### sslcipher (string) (optional)
136
+
137
+ SSL cipher.
138
+
139
+ ### sslverify (bool) (optional)
140
+
141
+ SSL Verify Server Certificate.
142
+
143
+ ### cast_booleans (bool) (optional)
144
+
145
+ Tell Mysql2 to cast tinyint(1) fields to boolean values.
146
+
147
+ ### stream (bool) (optional)
148
+
149
+ Fetch rows from the mysql on demand.
150
+
151
+ ### table (string) (required)
152
+
153
+ Database table name.
154
+
155
+ ### tag (string) (required)
156
+
157
+ New tag.
158
+
159
+ ### record_key (string) (required)
160
+
161
+ Use the record value for where condition. (record_accessor format)
162
+
163
+ ### additional_condition (string) (optional)
164
+
165
+ Append this value to where clause.
166
+
167
+ ### where_column (string) (optional)
168
+
169
+ Database column name for where condition.
170
+
171
+ ### column_names (array) (optional)
172
+
173
+ Select column names.
174
+
175
+ Default value: `["*"]`.
176
+
177
+ ### \<record_matching_key\> section (optional) (multiple)
178
+
179
+ #### fluentd_record_key (string) (required)
180
+
181
+ Fluentd record key to identify target to merge with mysql record
182
+
183
+ #### mysql_record_key (string) (required)
184
+
185
+ Mysql record column name to identify target to merge with fluentd record
186
+
187
+ ### merge_priority (enum) (optional)
188
+
189
+ Preserve data priority. If this is set :mysql, prioritize database record data.
190
+
191
+ Available values: fluentd, mysql
192
+
193
+ Default value: `fluentd`.
194
+
195
+ ### remove_keys (array) (optional)
196
+
197
+ A list of keys to delete from fluentd record
198
+
199
+ Default value: `[]`.
200
+
201
+ ### \<buffer\> section (optional) (multiple)
202
+
203
+ #### chunk_limit_records () (optional)
204
+
205
+ Default value: `1000`.
206
+
207
+
208
+ ## Copyright
209
+
210
+ * Copyright(c) 2018- joker1007
211
+ * License
212
+ * Apache License, Version 2.0
@@ -0,0 +1,24 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs.push("lib", "test")
8
+ t.test_files = FileList["test/**/test_*.rb"]
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
13
+ task default: [:test]
14
+
15
+ namespace :circleci do
16
+ namespace :db do
17
+ task :create do
18
+ require "mysql2"
19
+
20
+ client = Mysql2::Client.new(host: "127.0.0.1", username: "root", password: "", database: "sample")
21
+ client.query("CREATE TABLE users (id INT unsigned NOT NULL AUTO_INCREMENT, name VARCHAR(191) NOT NULL, PRIMARY KEY (id))")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "fluent-plugin-mysql-fetch-and-emit"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["joker1007"]
8
+ spec.email = ["kakyoin.hierophant@gmail.com"]
9
+
10
+ spec.summary = %q{Fluentd plugin to fetch record by input data, and to emit the record data.}
11
+ spec.description = %q{Fluentd plugin to fetch record by input data, and to emit the record data.}
12
+ spec.homepage = "https://github.com/joker1007/fluent-plugin-mysql-fetch-and-emit"
13
+ spec.license = "Apache-2.0"
14
+
15
+ test_files, files = `git ls-files -z`.split("\x0").partition do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.files = files
19
+ spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = test_files
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
+ spec.add_development_dependency "test-unit", "~> 3.0"
26
+ spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
27
+ spec.add_runtime_dependency "mysql2"
28
+ end
@@ -0,0 +1,206 @@
1
+ #
2
+ # Copyright 2018- joker1007
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "fluent/plugin/output"
17
+ require "mysql2"
18
+
19
+ module Fluent
20
+ module Plugin
21
+ class MysqlFetchAndEmitOutput < Fluent::Plugin::Output
22
+ Fluent::Plugin.register_output("mysql_fetch_and_emit", self)
23
+
24
+ helpers :event_emitter, :record_accessor
25
+
26
+ config_param :host, :string, default: '127.0.0.1',
27
+ desc: "Database host."
28
+ config_param :port, :integer, default: 3306,
29
+ desc: "Database port."
30
+ config_param :database, :string,
31
+ desc: "Database name."
32
+ config_param :username, :string,
33
+ desc: "Database user."
34
+ config_param :password, :string, default: '', secret: true,
35
+ desc: "Database password."
36
+ config_param :sslkey, :string, default: nil,
37
+ desc: "SSL key."
38
+ config_param :sslcert, :string, default: nil,
39
+ desc: "SSL cert."
40
+ config_param :sslca, :string, default: nil,
41
+ desc: "SSL CA."
42
+ config_param :sslcapath, :string, default: nil,
43
+ desc: "SSL CA path."
44
+ config_param :sslcipher, :string, default: nil,
45
+ desc: "SSL cipher."
46
+ config_param :sslverify, :bool, default: nil,
47
+ desc: "SSL Verify Server Certificate."
48
+ config_param :cast_booleans, :bool, default: false,
49
+ desc: "Tell Mysql2 to cast tinyint(1) fields to boolean values."
50
+ config_param :stream, :bool, default: false,
51
+ desc: "Fetch rows from the mysql on demand."
52
+
53
+ config_param :table, :string,
54
+ desc: "Database table name."
55
+
56
+ config_param :tag, :string,
57
+ desc: "New tag."
58
+
59
+ config_param :record_key, :string,
60
+ desc: "Use the record value for where condition. (record_accessor format)"
61
+ config_param :where_column, :string, default: nil,
62
+ desc: "Database column name for where condition."
63
+ config_param :additional_condition, :string, default: nil,
64
+ desc: "Append this value to where clause."
65
+ config_param :column_names, :array, value_type: :string, default: ["*"],
66
+ desc: "Select column names."
67
+
68
+ config_section :record_matching_key, param_name: :record_matching_keys, required: false, multi: true do
69
+ config_param :fluentd_record_key, :string,
70
+ desc: "Fluentd record key to identify target to merge with mysql record"
71
+ config_param :mysql_record_key, :string,
72
+ desc: "Mysql record column name to identify target to merge with fluentd record"
73
+ end
74
+
75
+ config_param :merge_priority, :enum, list: [:fluentd, :mysql], default: :fluentd,
76
+ desc: "Preserve data priority. If this is set :mysql, prioritize database record data."
77
+ config_param :remove_keys, :array, value_type: :string, default: [],
78
+ desc: "A list of keys to delete from fluentd record"
79
+
80
+ config_section :buffer do
81
+ config_set_default :chunk_limit_records, 1000
82
+ end
83
+
84
+ def configure(conf)
85
+ super
86
+ @accessor_for_record_key = record_accessor_create(@record_key)
87
+ @accessors_for_record_matching = @record_matching_keys.map { |cf| record_accessor_create(cf.fluentd_record_key) }
88
+ @column_names_for_record_matching = @record_matching_keys.map { |cf| cf.mysql_record_key }
89
+ end
90
+
91
+ def format(tag, time, record)
92
+ value = @accessor_for_record_key.call(record)
93
+ unless @accessors_for_record_matching.empty?
94
+ keys_for_origin_record = @accessors_for_record_matching.map { |accessor| accessor.call(record) }
95
+ if keys_for_origin_record.any?(&:nil?)
96
+ @log.warn("Incoming record is omitted, because values for record matching include nil", record: record)
97
+ return nil
98
+ end
99
+ end
100
+
101
+ case value
102
+ when String, Integer, Float
103
+ [tag, time, record].to_msgpack
104
+ else
105
+ @log.warn("Incoming record is omitted, Supported value type of `record_key` is String, Integer, Float", record: record)
106
+ nil
107
+ end
108
+ end
109
+
110
+ def formatted_to_msgpack_binary?
111
+ true
112
+ end
113
+
114
+ def multi_workers_ready?
115
+ true
116
+ end
117
+
118
+ def where_column_name
119
+ @where_column || @record_key
120
+ end
121
+
122
+ def write(chunk)
123
+ database, table = expand_placeholders(chunk.metadata)
124
+ @handler = client(database)
125
+ where_values = []
126
+ origin_records = {}
127
+ chunk.msgpack_each do |tag, time, data|
128
+ value = @accessor_for_record_key.call(data)
129
+ case value
130
+ when String
131
+ where_values << "'" + Mysql2::Client.escape(value) + "'" if value
132
+ when Integer, Float
133
+ where_values << value.to_s if value
134
+ else
135
+ next
136
+ end
137
+
138
+ unless @accessors_for_record_matching.empty?
139
+ keys_for_origin_record = @accessors_for_record_matching.map { |accessor| accessor.call(data) }
140
+ parent = keys_for_origin_record[0..-2].inject(origin_records) do |h, v|
141
+ h[v] ||= {}
142
+ end
143
+ parent[keys_for_origin_record.last] = data
144
+ end
145
+ end
146
+ where_condition = "WHERE #{where_column_name} IN (#{where_values.join(',')})"
147
+
148
+ if @additional_condition
149
+ condition_sql = extract_placeholders(@additional_condition, chunk.metadata)
150
+ where_condition += " AND #{condition_sql}"
151
+ end
152
+
153
+ sql = "SELECT #{@column_names.join(", ")} FROM #{table} #{where_condition}"
154
+ @log.debug(sql)
155
+ results = @handler.query(sql, cast_booleans: @cast_booleans, stream: @stream)
156
+
157
+ time = Fluent::EventTime.now
158
+ results.each do |row|
159
+ unless @column_names_for_record_matching.empty?
160
+ record = @column_names_for_record_matching.inject(origin_records) do |h, k|
161
+ if h
162
+ h[row[k]]
163
+ end
164
+ end
165
+
166
+ if record
167
+ @remove_keys.each do |k|
168
+ record.delete(k)
169
+ end
170
+
171
+ if @merge_priority == :mysql
172
+ row = record.merge!(row)
173
+ else
174
+ row = row.merge!(record)
175
+ end
176
+ end
177
+ end
178
+ @log.debug("emit", tag: @tag, record: row)
179
+ router.emit(@tag, time, row)
180
+ end
181
+ end
182
+
183
+ def client(database)
184
+ Mysql2::Client.new(
185
+ host: @host,
186
+ port: @port,
187
+ username: @username,
188
+ password: @password,
189
+ database: database,
190
+ sslkey: @sslkey,
191
+ sslcert: @sslcert,
192
+ sslca: @sslca,
193
+ sslcapath: @sslcapath,
194
+ sslcipher: @sslcipher,
195
+ sslverify: @sslverify
196
+ )
197
+ end
198
+
199
+ def expand_placeholders(metadata)
200
+ database = extract_placeholders(@database, metadata).gsub('.', '_')
201
+ table = extract_placeholders(@table, metadata).gsub('.', '_')
202
+ return database, table
203
+ end
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
2
+ require "test-unit"
3
+ require "fluent/test"
4
+ require "fluent/test/driver/output"
5
+ require "fluent/test/helpers"
6
+
7
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
8
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
@@ -0,0 +1,226 @@
1
+ require "helper"
2
+ require "fluent/plugin/out_mysql_fetch_and_emit.rb"
3
+
4
+ class MysqlFetchAndEmitOutputTest < Test::Unit::TestCase
5
+ setup do
6
+ Fluent::Test.setup
7
+
8
+ mysql_client.query("INSERT INTO users (id, name) VALUES (1, 'user1'), (2, 'user2'), (3, 'user3')")
9
+ end
10
+
11
+ teardown do
12
+ mysql_client.query("TRUNCATE TABLE users")
13
+ end
14
+
15
+ test "mysql_connect" do
16
+ results = mysql_client.query("SELECT * FROM users ORDER BY id")
17
+ assert do
18
+ results.to_a.map { |row| row["id"] } == [1, 2, 3]
19
+ end
20
+ end
21
+
22
+ CONFIG = <<~CONF
23
+ host 127.0.0.1
24
+ username root
25
+ password ""
26
+ database sample
27
+ table users
28
+
29
+ record_key id
30
+ column_names ["id as foo", "name"]
31
+
32
+ tag new_tag
33
+ CONF
34
+
35
+ test "configure" do
36
+ driver = create_driver
37
+
38
+ assert_equal("id", driver.instance.where_column_name)
39
+ end
40
+
41
+ sub_test_case "write" do
42
+ test "single feed" do
43
+ driver = create_driver
44
+
45
+ driver.run do
46
+ driver.feed("tag", Time.now.to_i, {"id" => "1"})
47
+ end
48
+ event = driver.events[0]
49
+ assert_equal("new_tag", event[0])
50
+ assert_kind_of(Fluent::EventTime, event[1])
51
+ assert_equal({"foo" => 1, "name" => "user1"}, event[2])
52
+ end
53
+
54
+ test "multiple feed" do
55
+ driver = create_driver
56
+
57
+ driver.run do
58
+ driver.feed("tag", Time.now.to_i, {"id" => 1})
59
+ driver.feed("tag", Time.now.to_i, {"id" => 3})
60
+ end
61
+ event1 = driver.events[0]
62
+ event2 = driver.events[1]
63
+
64
+ assert_equal("new_tag", event1[0])
65
+ assert_kind_of(Fluent::EventTime, event1[1])
66
+ assert_equal({"foo" => 1, "name" => "user1"}, event1[2])
67
+
68
+ assert_equal("new_tag", event2[0])
69
+ assert_kind_of(Fluent::EventTime, event2[1])
70
+ assert_equal({"foo" => 3, "name" => "user3"}, event2[2])
71
+ end
72
+
73
+ test "multiple feed with additional_condition" do
74
+ driver = create_driver(CONFIG + "\n" + <<~CONF)
75
+ <buffer tag>
76
+ </buffer>
77
+ additional_condition "name = '${tag}'"
78
+ CONF
79
+
80
+ driver.run do
81
+ driver.feed("user1", Time.now.to_i, {"id" => 1})
82
+ driver.feed("user1", Time.now.to_i, {"id" => 3})
83
+ end
84
+ assert_equal(1, driver.events.size)
85
+ event1 = driver.events[0]
86
+
87
+ assert_equal("new_tag", event1[0])
88
+ assert_kind_of(Fluent::EventTime, event1[1])
89
+ assert_equal({"foo" => 1, "name" => "user1"}, event1[2])
90
+ end
91
+ end
92
+
93
+ sub_test_case "record_matching_key" do
94
+ test "mysql_record.merge(fluentd_record)" do
95
+ mysql_client.query("INSERT INTO users (id, name) VALUES (4, 'user1')")
96
+ driver = create_driver(CONFIG + "\n" + <<~CONF)
97
+ <buffer tag>
98
+ </buffer>
99
+
100
+ <record_matching_key>
101
+ fluentd_record_key id
102
+ mysql_record_key foo
103
+ </record_matching_key>
104
+ CONF
105
+
106
+ driver.run do
107
+ driver.feed("user1", Time.now.to_i, {"id" => 1, "name" => "new_user1", "hoge" => "bar1"})
108
+ driver.feed("user1", Time.now.to_i, {"id" => 3, "hoge" => "bar3"})
109
+ driver.feed("user1", Time.now.to_i, {"id" => 4, "name" => "new_user4", "hoge" => "bar4"})
110
+ end
111
+ assert_equal(3, driver.events.size)
112
+ event1 = driver.events[0]
113
+ event2 = driver.events[1]
114
+ event3 = driver.events[2]
115
+
116
+ assert_equal("new_tag", event1[0])
117
+ assert_kind_of(Fluent::EventTime, event1[1])
118
+ assert_equal({"id" => 1, "foo" => 1, "name" => "new_user1", "hoge" => "bar1"}, event1[2])
119
+ assert_equal({"id" => 3, "foo" => 3, "name" => "user3", "hoge" => "bar3"}, event2[2])
120
+ assert_equal({"id" => 4, "foo" => 4, "name" => "new_user4", "hoge" => "bar4"}, event3[2])
121
+ end
122
+
123
+ test "mysql_record.merge(fluentd_record) with remove_keys" do
124
+ mysql_client.query("INSERT INTO users (id, name) VALUES (4, 'user1')")
125
+ driver = create_driver(CONFIG + "\n" + <<~CONF)
126
+ <buffer tag>
127
+ </buffer>
128
+
129
+ <record_matching_key>
130
+ fluentd_record_key id
131
+ mysql_record_key foo
132
+ </record_matching_key>
133
+
134
+ remove_keys name, hoge
135
+ CONF
136
+
137
+ driver.run do
138
+ driver.feed("user1", Time.now.to_i, {"id" => 1, "name" => "new_user1", "hoge" => "bar1"})
139
+ driver.feed("user1", Time.now.to_i, {"id" => 3, "hoge" => "bar3"})
140
+ driver.feed("user1", Time.now.to_i, {"id" => 4, "name" => "new_user4", "hoge" => "bar4"})
141
+ end
142
+ assert_equal(3, driver.events.size)
143
+ event1 = driver.events[0]
144
+ event2 = driver.events[1]
145
+ event3 = driver.events[2]
146
+
147
+ assert_equal("new_tag", event1[0])
148
+ assert_kind_of(Fluent::EventTime, event1[1])
149
+ assert_equal({"id" => 1, "foo" => 1, "name" => "user1"}, event1[2])
150
+ assert_equal({"id" => 3, "foo" => 3, "name" => "user3"}, event2[2])
151
+ assert_equal({"id" => 4, "foo" => 4, "name" => "user1"}, event3[2])
152
+ end
153
+
154
+ test "fluentd_record.merge(mysql_record)" do
155
+ mysql_client.query("INSERT INTO users (id, name) VALUES (4, 'user1')")
156
+ driver = create_driver(CONFIG + "\n" + <<~CONF)
157
+ <buffer tag>
158
+ </buffer>
159
+ merge_priority mysql
160
+
161
+ <record_matching_key>
162
+ fluentd_record_key id
163
+ mysql_record_key foo
164
+ </record_matching_key>
165
+ CONF
166
+
167
+ driver.run do
168
+ driver.feed("user1", Time.now.to_i, {"id" => 1, "name" => "new_user1", "hoge" => "bar1"})
169
+ driver.feed("user1", Time.now.to_i, {"id" => 3, "hoge" => "bar3"})
170
+ driver.feed("user1", Time.now.to_i, {"id" => 4, "name" => "new_user4", "hoge" => "bar4"})
171
+ end
172
+ assert_equal(3, driver.events.size)
173
+ event1 = driver.events[0]
174
+ event2 = driver.events[1]
175
+ event3 = driver.events[2]
176
+
177
+ assert_equal("new_tag", event1[0])
178
+ assert_kind_of(Fluent::EventTime, event1[1])
179
+ assert_equal({"id" => 1, "foo" => 1, "name" => "user1", "hoge" => "bar1"}, event1[2])
180
+ assert_equal({"id" => 3, "foo" => 3, "name" => "user3", "hoge" => "bar3"}, event2[2])
181
+ assert_equal({"id" => 4, "foo" => 4, "name" => "user1", "hoge" => "bar4"}, event3[2])
182
+ end
183
+
184
+ test "fluentd_record.merge(mysql_record) with remove_keys" do
185
+ mysql_client.query("INSERT INTO users (id, name) VALUES (4, 'user1')")
186
+ driver = create_driver(CONFIG + "\n" + <<~CONF)
187
+ <buffer tag>
188
+ </buffer>
189
+ merge_priority mysql
190
+
191
+ <record_matching_key>
192
+ fluentd_record_key id
193
+ mysql_record_key foo
194
+ </record_matching_key>
195
+
196
+ remove_keys name, hoge
197
+ CONF
198
+
199
+ driver.run do
200
+ driver.feed("user1", Time.now.to_i, {"id" => 1, "name" => "new_user1", "hoge" => "bar1"})
201
+ driver.feed("user1", Time.now.to_i, {"id" => 3, "hoge" => "bar3"})
202
+ driver.feed("user1", Time.now.to_i, {"id" => 4, "name" => "new_user4", "hoge" => "bar4"})
203
+ end
204
+ assert_equal(3, driver.events.size)
205
+ event1 = driver.events[0]
206
+ event2 = driver.events[1]
207
+ event3 = driver.events[2]
208
+
209
+ assert_equal("new_tag", event1[0])
210
+ assert_kind_of(Fluent::EventTime, event1[1])
211
+ assert_equal({"id" => 1, "foo" => 1, "name" => "user1"}, event1[2])
212
+ assert_equal({"id" => 3, "foo" => 3, "name" => "user3"}, event2[2])
213
+ assert_equal({"id" => 4, "foo" => 4, "name" => "user1"}, event3[2])
214
+ end
215
+ end
216
+
217
+ private
218
+
219
+ def create_driver(conf = CONFIG)
220
+ Fluent::Test::Driver::Output.new(Fluent::Plugin::MysqlFetchAndEmitOutput).configure(conf)
221
+ end
222
+
223
+ def mysql_client
224
+ Mysql2::Client.new(host: "127.0.0.1", username: "root", password: "", database: "sample")
225
+ end
226
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysql-fetch-and-emit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fluentd
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.10
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '2'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 0.14.10
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '2'
75
+ - !ruby/object:Gem::Dependency
76
+ name: mysql2
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: Fluentd plugin to fetch record by input data, and to emit the record
90
+ data.
91
+ email:
92
+ - kakyoin.hierophant@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files: []
96
+ files:
97
+ - ".circleci/config.yml"
98
+ - ".gitignore"
99
+ - Gemfile
100
+ - Gemfile.lock
101
+ - LICENSE
102
+ - README.md
103
+ - Rakefile
104
+ - fluent-plugin-mysql-fetch-and-emit.gemspec
105
+ - lib/fluent/plugin/out_mysql_fetch_and_emit.rb
106
+ - test/helper.rb
107
+ - test/plugin/test_out_mysql_fetch_and_emit.rb
108
+ homepage: https://github.com/joker1007/fluent-plugin-mysql-fetch-and-emit
109
+ licenses:
110
+ - Apache-2.0
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.7.4
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Fluentd plugin to fetch record by input data, and to emit the record data.
132
+ test_files:
133
+ - test/helper.rb
134
+ - test/plugin/test_out_mysql_fetch_and_emit.rb