fluent-plugin-protobuf-http 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d711da9e5d6276adfcf1d75064db4961edac6597d27a1d298c019626ecb1c097
4
+ data.tar.gz: 6f4ad4f23bb711c785b2937c78845af3d7ada40dbbf47c5b57e4583816e49952
5
+ SHA512:
6
+ metadata.gz: a0a5cab43923e9181d527f3e902e7dea47d3deb03508d98d02704a1478109e5ae179f164a39bd3423260a48f8a36b1b409d8af54a17882c1687e43471413707f
7
+ data.tar.gz: cb2834e3ba52bc4cc3bcfec8a3cb201ca1954df16f0b5995c84fbef19a50817f601e4da54b34e1bb2273621c9df43f30b929d0849e9e0e28574c2935271d3829
@@ -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,293 @@
1
+ # fluent-plugin-protobuf-http
2
+
3
+ [Fluentd](https://fluentd.org/) HTTP input plugin for Protocol Buffers.
4
+
5
+ ## Features
6
+
7
+ * **ProtoBuf Schemas**: Automatic compilation of `.proto` files located in `proto_dir`
8
+ * **Incoming Message Format**: Support for binary or JSON format (`Content-Type`: `application/octet-stream` or `application/json`)
9
+ * **Outgoing Message Format**: Support for binary or JSON format
10
+ * **Message Types**: Single or Batch
11
+
12
+ ## Installation
13
+
14
+ ### RubyGems
15
+
16
+ ```
17
+ $ gem install fluent-plugin-protobuf-http
18
+ ```
19
+
20
+ ### Bundler
21
+
22
+ Add following line to your Gemfile:
23
+ ```ruby
24
+ gem "fluent-plugin-protobuf-http"
25
+ ```
26
+
27
+ And then execute:
28
+ ```
29
+ $ bundle
30
+ ```
31
+
32
+ ## Configuration
33
+
34
+ * **bind** (string) (optional): The address to listen to.
35
+ * Default value: `0.0.0.0`.
36
+ * **port** (integer) (optional): The port to listen to.
37
+ * Default value: `8080`.
38
+ * **proto_dir** (string) (required): The directory path that contains the .proto files.
39
+ * **in_mode** (enum) (optional): The mode of incoming (supported) events.
40
+ * Available values: binary, json
41
+ * Default value: `binary`.
42
+ * **out_mode** (enum) (optional): The mode of outgoing (emitted) events.
43
+ * Available values: binary, json
44
+ * Default value: `binary`.
45
+ * **tag** (string) (required): The tag for the event.
46
+
47
+ ### Example
48
+
49
+ ```
50
+ # Single Message: http://ip:port/<tag>?msgtype=<msgtype>
51
+ # Batch Message: http://ip:port/<tag>?msgtype=<batch-msgtype>?batch=true
52
+
53
+ <source>
54
+ @type protobuf_http
55
+ @id protobuf_http_input
56
+
57
+ bind 0.0.0.0
58
+ port 8080
59
+ tag debug.test
60
+
61
+ proto_dir ~/fluent/protos
62
+ in_mode binary
63
+ out_mode json
64
+ </source>
65
+ ```
66
+
67
+ ## Schemas (`.proto` files)
68
+
69
+ The logging of events is assumed to be the prime use-case for this plugin.
70
+ So, use self-contained `.proto` file(s) that don't import other custom `.proto` file(s).
71
+ The `package` and `message` names must be unique and are treated as case-sensitive.
72
+
73
+ Consider this [`log.proto`](https://github.com/iamAzeem/protobuf-log-sample/blob/master/log.proto) schema:
74
+ ```
75
+ syntax = "proto3";
76
+
77
+ package service.logging;
78
+
79
+ import "google/protobuf/timestamp.proto";
80
+
81
+ message Log {
82
+ message Context {
83
+ google.protobuf.Timestamp timestamp = 1;
84
+ string host_or_ip = 2;
85
+ string service_name = 3;
86
+ string user = 4;
87
+ }
88
+
89
+ enum Level {
90
+ DEBUG = 0;
91
+ INFO = 1;
92
+ WARN = 2;
93
+ ERROR = 3;
94
+ FATAL = 4;
95
+ }
96
+
97
+ Context context = 1;
98
+ Level level = 2;
99
+ string message = 3;
100
+ }
101
+ ```
102
+
103
+ The fully-qualified message type for `Log` will be `service.logging.Log`.
104
+ This message type is used as the value of `msgtype` query parameter in the URL.
105
+ See URL section below for more on `msgtype`.
106
+
107
+ ### Single Message
108
+
109
+ The above schema will be used as-is for the single message.
110
+
111
+ ### Batch Message
112
+
113
+ For a batch, the schema must be like this:
114
+ ```
115
+ message Batch {
116
+ string type = 1;
117
+ repeated Log batch = 2;
118
+ }
119
+ ```
120
+
121
+ IMPORTANT:
122
+ The `Batch` message type is part of `log.proto`, it's not a separate file!
123
+ You can choose any name for a batch message type.
124
+
125
+ The complete `log.proto` will be:
126
+ ```
127
+ syntax = "proto3";
128
+
129
+ package service.logging;
130
+
131
+ import "google/protobuf/timestamp.proto";
132
+
133
+ message Log {
134
+ message Context {
135
+ google.protobuf.Timestamp timestamp = 1;
136
+ string host_or_ip = 2;
137
+ string service_name = 3;
138
+ string user = 4;
139
+ }
140
+
141
+ enum Level {
142
+ DEBUG = 0;
143
+ INFO = 1;
144
+ WARN = 2;
145
+ ERROR = 3;
146
+ FATAL = 4;
147
+ }
148
+
149
+ Context context = 1;
150
+ Level level = 2;
151
+ string message = 3;
152
+ }
153
+
154
+ message Batch {
155
+ string type = 1;
156
+ repeated Log batch = 2;
157
+ }
158
+ ```
159
+
160
+ For batch processing, the plugin looks for special members `type` and `batch`.
161
+ The `type` will indicate the message type of `batch` i.e. `Log` in this example.
162
+
163
+ The type of `Batch` is `service.logging.Batch` and it will be the value of `msgtype` in the URL query.
164
+ The type of `batch` array is `service.logging.Log` and it will be the value of `type`.
165
+
166
+ The `google.protobuf.Any` type has not been used deliberately here.
167
+ It stores message type information with each message resulting in increase in size.
168
+ With the above approach, the type is stored only once for the whole batch.
169
+
170
+ ### Endpoint (URL)
171
+
172
+ For single message:
173
+ ```
174
+ http://<ip>:<port>/<tag>?msgtype=<fully-qualified-message-type>
175
+ ```
176
+
177
+ For batch message:
178
+ ```
179
+ http://<ip>:<port>/<tag>?msgtype=<fully-qualified-message-type-for-batch>&batch=true
180
+ ```
181
+
182
+ Without `batch=true` query parameter, the batch will be treated as a single message.
183
+
184
+ For example, for a log type `service.logging.Log` and its corresponding batch type `service.logging.Batch`:
185
+
186
+ Single:
187
+ ```
188
+ http://localhost:8080/debug.test?msgtype=service.logging.Log
189
+ ```
190
+
191
+ Batch:
192
+ ```
193
+ http://localhost:8080/debug.test?msgtype=service.logging.Batch&batch=true
194
+ ```
195
+
196
+ **NOTE**: The values of query parameters (`msgtype`, `batch`) are case-sensitive!
197
+
198
+ ## Test Use-Case (`curl`)
199
+
200
+ For a simple test use-case of events and their routing to [stdout](https://docs.fluentd.org/output/stdout) can be configured like this:
201
+
202
+ `fluent.conf`:
203
+ ```
204
+ <source>
205
+ @type protobuf_http
206
+ @id protobuf_http_input
207
+
208
+ bind 0.0.0.0
209
+ port 8080
210
+ tag debug.test
211
+
212
+ proto_dir ~/fluent/protos
213
+ in_mode binary
214
+ out_mode json
215
+ </source>
216
+
217
+ <match debug.test>
218
+ @type stdout
219
+ @id stdout_output
220
+ </match>
221
+ ```
222
+
223
+ The incoming binary messages will be transformed to JSON for further consumption.
224
+
225
+ #### Single Message
226
+
227
+ Test Input Parameters:
228
+ * data: `log.bin`, msgtype: `service.logging.Log`
229
+
230
+ Command:
231
+ ```
232
+ $ curl -X POST -H "Content-Type: application/octet-stream" --data-binary "@/<path>/log.bin" "http://localhost:8080/debug.test?msgtype=service.logging.Log"
233
+ ```
234
+
235
+ `fluentd` Logs (Observe JSON at the end):
236
+ ```
237
+ 2020-06-09 18:53:47 +0500 [info]: #0 [protobuf_http_input] [R] {binary} [127.0.0.1:41222, size: 86 bytes]
238
+ 2020-06-09 18:53:47 +0500 [warn]: #0 [protobuf_http_input] 'batch' not found in 'query_string' [msgtype=service.logging.Log]
239
+ 2020-06-09 18:53:47 +0500 [info]: #0 [protobuf_http_input] [S] {binary} [127.0.0.1:41222, msgtype: service.logging.Log, size: 86 bytes]
240
+ 2020-06-09 18:53:47.025638542 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-01T16:24:19Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./log.rb].\"}"}
241
+ 2020-06-09 18:53:47 +0500 [info]: #0 [protobuf_http_input] [S] {json} [127.0.0.1:41222, msgtype: service.logging.Log, size: 183 bytes]
242
+ ```
243
+
244
+ For Test Single Message Generation: https://github.com/iamAzeem/protobuf-log-sample
245
+
246
+ #### Batch Message
247
+
248
+ Test Input Parameters:
249
+ * data: `logbatch2.bin`, msgtype: `service.logging.Batch`, type: `service.logging.Log` [batch_size: 2 messages]
250
+ * data: `logbatch5.bin`, msgtype: `service.logging.Batch`, type: `service.logging.Log` [batch_size: 5 messages]
251
+
252
+ Command (`logbatch2.bin`):
253
+ ```
254
+ $ curl -X POST -H "Content-Type: application/octet-stream" --data-binary "@/<path>/logbatch2.bin" "http://localhost:8080/debug.test?msgtype=service.logging.Batch&batch=true"
255
+ {"status":"Batch received! [batch_type: service.logging.Log, batch_size: 2 messages]"}
256
+ ```
257
+
258
+ `fluentd` Logs:
259
+ ```
260
+ 2020-06-09 19:04:13 +0500 [info]: #0 [protobuf_http_input] [R] {binary} [127.0.0.1:41416, size: 207 bytes]
261
+ 2020-06-09 19:04:13 +0500 [info]: #0 [protobuf_http_input] [B] {binary} [127.0.0.1:41416, msgtype: service.logging.Batch, size: 207 bytes]
262
+ 2020-06-09 19:04:13 +0500 [info]: #0 [protobuf_http_input] [B] Emitting message stream/batch [batch_size: 2 messages]...
263
+ 2020-06-09 19:04:13.546158307 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
264
+ 2020-06-09 19:04:13.546192659 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
265
+ 2020-06-09 19:04:13 +0500 [info]: #0 [protobuf_http_input] [B] {json} [127.0.0.1:41416, msgtype: service.logging.Batch] Batch received! [batch_type: service.logging.Log, batch_size: 2 messages]
266
+ ```
267
+
268
+ Command (`logbatch5.bin`):
269
+ ```
270
+ $ curl -X POST -H "Content-Type: application/octet-stream" --data-binary "@/<path>/logbatch5.bin" "http://localhost:8080/debug.test?msgtype=service.logging.Batch&batch=true"
271
+ {"status":"Batch received! [batch_type: service.logging.Log, batch_size: 5 messages]"}
272
+ ```
273
+
274
+ `fluentd` Logs:
275
+ ```
276
+ 2020-06-09 19:07:09 +0500 [info]: #0 [protobuf_http_input] [R] {binary} [127.0.0.1:41552, size: 486 bytes]
277
+ 2020-06-09 19:07:09 +0500 [info]: #0 [protobuf_http_input] [B] {binary} [127.0.0.1:41552, msgtype: service.logging.Batch, size: 486 bytes]
278
+ 2020-06-09 19:07:09 +0500 [info]: #0 [protobuf_http_input] [B] Emitting message stream/batch [batch_size: 5 messages]...
279
+ 2020-06-09 19:07:09.738057617 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
280
+ 2020-06-09 19:07:09.738131040 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
281
+ 2020-06-09 19:07:09.738144897 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
282
+ 2020-06-09 19:07:09.738155033 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
283
+ 2020-06-09 19:07:09.738165527 +0500 debug.test: {"message":"{\"context\":{\"timestamp\":\"2020-06-08T17:27:05Z\",\"hostOrIp\":\"192.168.xxx.xxx\",\"serviceName\":\"test\",\"user\":\"test\"},\"level\":\"INFO\",\"message\":\"This is a test log generated by [./logbatch.rb].\"}"}
284
+ 2020-06-09 19:07:09 +0500 [info]: #0 [protobuf_http_input] [B] {json} [127.0.0.1:41552, msgtype: service.logging.Batch] Batch received! [batch_type: service.logging.Log, batch_size: 5 messages]
285
+ ```
286
+
287
+ For Test Batch Message Generation: https://gist.github.com/iamAzeem/a8a24092132e1741a76956192f2104cc
288
+
289
+ ## Copyright
290
+
291
+ * Copyright&copy; 2020 [Azeem Sajid](https://www.linkedin.com/in/az33msajid/)
292
+ * License
293
+ * 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,28 @@
1
+ lib = File.expand_path('../lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'fluent-plugin-protobuf-http'
6
+ spec.version = '0.1.0'
7
+ spec.authors = ['Azeem Sajid']
8
+ spec.email = ['azeem.sajid@gmail.com']
9
+
10
+ spec.summary = 'fluentd HTTP Input Plugin for Protocol Buffers'
11
+ spec.description = 'fluentd HTTP Input Plugin for Protocol Buffers with Single and Batch Messages Support]'
12
+ spec.homepage = 'https://github.com/iamAzeem/fluent-plugin-protobuf-http'
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 'google-protobuf', '~> 3.12', '>= 3.12.2'
28
+ end
@@ -0,0 +1,299 @@
1
+ #
2
+ # Copyright 2020 Azeem Sajid
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/input'
17
+ require 'fluent/config/error'
18
+ require 'fluent/plugin_helper/http_server'
19
+ require 'webrick/httputils'
20
+ require 'json'
21
+
22
+ module Fluent
23
+ module Plugin
24
+ class ProtobufHttpInput < Fluent::Plugin::Input
25
+ Fluent::Plugin.register_input('protobuf_http', self)
26
+
27
+ helpers :http_server, :event_emitter
28
+
29
+ desc 'The address to listen to.'
30
+ config_param :bind, :string, default: '0.0.0.0'
31
+ desc 'The port to listen to.'
32
+ config_param :port, :integer, default: 8080
33
+
34
+ desc 'The directory path that contains the .proto files.'
35
+ config_param :proto_dir, :string
36
+
37
+ desc 'The mode of incoming (supported) events.'
38
+ config_param :in_mode, :enum, list: %i[binary json], default: :binary
39
+ desc 'The mode of outgoing (emitted) events.'
40
+ config_param :out_mode, :enum, list: %i[binary json], default: :binary
41
+
42
+ desc 'The tag for the event.'
43
+ config_param :tag, :string
44
+
45
+ def initialize
46
+ super
47
+
48
+ @protos = [] # list of *.proto files
49
+ @compiled_protos = [] # list of compiled protos i.e. *_pb.rb files
50
+ @msgclass_lookup = {} # Lookup Hash: { msgtype => msgclass }
51
+ end
52
+
53
+ def compile_protos
54
+ log.debug("Checking proto_dir [#{@proto_dir}]...")
55
+
56
+ path = File.expand_path(@proto_dir).freeze
57
+ raise Fluent::ConfigError, "protos_dir does not exist! [#{path}]" unless Dir.exist?(path)
58
+
59
+ @protos = Dir["#{path}/*.proto"].freeze
60
+ raise Fluent::ConfigError, "Empty proto_dir! [#{path}]" unless @protos.any?
61
+
62
+ log.info("Compiling .proto files [#{@protos.length}]...")
63
+
64
+ `protoc --ruby_out=#{path} --proto_path=#{path} #{path}/*.proto`
65
+ raise Fluent::ConfigError, 'Could not compile! See error(s) above.' unless $?.success?
66
+
67
+ log.info("Compiled successfully:\n- #{@protos.join("\n- ")}")
68
+
69
+ @protos.each do |proto|
70
+ @compiled_protos.push(get_compiled_proto(proto))
71
+ end
72
+
73
+ log.info("Compiled .proto files:\n- #{@compiled_protos.join("\n- ")}")
74
+ end
75
+
76
+ def get_compiled_proto(proto)
77
+ proto_suffix = '.proto'.freeze
78
+ compiled_proto_suffix = '_pb.rb'.freeze
79
+
80
+ compiled_proto = proto.chomp(proto_suffix) + compiled_proto_suffix
81
+ raise Fluent::ConfigError, "Compiled proto not found! [#{compiled_proto}]" unless File.file?(compiled_proto)
82
+
83
+ compiled_proto
84
+ end
85
+
86
+ def populate_msgclass_lookup
87
+ @compiled_protos.each do |compiled_proto|
88
+ msg_types = get_msg_types(compiled_proto)
89
+ next unless msg_types.any?
90
+
91
+ begin
92
+ require compiled_proto
93
+ rescue LoadError => e
94
+ raise Fluent::ConfigError, "Possible 'import' issue! Use a single self-contianed .proto file! #{e}"
95
+ end
96
+
97
+ msg_types.each do |msg_type|
98
+ @msgclass_lookup[msg_type] = get_msg_class(msg_type)
99
+ end
100
+ end
101
+
102
+ raise Fluent::ConfigError, "No message types found! Check proto_dir [#{@proto_dir}]!" if @msgclass_lookup.empty?
103
+
104
+ log.info("Registered messages [#{@msgclass_lookup.keys.length}]:\n- #{@msgclass_lookup.keys.join("\n- ")}")
105
+ end
106
+
107
+ def get_msg_types(compiled_proto)
108
+ log.debug("Extracting message types [#{compiled_proto}]...")
109
+ msg_types = []
110
+ File.foreach(compiled_proto) do |line|
111
+ if line.lstrip.start_with?('add_message')
112
+ msg_type = line[/"([^"]*)"/, 1].freeze # regex: <add_message> 'msg_type' <do>
113
+ msg_types.push(msg_type) unless msg_type.nil?
114
+ end
115
+ end
116
+
117
+ if msg_types.any?
118
+ log.info("Total [#{msg_types.length}] message types in [#{compiled_proto}]:\n- #{msg_types.join("\n- ")}")
119
+ else
120
+ log.warn("No message types found! [#{compiled_proto}]")
121
+ end
122
+
123
+ msg_types
124
+ end
125
+
126
+ def get_msg_class(msg_type)
127
+ msg = Google::Protobuf::DescriptorPool.generated_pool.lookup(msg_type)
128
+ raise Fluent::ConfigError, "Message type ['#{msg_type}'] not registered!'" if msg.nil?
129
+
130
+ msg.msgclass
131
+ end
132
+
133
+ def start
134
+ super
135
+
136
+ compile_protos
137
+ populate_msgclass_lookup
138
+
139
+ log.info("Starting protobuf server [#{@bind}:#{@port}]...")
140
+
141
+ http_server_create_http_server(:protobuf_server, addr: @bind, port: @port, logger: log) do |server|
142
+ server.post("/#{tag}") do |req|
143
+ peeraddr = "#{req.peeraddr[2]}:#{req.peeraddr[1]}".freeze # ip:port
144
+ serialized_msg = req.body.freeze
145
+
146
+ log.info("[R] {#{@in_mode}} [#{peeraddr}, size: #{serialized_msg.length} bytes]")
147
+ log.debug("Dumping serialized message [#{serialized_msg.length} bytes]:\n#{serialized_msg}")
148
+
149
+ content_type = req.header['content-type'][0]
150
+
151
+ unless valid_content_type?(content_type)
152
+ status = "Invalid 'Content-Type' header! [#{content_type}]".freeze
153
+ log.warn("[X] Message rejected! [#{peeraddr}] #{status}")
154
+ next [400, { 'Content-Type' => 'application/json', 'Connection' => 'close' }, { 'status' => status }.to_json]
155
+ end
156
+
157
+ log.debug("[>] Content-Type: #{content_type}")
158
+
159
+ msgtype, batch = get_query_params(req.query_string)
160
+ unless @msgclass_lookup.key?(msgtype)
161
+ status = "Invalid 'msgtype' in 'query_string'! [#{msgtype}]".freeze
162
+ log.warn("[X] Message rejected! [#{peeraddr}] #{status}")
163
+ next [400, { 'Content-Type' => 'application/json', 'Connection' => 'close' }, { 'status' => status }.to_json]
164
+ end
165
+
166
+ log.debug("[>] Query parameters: [msgtype: #{content_type}, batch: #{batch}]")
167
+
168
+ deserialized_msg = deserialize_msg(msgtype, serialized_msg)
169
+
170
+ if deserialized_msg.nil?
171
+ status = "Incompatible message! [msgtype: #{msgtype}, size: #{serialized_msg.length} bytes]".freeze
172
+ log.warn("[X] Message rejected! [#{peeraddr}] #{status}")
173
+ next [400, { 'Content-Type' => 'application/json', 'Connection' => 'close' }, { 'status' => status }.to_json]
174
+ end
175
+
176
+ is_batch = !batch.nil? && batch == 'true'
177
+ log.debug("[>] Message validated! [msgtype: #{content_type}, is_batch: #{is_batch}]")
178
+
179
+ # Log single message
180
+
181
+ unless is_batch
182
+ log.info("[S] {#{@in_mode}} [#{peeraddr}, msgtype: #{msgtype}, size: #{serialized_msg.length} bytes]")
183
+
184
+ time = Fluent::Engine.now
185
+ event_msg = serialize_msg(msgtype, deserialized_msg)
186
+ record = { 'message' => event_msg }
187
+ router.emit(@tag, time, record)
188
+
189
+ log.info("[S] {#{@out_mode}} [#{peeraddr}, msgtype: #{msgtype}, size: #{event_msg.length} bytes]")
190
+ next [200, { 'Content-Type' => 'text/plain' }, nil]
191
+ end
192
+
193
+ # Log batch messages
194
+
195
+ log.info("[B] {#{@in_mode}} [#{peeraddr}, msgtype: #{msgtype}, size: #{serialized_msg.length} bytes]")
196
+
197
+ if deserialized_msg.type.nil? || deserialized_msg.batch.nil? || deserialized_msg.batch.empty?
198
+ status = "Invalid 'batch' message! [msgtype: #{msgtype}, size: #{serialized_msg.length} bytes]".freeze
199
+ log.warn("[X] Message rejected! [#{peeraddr}] #{status}")
200
+ next [400, { 'Content-Type' => 'application/json', 'Connection' => 'close' }, { 'status' => status }.to_json]
201
+ end
202
+
203
+ batch_type = deserialized_msg.type
204
+ batch_msgs = deserialized_msg.batch
205
+ batch_size = batch_msgs.length
206
+
207
+ log.info("[B] Emitting message stream/batch [batch_size: #{batch_size} messages]...")
208
+
209
+ stream = MultiEventStream.new
210
+ batch_msgs.each do |batch_msg|
211
+ time = Fluent::Engine.now
212
+ record = { 'message' => serialize_msg(batch_type, batch_msg) }
213
+ stream.add(time, record)
214
+ end
215
+
216
+ router.emit_stream(@tag, stream)
217
+
218
+ status = "Batch received! [batch_type: #{batch_type}, batch_size: #{batch_size} messages]".freeze
219
+ log.info("[B] {#{@out_mode}} [#{peeraddr}, msgtype: #{msgtype}] #{status}")
220
+ [200, { 'Content-Type' => 'application/json', 'Connection' => 'close' }, { 'status' => status }.to_json]
221
+ end
222
+ end
223
+ end
224
+
225
+ def valid_content_type?(content_type)
226
+ hdr_binary = 'application/octet-stream'.freeze
227
+ hdr_json = 'application/json'.freeze
228
+
229
+ case @in_mode
230
+ when :binary
231
+ content_type == hdr_binary
232
+ when :json
233
+ content_type == hdr_json
234
+ when :binary_and_json
235
+ content_type == hdr_binary || content_type == hdr_json
236
+ end
237
+ end
238
+
239
+ def get_query_params(query_string)
240
+ if query_string.nil?
241
+ log.warn("Empty query string! 'msgtype' is required!")
242
+ return nil
243
+ end
244
+
245
+ query = WEBrick::HTTPUtils.parse_query(query_string)
246
+ msgtype = query['msgtype']
247
+ log.warn("'msgtype' not found in 'query_string' [#{query_string}]") if msgtype.nil?
248
+
249
+ batch = query['batch']
250
+ log.warn("'batch' not found in 'query_string' [#{query_string}]") if batch.nil?
251
+
252
+ [msgtype, batch]
253
+ end
254
+
255
+ def deserialize_msg(msgtype, serialized_msg)
256
+ msgclass = @msgclass_lookup[msgtype]
257
+ log.debug("Deserializing {#{@in_mode}} message of type [#{msgclass}]...")
258
+ begin
259
+ case @in_mode
260
+ when :binary
261
+ msgclass.decode(serialized_msg)
262
+ when :json
263
+ msgclass.decode_json(serialized_msg)
264
+ end
265
+ rescue Google::Protobuf::ParseError => e
266
+ log.error("Incompatible message! [msgtype: #{msgtype}, size: #{serialized_msg.length} bytes] #{e}")
267
+ nil
268
+ rescue => e
269
+ log.error("Deserializaton failed! Error: #{e}")
270
+ nil
271
+ end
272
+ end
273
+
274
+ def serialize_msg(msgtype, deserialized_msg)
275
+ msgclass = @msgclass_lookup[msgtype]
276
+ log.debug("Serializing [#{@in_mode} > #{@out_mode}]...")
277
+ begin
278
+ case @out_mode
279
+ when :binary
280
+ msgclass.encode(deserialized_msg)
281
+ when :json
282
+ msgclass.encode_json(deserialized_msg)
283
+ end
284
+ rescue => e
285
+ log.error("Serialization failed! [msgtype: #{msgtype}, msg: #{deserialized_msg}] Error: #{e}")
286
+ nil
287
+ end
288
+ end
289
+
290
+ def shutdown
291
+ @compiled_protos.each do |compiled_proto|
292
+ File.delete(compiled_proto)
293
+ end
294
+
295
+ super
296
+ end
297
+ end
298
+ end
299
+ 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/input"
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,18 @@
1
+ require "helper"
2
+ require "fluent/plugin/in_protobuf_http.rb"
3
+
4
+ class ProtobufHttpInputTest < Test::Unit::TestCase
5
+ setup do
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ test "failure" do
10
+ flunk
11
+ end
12
+
13
+ private
14
+
15
+ def create_driver(conf)
16
+ Fluent::Test::Driver::Input.new(Fluent::Plugin::ProtobufHttpInput).configure(conf)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-protobuf-http
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-06-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: google-protobuf
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.12'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.12.2
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '3.12'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 3.12.2
95
+ description: fluentd HTTP Input Plugin for Protocol Buffers with Single and Batch
96
+ Messages Support]
97
+ email:
98
+ - azeem.sajid@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - fluent-plugin-protobuf-http.gemspec
109
+ - lib/fluent/plugin/in_protobuf_http.rb
110
+ - test/helper.rb
111
+ - test/plugin/test_in_protobuf_http.rb
112
+ homepage: https://github.com/iamAzeem/fluent-plugin-protobuf-http
113
+ licenses:
114
+ - Apache-2.0
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.7.6
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: fluentd HTTP Input Plugin for Protocol Buffers
136
+ test_files:
137
+ - test/helper.rb
138
+ - test/plugin/test_in_protobuf_http.rb