fluent-plugin-http-cwm 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: 39a2ea2e53676d645f793e2f29bf10b9f75760f1ecd389b32ea2e35908e42648
4
+ data.tar.gz: 8c62dbb68b8ecec4bd104ed17112b9be9b0fe91db95ce5ddc0fdfa7e40a1429b
5
+ SHA512:
6
+ metadata.gz: 23c97e8a1b2866ad5a0a69fed90b87683996d3f1bc3a8bb49abbff15f67ee126970bbe914606ef73e7d074e16c6256d8b4f931a393b8f36d179d02cf03c92e98
7
+ data.tar.gz: c36fb79794ed69510cedc83b485f02b41007471d5aa69264196c0cf0ae1446819750f801d1d5471ef893f4445939875095e0df3de5b7f687e809ed18bf2af76e
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
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 2020 Azeem Sajid
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,156 @@
1
+ # fluent-plugin-http-cwm
2
+
3
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache-blue.svg?style=flat-square)](https://github.com/iamAzeem/fluent-plugin-http-cwm/blob/master/LICENSE)
4
+ [![RubyGems Downloads](https://img.shields.io/gem/dt/fluent-plugin-http-cwm?color=blue&style=flat-square&label=Downloads)](https://rubygems.org/gems/fluent-plugin-http-cwm)
5
+
6
+ [Fluentd](https://fluentd.org/) HTTP input plugin for
7
+ [CloudWebManage](https://github.com/CloudWebManage) Logging Component.
8
+
9
+ ## Overview
10
+
11
+ This plugin:
12
+
13
+ 1. receives the incoming JSON logs from MinIO using an HTTP endpoint,
14
+ 2. validates the required JSON fields,
15
+ 3. aggregates the logging metrics,
16
+ 4. flushes the aggregated metrics to the configured Redis instance; and,
17
+ 5. routes logs to the configured log targets e.g. S3, ElasticSearch, etc.
18
+
19
+ ```text
20
+ +------------------+
21
+ | MinIO |
22
+ +------------------+
23
+ |
24
+ | JSON
25
+ | logs
26
+ |
27
+ v
28
+ +------------------+
29
+ | fluentd |
30
+ | |
31
+ | +--------------+ | +-----------------+
32
+ | | http_cwm | | [metrics] | Redis |
33
+ | | (input) |-------------------->| Server |
34
+ | +--------------+ | +-----------------+
35
+ | |
36
+ | +--------------+ | +-----------------+
37
+ | | s3 | | [raw logs] | S3 |
38
+ | | (output) |-------------------->| (log target) |
39
+ | +--------------+ | +-----------------+
40
+ | |
41
+ | +--------------+ | +-----------------+
42
+ | |elasticsearch | | [raw logs] | ElasticSearch |
43
+ | | (output) |-------------------->| (log target) |
44
+ | +--------------+ | +-----------------+
45
+ | |
46
+ +------------------+
47
+ ```
48
+
49
+ The following metrics are aggregated:
50
+
51
+ | metric | description |
52
+ |:--------------------|:----------------------------------------------------|
53
+ | `bytes_in` | size of Request header and its Content-Length |
54
+ | `bytes_out` | size of Response header and its Content-Length |
55
+ | `num_requests_in` | count of APIs [WebUpload, PutObject, DeleteObject] |
56
+ | `num_requests_out` | count of APIs [WebDownload, GetObject] |
57
+ | `num_requests_misc` | count of APIs other than `in` and `out` |
58
+
59
+ ## Installation
60
+
61
+ ### RubyGems
62
+
63
+ ```shell
64
+ gem install fluent-plugin-http-cwm
65
+ ```
66
+
67
+ ### Bundler
68
+
69
+ Add the following line to your Gemfile:
70
+
71
+ ```ruby
72
+ gem "fluent-plugin-http-cwm"
73
+ ```
74
+
75
+ And then execute:
76
+
77
+ ```shell
78
+ bundle
79
+ ```
80
+
81
+ ## Configuration
82
+
83
+ * `host` (string) (optional): The address to bind to.
84
+ * Default value: `localhost`.
85
+ * `port` (integer) (optional): The port to listen to.
86
+ * Default value: `8080`.
87
+ * `tag` (string) (required): The tag for the event.
88
+
89
+ ### `<redis>` section (optional) (single)
90
+
91
+ * `host` (string) (optional): The address of Redis server.
92
+ * Default value: `localhost`.
93
+ * `port` (integer) (optional): The port of Redis server.
94
+ * Default value: `6379`.
95
+ * `grace_period` (time) (optional): The grace period for last update.
96
+ * Default value: `300s`.
97
+ * `flush_interval` (time) (optional): The flush interval to send metrics.
98
+ * Default value: `300s`.
99
+ * `last_update_prefix` (string) (optional): The prefix for last update key.
100
+ * Default value: `deploymentid:last_action`.
101
+ * `metrics_prefix` (string) (optional): The prefix for metrics key.
102
+ * Default value: `deploymentid:minio-metrics`.
103
+
104
+ ### Sample Configuration
105
+
106
+ ```text
107
+ # Endpoint for incoming logs: http://host:port/<tag>
108
+
109
+ # HTTP Input
110
+ <source>
111
+ @type http_cwm
112
+ @id http_cwm_logs
113
+
114
+ host localhost
115
+ port 8080
116
+
117
+ tag logs
118
+
119
+ <redis>
120
+ host localhost
121
+ port 6379
122
+ grace_period 10s
123
+ flush_interval 10s
124
+ last_update_prefix deploymentid:last_action
125
+ metrics_prefix deploymentid:minio-metrics
126
+ </redis>
127
+ </source>
128
+
129
+ # Output e.g. ElasticSearch, S3, etc.
130
+ <match logs>
131
+ @type elasticsearch
132
+ # ...
133
+ </match>
134
+ ```
135
+
136
+ The environment variables may also be used for the configuration.
137
+
138
+ Example:
139
+
140
+ ```text
141
+ <source>
142
+ @type http_cwm
143
+ @id http_cwm_logs
144
+
145
+ host "#{ENV['HTTP_HOST']}"
146
+ port "#{ENV['HTTP_PORT']}"
147
+
148
+ # ...
149
+ </source>
150
+ ```
151
+
152
+ ## Copyright
153
+
154
+ * Copyright &copy; 2020 Azeem Sajid
155
+ * License
156
+ * Apache License, Version 2.0
@@ -0,0 +1,13 @@
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]
@@ -0,0 +1,29 @@
1
+ # frozen-string-literal: true
2
+
3
+ lib = File.expand_path('../lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'fluent-plugin-http-cwm'
8
+ spec.version = '0.1.0'
9
+ spec.authors = ['Azeem Sajid']
10
+ spec.email = ['azeem.sajid@gmail.com']
11
+
12
+ spec.summary = 'fluentd HTTP Input Plugin for CloudWebManage Logging Component'
13
+ spec.description = 'fluentd HTTP Input Plugin for CloudWebManage Logging Component with Log Metrics Support'
14
+ spec.homepage = 'https://github.com/iamAzeem/fluent-plugin-http-cwm'
15
+ spec.license = 'Apache-2.0'
16
+
17
+ test_files, files = `git ls-files -z`.split("\x0").partition { |f| f.match(%r{^(test|spec|features)/}) }
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 'json', '~> 1.8', '>= 1.8.3'
28
+ spec.add_runtime_dependency 'redis', '~> 4.2', '>= 4.2.5'
29
+ end
@@ -0,0 +1,244 @@
1
+ # frozen-string-literal: true
2
+
3
+ #
4
+ # Copyright 2020 Azeem Sajid
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'fluent/plugin/input'
19
+ require 'fluent/config/error'
20
+ require 'fluent/plugin_helper/http_server'
21
+ require 'fluent/plugin_helper/timer'
22
+ require 'webrick/httputils'
23
+ require 'json'
24
+ require 'redis'
25
+
26
+ FMT_DATETIME = '%Y-%m-%dT%H:%M:%S.%8NZ'
27
+
28
+ module Fluent
29
+ module Plugin
30
+ # Custom HTTP Input Plugin class for CWM
31
+ class CwmHttpInput < Fluent::Plugin::Input
32
+ Fluent::Plugin.register_input('http_cwm', self)
33
+
34
+ helpers :http_server, :event_emitter, :timer
35
+
36
+ desc 'The address to bind to.'
37
+ config_param :host, :string, default: 'localhost'
38
+
39
+ desc 'The port to listen to.'
40
+ config_param :port, :integer, default: 8080
41
+
42
+ desc 'The tag for the event.'
43
+ config_param :tag, :string
44
+
45
+ desc 'The Redis configuration section for flushing metrics.'
46
+ config_section :redis, required: false, multi: false, init: true, param_name: :redis_config do
47
+ desc 'The address of Redis server.'
48
+ config_param :host, :string, default: 'localhost'
49
+
50
+ desc 'The port of Redis server.'
51
+ config_param :port, :integer, default: 6379
52
+
53
+ desc 'The grace period for last update.'
54
+ config_param :grace_period, :time, default: '300s'
55
+
56
+ desc 'The flush interval to send metrics.'
57
+ config_param :flush_interval, :time, default: '300s'
58
+
59
+ desc 'The prefix for last update key.'
60
+ config_param :last_update_prefix, :string, default: 'deploymentid:last_action'
61
+
62
+ desc 'The prefix for metrics key.'
63
+ config_param :metrics_prefix, :string, default: 'deploymentid:minio-metrics'
64
+ end
65
+
66
+ def default_api_metrics_hash
67
+ Hash.new do |h, k|
68
+ h[k] = {
69
+ 'bytes_in' => 0, 'bytes_out' => 0,
70
+ 'num_requests_in' => 0, 'num_requests_out' => 0, 'num_requests_misc' => 0
71
+ }
72
+ end
73
+ end
74
+
75
+ def initialize
76
+ super
77
+
78
+ @redis = nil
79
+ @deployment_api_metrics = default_api_metrics_hash
80
+ end
81
+
82
+ def set_up_redis
83
+ log.info("Connecting with Redis [#{@redis_config.host}:#{@redis_config.port}]")
84
+ @redis = Redis.new(host: @redis_config.host, port: @redis_config.port)
85
+ ready = false
86
+ until ready
87
+ sleep(1)
88
+ begin
89
+ @redis.ping
90
+ ready = true
91
+ rescue StandardError => e
92
+ log.error("Unable to connect to Redis server! ERROR: '#{e}'. Retrying...")
93
+ end
94
+ end
95
+ end
96
+
97
+ def configure(conf)
98
+ super
99
+
100
+ set_up_redis
101
+ end
102
+
103
+ def start
104
+ super
105
+
106
+ # set up timer for flush interval
107
+ timer_execute(:metrics_flush_timer, @redis_config.flush_interval) do
108
+ flush_api_metrics
109
+ end
110
+
111
+ log.info("Starting HTTP server [#{@host}:#{@port}]...")
112
+ http_server_create_http_server(:http_server, addr: @host, port: @port, logger: log) do |server|
113
+ server.post("/#{tag}") do |req|
114
+ data = parse_data(req.body)
115
+ if data
116
+ route(data) if update_deployment_metrics(data)
117
+ end
118
+
119
+ # return HTTP 200 OK response to MinIO
120
+ [200, { 'Content-Type' => 'text/plain' }, nil]
121
+ end
122
+ end
123
+ end
124
+
125
+ def parse_data(data)
126
+ JSON.parse(data)
127
+ rescue StandardError => e
128
+ log.debug("ERROR: #{e}")
129
+ nil
130
+ end
131
+
132
+ def route(data)
133
+ time = Fluent::Engine.now
134
+ record = { 'message' => data }
135
+ router.emit(@tag, time, record)
136
+ end
137
+
138
+ def days_to_seconds(days)
139
+ days * 24 * 60 * 60
140
+ end
141
+
142
+ def update_deployment_last_action(deploymentid)
143
+ log.debug('Updating deployment last action')
144
+
145
+ key = "#{@redis_config.last_update_prefix}:#{deploymentid}"
146
+ curdt = DateTime.now
147
+
148
+ begin
149
+ lastval = @redis.get(key)
150
+ lastdt = DateTime.parse(lastval, FMT_DATETIME) if lastval
151
+ if lastdt.nil? || days_to_seconds((curdt - lastdt).to_i) >= @redis_config.grace_period
152
+ log.debug('Setting last action')
153
+ @redis.set(key, curdt.strftime(FMT_DATETIME))
154
+ end
155
+ rescue StandardError => e
156
+ log.error("Unable to update last action! ERROR: '#{e}'.")
157
+ end
158
+ end
159
+
160
+ def validate_and_get_value(data_hash, key)
161
+ value = data_hash[key]
162
+ log.debug("missing '#{key}': #{data_hash.to_json}") unless value
163
+ value
164
+ end
165
+
166
+ def update_deployment_metrics(data)
167
+ log.debug('Updating deployment metrics')
168
+
169
+ deploymentid = validate_and_get_value(data, 'deploymentid')
170
+ return false unless deploymentid
171
+
172
+ update_deployment_last_action(deploymentid)
173
+
174
+ api_data = validate_and_get_value(data, 'api')
175
+ return false unless api_data
176
+
177
+ api_name = validate_and_get_value(api_data, 'name')
178
+ return false unless api_name
179
+
180
+ response_header_data = validate_and_get_value(data, 'responseHeader')
181
+ return false unless response_header_data
182
+
183
+ request_header_data = validate_and_get_value(data, 'requestHeader')
184
+ return false unless request_header_data
185
+
186
+ response_content_length = response_header_data['Content-Length'].to_i
187
+ response_content_length += response_header_data.to_s.length
188
+
189
+ response_is_cached = (response_header_data['X-Cache'] == 'HIT')
190
+
191
+ request_content_length = request_header_data['Content-Length'].to_i
192
+ request_content_length += request_header_data.to_s.length
193
+
194
+ update_deployment_api_metrics(deploymentid, api_name, request_content_length, response_content_length, response_is_cached)
195
+
196
+ true
197
+ end
198
+
199
+ def get_request_type(api_name)
200
+ in_apis = %w[WebUpload PutObject DeleteObject].freeze
201
+ out_apis = %w[WebDownload GetObject].freeze
202
+ return 'in' if in_apis.include?(api_name)
203
+ return 'out' if out_apis.include?(api_name)
204
+
205
+ 'misc'
206
+ end
207
+
208
+ def update_deployment_api_metrics(deploymentid, api_name, request_content_length, response_content_length, response_is_cached)
209
+ log.debug('Updating deployment API metrics')
210
+
211
+ request_type = get_request_type(api_name)
212
+ log.debug("#{deploymentid}.#{api_name}: (type=#{request_type}, req_size=#{request_content_length}, res_size=#{response_content_length}, res_cache=#{response_is_cached})")
213
+
214
+ metrics = @deployment_api_metrics[deploymentid]
215
+ metrics['bytes_in'] += request_content_length
216
+ metrics['bytes_out'] += response_content_length
217
+ metrics["num_requests_#{request_type}"] += 1
218
+ @deployment_api_metrics[deploymentid] = metrics
219
+ end
220
+
221
+ def flush_api_metrics
222
+ return if @deployment_api_metrics.empty?
223
+
224
+ log.debug("Flushing metrics: #{@deployment_api_metrics}")
225
+
226
+ begin
227
+ @redis.pipelined do
228
+ @deployment_api_metrics.each do |deploymentid, metrics|
229
+ metrics.each do |metric, value|
230
+ @redis.incrby("#{@redis_config.metrics_prefix}:#{deploymentid}:#{metric}", value) if value.positive?
231
+ end
232
+ end
233
+ end
234
+
235
+ @deployment_api_metrics = default_api_metrics_hash
236
+
237
+ log.debug('Flushing complete!')
238
+ rescue StandardError => e
239
+ log.error("Unable to flush metrics! ERROR: '#{e}'.")
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,10 @@
1
+ # frozen-string-literal: true
2
+
3
+ $LOAD_PATH.unshift(File.expand_path('..', __dir__))
4
+ require 'test-unit'
5
+ require 'fluent/test'
6
+ require 'fluent/test/driver/input'
7
+ require 'fluent/test/helpers'
8
+
9
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
10
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
@@ -0,0 +1,20 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'helper'
4
+ require 'fluent/plugin/in_http_cwm.rb'
5
+
6
+ class CwmHttpInputTest < Test::Unit::TestCase
7
+ setup do
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ test 'failure' do
12
+ flunk
13
+ end
14
+
15
+ private
16
+
17
+ def create_driver(conf)
18
+ Fluent::Test::Driver::Input.new(Fluent::Plugin::CwmHttpInput).configure(conf)
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-http-cwm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Azeem Sajid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-28 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: json
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.8'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 1.8.3
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.8'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.8.3
95
+ - !ruby/object:Gem::Dependency
96
+ name: redis
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '4.2'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 4.2.5
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '4.2'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 4.2.5
115
+ description: fluentd HTTP Input Plugin for CloudWebManage Logging Component with Log
116
+ Metrics Support
117
+ email:
118
+ - azeem.sajid@gmail.com
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - ".gitignore"
124
+ - Gemfile
125
+ - LICENSE
126
+ - README.md
127
+ - Rakefile
128
+ - fluent-plugin-http-cwm.gemspec
129
+ - lib/fluent/plugin/in_http_cwm.rb
130
+ - test/helper.rb
131
+ - test/plugin/test_in_http_cwm.rb
132
+ homepage: https://github.com/iamAzeem/fluent-plugin-http-cwm
133
+ licenses:
134
+ - Apache-2.0
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.7.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: fluentd HTTP Input Plugin for CloudWebManage Logging Component
156
+ test_files:
157
+ - test/helper.rb
158
+ - test/plugin/test_in_http_cwm.rb