logstash-output-influxdb2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 32cf6c3b489b3187d756694ebc95bd38958bd6015d52099636a30807dcf33057
4
+ data.tar.gz: df4f9f1018b646ed2bcd1f8563fef4486250d6b8b3d9a4eb9d24e9fe5ab25520
5
+ SHA512:
6
+ metadata.gz: 3c4cf17dbbb2e351f76fb90043c2c27130ad4f63e20080f3c5d482a8fd9160a624c6014524aad34a0884991f725b5d02e424bbd60be566df57d90dbe09fd1987
7
+ data.tar.gz: 537a2034ea93b378442d9cac711f5688a40b6052d336631324e151a1a3f6202e709bdd16f735b2e0609cb456731cd3806ee16959c53f6523c7e4d5ed903d1c2f
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Issey Yamakoshi (r16turbo)
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-output-influxdb2
2
+ Example output plugin. This should help bootstrap your effort to write your own output plugin!
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ logstash_path = ENV['LOGSTASH_PATH'] || '/usr/share/logstash'
5
+
6
+ if Dir.exist?(logstash_path)
7
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
8
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
9
+ end
10
+
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.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/main/CONTRIBUTING.md) file.
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "influxdb-client"
4
+
5
+ class LogStash::Outputs::InfluxDB2 < LogStash::Outputs::Base
6
+ config_name "influxdb2"
7
+
8
+ # https://influxdata.github.io/influxdb-client-ruby/InfluxDB2/Client.html
9
+ config :url, :validate => :string, :required => true
10
+ config :token, :validate => :password, :required => true
11
+ config :options, :validate => :hash, :default => {}
12
+
13
+ # https://influxdata.github.io/influxdb-client-ruby/InfluxDB2/WriteOptions.html
14
+ config :write_options, :validate => :hash, :default => {}
15
+
16
+ config :measurement, :validate => :string, :required => true
17
+ config :tags, :validate => :string, :required => true
18
+ config :fields, :validate => :string, :required => true
19
+
20
+ config :include_tags, :validate => :string, :list => true, :default => []
21
+ config :exclude_tags, :validate => :string, :list => true, :default => []
22
+
23
+ config :escape_value, :validate => :boolean, :default => false
24
+
25
+ public
26
+ def register
27
+ @precision = @options.fetch("precision", InfluxDB2::DEFAULT_WRITE_PRECISION)
28
+ @client = InfluxDB2::Client.new(@url, @token.value, **_to_kwargs(@options))
29
+ write_options = InfluxDB2::WriteOptions.new(**_to_kwargs(@write_options))
30
+ @write_api = @client.create_write_api(write_options: write_options)
31
+ end # def register
32
+
33
+ public
34
+ def receive(event)
35
+ begin
36
+ tags = event.get(@tags)
37
+ unless @include_tags.empty?
38
+ tags = tags.select { |k,v| @include_tags.include?(k) }
39
+ end
40
+ unless @exclude_tags.empty?
41
+ tags = tags.select { |k,v| ! @exclude_tags.include?(k) }
42
+ end
43
+ fields = event.get(@fields)
44
+ unless @escape_value
45
+ fields = fields.transform_values { |v| ToStr.new(v) }
46
+ end
47
+ @write_api.write(data: InfluxDB2::Point.new(
48
+ name: event.sprintf(@measurement), tags: tags, fields: fields,
49
+ time: event.timestamp.time, precision: @precision))
50
+ rescue InfluxDB2::InfluxError => ie
51
+ @logger.warn("HTTP communication error while writing to InfluxDB", :exception => ie)
52
+ rescue Exception => e
53
+ @logger.warn("Non recoverable exception while writing to InfluxDB", :exception => e)
54
+ end
55
+ end # def event
56
+
57
+ def close
58
+ @client.close!
59
+ end
60
+
61
+ def _to_kwargs(hash)
62
+ hash.map do |k,v|
63
+ case v
64
+ when "true" then v = true
65
+ when "false" then v = false
66
+ end
67
+ [k.to_sym, v]
68
+ end.to_h
69
+ end
70
+
71
+ # prevents auto escape by client library.
72
+ class ToStr
73
+ def initialize(obj)
74
+ @obj = obj
75
+ end
76
+
77
+ def to_s
78
+ @obj.to_s
79
+ end
80
+
81
+ def to_str
82
+ @obj.to_str
83
+ end
84
+ end
85
+
86
+ end # class LogStash::Outputs::InfluxDB2
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-influxdb2'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Logstash Output Plugin for InfluxDB2'
6
+ s.description = 'This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program'
7
+ s.homepage = 'https://github.com/r16turbo/logstash-output-influxdb2'
8
+ s.authors = ['Issey Yamakoshi']
9
+ s.email = 'r16turbo@gmail.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.1"
22
+ s.add_runtime_dependency "influxdb-client", "~> 2.9"
23
+ s.add_development_dependency "logstash-devutils", "~> 2.5"
24
+ end
@@ -0,0 +1,339 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/influxdb2"
4
+ require "influxdb-client"
5
+
6
+ describe LogStash::Outputs::InfluxDB2 do
7
+
8
+ subject { LogStash::Outputs::InfluxDB2.new(config) }
9
+
10
+ context "validate default config" do
11
+
12
+ let(:config) do
13
+ {
14
+ "url" => "http://localhost:8086",
15
+ "token" => "token123",
16
+ "measurement" => "%{[kubernetes][labels][app]}",
17
+ "tags" => "[prometheus][labels]",
18
+ "fields" => "[prometheus][metrics]"
19
+ }
20
+ end
21
+
22
+ before do
23
+ subject.register
24
+ subject.close
25
+ end
26
+
27
+ it "check configs" do
28
+ options = subject.instance_variable_get(:@client).options
29
+ expect(options[:url]).to eq "http://localhost:8086"
30
+ expect(options[:token]).to eq "token123"
31
+
32
+ expect(options[:bucket]).to be_nil
33
+ expect(options[:org]).to be_nil
34
+ expect(options[:precision]).to be_nil
35
+ expect(options[:open_timeout]).to be_nil
36
+ expect(options[:write_timeout]).to be_nil
37
+ expect(options[:read_timeout]).to be_nil
38
+ expect(options[:max_redirect_count]).to be_nil
39
+ expect(options[:redirect_forward_authorization]).to be_nil
40
+ expect(options[:use_ssl]).to be_nil
41
+ expect(options[:verify_mode]).to be_nil
42
+ expect(options[:debugging]).to be false
43
+ expect(options[:tags]).to be_nil
44
+
45
+ write_options = subject.instance_variable_get(:@write_api)
46
+ .instance_variable_get(:@write_options)
47
+ expect(write_options.write_type).to eq 1
48
+ expect(write_options.batch_size).to eq 1000
49
+ expect(write_options.flush_interval).to eq 1000
50
+ expect(write_options.retry_interval).to eq 5000
51
+ expect(write_options.jitter_interval).to eq 0
52
+ expect(write_options.max_retries).to eq 5
53
+ expect(write_options.max_retry_delay).to eq 125000
54
+ expect(write_options.max_retry_time).to eq 180000
55
+ expect(write_options.exponential_base).to eq 2
56
+ expect(write_options.batch_abort_on_exception).to be false
57
+
58
+ expect(subject.instance_variable_get(:@measurement)).to eq "%{[kubernetes][labels][app]}"
59
+ expect(subject.instance_variable_get(:@tags)).to eq "[prometheus][labels]"
60
+ expect(subject.instance_variable_get(:@fields)).to eq "[prometheus][metrics]"
61
+ expect(subject.instance_variable_get(:@include_tags)).to eq []
62
+ expect(subject.instance_variable_get(:@exclude_tags)).to eq []
63
+ expect(subject.instance_variable_get(:@escape_value)).to be false
64
+ end
65
+
66
+ end
67
+
68
+ context "validate all configs" do
69
+
70
+ let(:config) do
71
+ {
72
+ "url" => "http://localhost:8086",
73
+ "token" => "token123",
74
+ "options" => {
75
+ "bucket" => "test-bucket",
76
+ "org" => "test-org",
77
+ "precision" => "ms",
78
+ "open_timeout" => 1001,
79
+ "write_timeout" => 1002,
80
+ "read_timeout" => 1003,
81
+ "max_redirect_count" => 9999,
82
+ "redirect_forward_authorization" => true,
83
+ "use_ssl" => false,
84
+ "verify_mode" => 1234,
85
+ "debugging" => true,
86
+ "tags" => { "foo" => "bar", "abc" => "xyz" }
87
+ },
88
+ "write_options" => {
89
+ "write_type" => 2,
90
+ "batch_size" => 100,
91
+ "flush_interval" => 1001,
92
+ "retry_interval" => 1002,
93
+ "jitter_interval" => 1003,
94
+ "max_retries" => 2001,
95
+ "max_retry_delay" => 2002,
96
+ "max_retry_time" => 2003,
97
+ "exponential_base" => 9999,
98
+ "batch_abort_on_exception" => true
99
+ },
100
+ "measurement" => "%{[kubernetes][labels][app]}",
101
+ "tags" => "[prometheus][labels]",
102
+ "fields" => "[prometheus][metrics]",
103
+ "include_tags" => [ "foo", "abc" ],
104
+ "exclude_tags" => [ "bar", "xyz" ],
105
+ "escape_value" => true
106
+ }
107
+ end
108
+
109
+ before do
110
+ subject.register
111
+ subject.close
112
+ end
113
+
114
+ it "check configs" do
115
+ options = subject.instance_variable_get(:@client).options
116
+ expect(options[:url]).to eq "http://localhost:8086"
117
+ expect(options[:token]).to eq "token123"
118
+
119
+ expect(options[:bucket]).to eq "test-bucket"
120
+ expect(options[:org]).to eq "test-org"
121
+ expect(options[:precision]).to eq "ms"
122
+ expect(options[:open_timeout]).to eq 1001
123
+ expect(options[:write_timeout]).to eq 1002
124
+ expect(options[:read_timeout]).to eq 1003
125
+ expect(options[:max_redirect_count]).to eq 9999
126
+ expect(options[:redirect_forward_authorization]).to be true
127
+ expect(options[:use_ssl]).to be false
128
+ expect(options[:verify_mode]).to eq 1234
129
+ expect(options[:debugging]).to be true
130
+ expect(options[:tags]).to eq({ "foo" => "bar", "abc" => "xyz" })
131
+
132
+ write_options = subject.instance_variable_get(:@write_api)
133
+ .instance_variable_get(:@write_options)
134
+ expect(write_options.write_type).to eq 2
135
+ expect(write_options.batch_size).to eq 100
136
+ expect(write_options.flush_interval).to eq 1001
137
+ expect(write_options.retry_interval).to eq 1002
138
+ expect(write_options.jitter_interval).to eq 1003
139
+ expect(write_options.max_retries).to eq 2001
140
+ expect(write_options.max_retry_delay).to eq 2002
141
+ expect(write_options.max_retry_time).to eq 2003
142
+ expect(write_options.exponential_base).to eq 9999
143
+ expect(write_options.batch_abort_on_exception).to be true
144
+
145
+ expect(subject.instance_variable_get(:@measurement)).to eq "%{[kubernetes][labels][app]}"
146
+ expect(subject.instance_variable_get(:@tags)).to eq "[prometheus][labels]"
147
+ expect(subject.instance_variable_get(:@fields)).to eq "[prometheus][metrics]"
148
+ expect(subject.instance_variable_get(:@include_tags)).to eq [ "foo", "abc" ]
149
+ expect(subject.instance_variable_get(:@exclude_tags)).to eq [ "bar", "xyz" ]
150
+ expect(subject.instance_variable_get(:@escape_value)).to be true
151
+ end
152
+
153
+ end
154
+
155
+ context "validate payload - escape value" do
156
+
157
+ let(:config) do
158
+ {
159
+ "url" => "http://localhost:8086",
160
+ "token" => "token123",
161
+ "options" => { "bucket" => "test-bucket", "org" => "test-org", "precision" => "ms" },
162
+ "measurement" => "%{[kubernetes][labels][app]}",
163
+ "tags" => "[prometheus][labels]",
164
+ "fields" => "[prometheus][metrics]",
165
+ "escape_value" => true
166
+ }
167
+ end
168
+
169
+ before do
170
+ subject.register
171
+
172
+ write_api = subject.instance_variable_get(:@write_api)
173
+ allow(write_api).to receive(:write_raw).and_return(nil)
174
+
175
+ subject.receive(LogStash::Event.new(
176
+ "@timestamp" => "2020-01-01T00:00:00Z",
177
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
178
+ "prometheus" => {
179
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
180
+ "metrics" => { "count" => 123 }
181
+ }
182
+ ))
183
+
184
+ subject.receive(LogStash::Event.new(
185
+ "@timestamp" => "2020-01-01T00:00:00Z",
186
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
187
+ "prometheus" => {
188
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
189
+ "metrics" => { "count" => 123.0 }
190
+ }
191
+ ))
192
+
193
+ subject.receive(LogStash::Event.new(
194
+ "@timestamp" => "2020-01-01T00:00:00Z",
195
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
196
+ "prometheus" => {
197
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
198
+ "metrics" => { "count" => "abc" }
199
+ }
200
+ ))
201
+
202
+ subject.close
203
+ end
204
+
205
+ it "check payload" do
206
+ write_api = subject.instance_variable_get(:@write_api)
207
+
208
+ expect(write_api).to have_received(:write_raw)
209
+ .with('dummy,abc=xyz,foo=bar count=123i 1577836800000',
210
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
211
+
212
+ expect(write_api).to have_received(:write_raw)
213
+ .with('dummy,abc=xyz,foo=bar count=123.0 1577836800000',
214
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
215
+
216
+ expect(write_api).to have_received(:write_raw)
217
+ .with('dummy,abc=xyz,foo=bar count="abc" 1577836800000',
218
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
219
+ end
220
+
221
+ end
222
+
223
+ context "validate payload - non escape value" do
224
+
225
+ let(:config) do
226
+ {
227
+ "url" => "http://localhost:8086",
228
+ "token" => "token123",
229
+ "options" => { "bucket" => "test-bucket", "org" => "test-org", "precision" => "ms" },
230
+ "measurement" => "%{[kubernetes][labels][app]}",
231
+ "tags" => "[prometheus][labels]",
232
+ "fields" => "[prometheus][metrics]",
233
+ "escape_value" => false
234
+ }
235
+ end
236
+
237
+ before do
238
+ subject.register
239
+
240
+ write_api = subject.instance_variable_get(:@write_api)
241
+ allow(write_api).to receive(:write_raw).and_return(nil)
242
+
243
+ subject.receive(LogStash::Event.new(
244
+ "@timestamp" => "2020-01-01T00:00:00Z",
245
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
246
+ "prometheus" => {
247
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
248
+ "metrics" => { "count" => 123 }
249
+ }
250
+ ))
251
+
252
+ subject.receive(LogStash::Event.new(
253
+ "@timestamp" => "2020-01-01T00:00:00Z",
254
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
255
+ "prometheus" => {
256
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
257
+ "metrics" => { "count" => 123.0 }
258
+ }
259
+ ))
260
+
261
+ subject.receive(LogStash::Event.new(
262
+ "@timestamp" => "2020-01-01T00:00:00Z",
263
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
264
+ "prometheus" => {
265
+ "labels" => { "foo" => "bar", "abc" => "xyz" },
266
+ "metrics" => { "count" => "abc" }
267
+ }
268
+ ))
269
+
270
+ subject.close
271
+ end
272
+
273
+ it "check payload" do
274
+ write_api = subject.instance_variable_get(:@write_api)
275
+
276
+ expect(write_api).to have_received(:write_raw)
277
+ .with('dummy,abc=xyz,foo=bar count=123 1577836800000',
278
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
279
+
280
+ expect(write_api).to have_received(:write_raw)
281
+ .with('dummy,abc=xyz,foo=bar count=123.0 1577836800000',
282
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
283
+
284
+ expect(write_api).to have_received(:write_raw)
285
+ .with('dummy,abc=xyz,foo=bar count=abc 1577836800000',
286
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"}).once
287
+ end
288
+
289
+ end
290
+
291
+ context "validate payload - tag filter" do
292
+
293
+ let(:config) do
294
+ {
295
+ "url" => "http://localhost:8086",
296
+ "token" => "token123",
297
+ "options" => { "bucket" => "test-bucket", "org" => "test-org", "precision" => "ms" },
298
+ "measurement" => "%{[kubernetes][labels][app]}",
299
+ "tags" => "[prometheus][labels]",
300
+ "fields" => "[prometheus][metrics]",
301
+ "include_tags" => [ "foo", "abc", "test" ],
302
+ "exclude_tags" => [ "bar", "xyz", "test" ],
303
+ "escape_value" => true
304
+ }
305
+ end
306
+
307
+ before do
308
+ subject.register
309
+
310
+ write_api = subject.instance_variable_get(:@write_api)
311
+ allow(write_api).to receive(:write_raw).and_return(nil)
312
+
313
+ subject.receive(LogStash::Event.new(
314
+ "@timestamp" => "2020-01-01T00:00:00Z",
315
+ "kubernetes" => { "labels" => { "app" => "dummy" } },
316
+ "prometheus" => {
317
+ "labels" => {
318
+ "foo" => "bar", "abc" => "xyz",
319
+ "bar" => "foo", "xyz" => "abc",
320
+ "test" => "123"
321
+ },
322
+ "metrics" => { "message" => "Hello World!" }
323
+ }
324
+ ))
325
+
326
+ subject.close
327
+ end
328
+
329
+ it "check payload" do
330
+ write_api = subject.instance_variable_get(:@write_api)
331
+
332
+ expect(write_api).to have_received(:write_raw)
333
+ .with('dummy,abc=xyz,foo=bar message="Hello World!" 1577836800000',
334
+ {:bucket=>"test-bucket", :org=>"test-org", :precision=>"ms"})
335
+ end
336
+
337
+ end
338
+
339
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-influxdb2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Issey Yamakoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: influxdb-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logstash-devutils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ description: This gem is a Logstash plugin required to be installed on top of the
56
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
57
+ gem is not a stand-alone program
58
+ email: r16turbo@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - CHANGELOG.md
64
+ - CONTRIBUTORS
65
+ - DEVELOPER.md
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - lib/logstash/outputs/influxdb2.rb
70
+ - logstash-output-influxdb2.gemspec
71
+ - spec/outputs/influxdb2_spec.rb
72
+ homepage: https://github.com/r16turbo/logstash-output-influxdb2
73
+ licenses:
74
+ - Apache-2.0
75
+ metadata:
76
+ logstash_plugin: 'true'
77
+ logstash_group: output
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.3.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Logstash Output Plugin for InfluxDB2
97
+ test_files:
98
+ - spec/outputs/influxdb2_spec.rb