logstash-codec-protobuf 1.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +56 -0
  3. data/CONTRIBUTORS +12 -0
  4. data/DEVELOPER.md +2 -0
  5. data/Gemfile +11 -0
  6. data/LICENSE +202 -0
  7. data/NOTICE.TXT +4 -0
  8. data/README.md +184 -0
  9. data/docs/index.asciidoc +241 -0
  10. data/google-protobuf-lib-update.md +57 -0
  11. data/lib/logstash/codecs/protobuf.rb +804 -0
  12. data/logstash-codec-protobuf.gemspec +33 -0
  13. data/spec/codecs/pb2_spec.rb +236 -0
  14. data/spec/codecs/pb3_decode_spec.rb +665 -0
  15. data/spec/codecs/pb3_encode_spec.rb +243 -0
  16. data/spec/helpers/pb2/ColourTestcase.pb.rb +35 -0
  17. data/spec/helpers/pb2/ColourTestcase.proto +24 -0
  18. data/spec/helpers/pb2/event.pb.rb +19 -0
  19. data/spec/helpers/pb2/event.proto +12 -0
  20. data/spec/helpers/pb2/header/header.pb.rb +16 -0
  21. data/spec/helpers/pb2/header/header.proto +8 -0
  22. data/spec/helpers/pb2/human.pb.rb +26 -0
  23. data/spec/helpers/pb2/unicorn.pb.rb +19 -0
  24. data/spec/helpers/pb2/unicorn_event.pb.rb +24 -0
  25. data/spec/helpers/pb3/FantasyHorse_pb.rb +48 -0
  26. data/spec/helpers/pb3/PhoneDirectory_pb.rb +37 -0
  27. data/spec/helpers/pb3/ProbeResult_pb.rb +26 -0
  28. data/spec/helpers/pb3/ResultListComposerRequest_pb.rb +25 -0
  29. data/spec/helpers/pb3/dnsmessage_pb.rb +82 -0
  30. data/spec/helpers/pb3/events_pb.rb +17 -0
  31. data/spec/helpers/pb3/header/header.proto3 +7 -0
  32. data/spec/helpers/pb3/header/header_pb.rb +12 -0
  33. data/spec/helpers/pb3/integertest_pb.rb +18 -0
  34. data/spec/helpers/pb3/messageA_pb.rb +16 -0
  35. data/spec/helpers/pb3/messageB_pb.rb +15 -0
  36. data/spec/helpers/pb3/rum2_pb.rb +87 -0
  37. data/spec/helpers/pb3/rum3_pb.rb +87 -0
  38. data/spec/helpers/pb3/rum_pb.rb +87 -0
  39. data/spec/helpers/pb3/struct_test_pb.rb +21 -0
  40. data/spec/helpers/pb3/unicorn_pb.rb +31 -0
  41. metadata +175 -0
@@ -0,0 +1,241 @@
1
+ :plugin: protobuf
2
+ :type: codec
3
+
4
+ ///////////////////////////////////////////
5
+ START - GENERATED VARIABLES, DO NOT EDIT!
6
+ ///////////////////////////////////////////
7
+ :version: %VERSION%
8
+ :release_date: %RELEASE_DATE%
9
+ :changelog_url: %CHANGELOG_URL%
10
+ :include_path: ../../../../logstash/docs/include
11
+ ///////////////////////////////////////////
12
+ END - GENERATED VARIABLES, DO NOT EDIT!
13
+ ///////////////////////////////////////////
14
+
15
+ [id="plugins-{type}s-{plugin}"]
16
+
17
+ === Protobuf codec plugin
18
+
19
+ include::{include_path}/plugin_header.asciidoc[]
20
+
21
+ ==== Description
22
+
23
+ This codec converts protobuf encoded messages into logstash events and vice versa. It supports the protobuf versions 2 and 3.
24
+
25
+ The plugin requires the protobuf definitions to be compiled to ruby files. +
26
+ For protobuf 2 use the https://github.com/codekitchen/ruby-protocol-buffers[ruby-protoc compiler]. +
27
+ For protobuf 3 use the https://developers.google.com/protocol-buffers/docs/reference/ruby-generated[official google protobuf compiler].
28
+
29
+ The following shows a usage example (protobuf v2) for decoding events from a kafka stream:
30
+ [source,ruby]
31
+ kafka
32
+ {
33
+ topic_id => "..."
34
+ key_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
35
+ value_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
36
+ codec => protobuf
37
+ {
38
+ class_name => "Animals::Mammals::Unicorn"
39
+ class_file => '/path/to/pb_definitions/some_folder/Unicorn.pb.rb'
40
+ protobuf_root_directory => "/path/to/pb_definitions/"
41
+ }
42
+ }
43
+
44
+ Decoder usage example for protobuf v3:
45
+ [source,ruby]
46
+ kafka
47
+ {
48
+ topic_id => "..."
49
+ key_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
50
+ value_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
51
+ codec => protobuf
52
+ {
53
+ class_name => "Animals.Mammals.Unicorn"
54
+ class_file => '/path/to/pb_definitions/some_folder/Unicorn_pb.rb'
55
+ protobuf_root_directory => "/path/to/pb_definitions/"
56
+ protobuf_version => 3
57
+ }
58
+ }
59
+
60
+
61
+ The codec can be used in input and output plugins. +
62
+ When using the codec in the kafka input plugin please set the deserializer classes as shown above. +
63
+ When using the codec in an output plugin:
64
+
65
+ * make sure to include all the desired fields in the protobuf definition, including timestamp.
66
+ Remove fields that are not part of the protobuf definition from the event by using the mutate filter. Encoding will fail if the event has fields which are not in the protobuf definition.
67
+ * the `@` symbol is currently not supported in field names when loading the protobuf definitions for encoding. Make sure to call the timestamp field `timestamp`
68
+ instead of `@timestamp` in the protobuf file. Logstash event fields will be stripped of the leading `@` before conversion.
69
+ * fields with a nil value will automatically be removed from the event. Empty fields will not be removed.
70
+ * it is recommended to set the config option `pb3_encoder_autoconvert_types` to true. Otherwise any type mismatch between your data and the protobuf definition will cause an event to be lost. The auto typeconversion does not alter your data. It just tries to convert obviously identical data into the expected datatype, such as converting integers to floats where floats are expected, or "true" / "false" strings into booleans where booleans are expected.
71
+ * When writing to Kafka: set the serializer class: `value_serializer => "org.apache.kafka.common.serialization.ByteArraySerializer"`
72
+
73
+ Encoder usage example (protobufg v3):
74
+
75
+ [source,ruby]
76
+ kafka
77
+ {
78
+ codec => protobuf
79
+ {
80
+ class_name => "Animals.Mammals.Unicorn"
81
+ class_file => '/path/to/pb_definitions/some_folder/Unicorn_pb.rb'
82
+ protobuf_root_directory => "/path/to/pb_definitions/"
83
+ protobuf_version => 3
84
+ }
85
+ value_serializer => "org.apache.kafka.common.serialization.ByteArraySerializer"
86
+ }
87
+ }
88
+
89
+
90
+ [id="plugins-{type}s-{plugin}-options"]
91
+ ==== Protobuf Codec Configuration Options
92
+
93
+ [cols="<,<,<",options="header",]
94
+ |=======================================================================
95
+ |Setting |Input type|Required
96
+ | <<plugins-{type}s-{plugin}-class_name>> |<<string,string>>|Yes
97
+ | <<plugins-{type}s-{plugin}-class_file>> |<<string,string>>|No
98
+ | <<plugins-{type}s-{plugin}-protobuf_root_directory>> |<<string,string>>|No
99
+ | <<plugins-{type}s-{plugin}-include_path>> |<<array,array>>|No
100
+ | <<plugins-{type}s-{plugin}-protobuf_version>> |<<number,number>>|Yes
101
+ | <<plugins-{type}s-{plugin}-stop_on_error>> |<<boolean,boolean>>|No
102
+ | <<plugins-{type}s-{plugin}-pb3_encoder_autoconvert_types>> |<<boolean,boolean>>|No
103
+ |=======================================================================
104
+
105
+ &nbsp;
106
+
107
+ [id="plugins-{type}s-{plugin}-class_name"]
108
+ ===== `class_name`
109
+
110
+ * This is a required setting.
111
+ * Value type is <<string,string>>
112
+ * There is no default value for this setting.
113
+
114
+ Fully qualified name of the class to decode.
115
+ Please note that the module delimiter is different depending on the protobuf version. For protobuf v2, use double colons:
116
+ [source,ruby]
117
+ class_name => "Animals::Mammals::Unicorn"
118
+
119
+ For protobuf v3, use single dots:
120
+ [source,ruby]
121
+ class_name => "Animals.Mammals.Unicorn"
122
+
123
+ For protobuf v3, you can copy the class name from the Descriptorpool registrations at the bottom of the generated protobuf ruby file. It contains lines like this:
124
+ [source,ruby]
125
+ Animals.Mammals.Unicorn = Google::Protobuf::DescriptorPool.generated_pool.lookup("Animals.Mammals.Unicorn").msgclass
126
+
127
+ If your class references other definitions: you only have to add the name of the main class here.
128
+
129
+ [id="plugins-{type}s-{plugin}-class_file"]
130
+ ===== `class_file`
131
+
132
+ * Value type is <<string,string>>
133
+ * There is no default value for this setting.
134
+
135
+ Absolute path to the directory that contains all compiled protobuf files. If the protobuf definitions are spread across multiple folders, this needs to point to the folder containing all those folders.
136
+
137
+ [id="plugins-{type}s-{plugin}-protobuf_root_directory"]
138
+ ===== `protobuf_root_directory`
139
+
140
+ * Value type is <<string,string>>
141
+ * There is no default value for this setting.
142
+
143
+ Absolute path to the root directory that contains all referenced/used dependencies of the main class (`class_name`) or any of its dependencies. Must be used in combination with the `class_file` setting, and can not be used in combination with the legacy loading mechanism `include_path`.
144
+
145
+ Example:
146
+
147
+ [source]
148
+ pb3
149
+ ├── header
150
+ │ └── header_pb.rb
151
+ ├── messageA_pb.rb
152
+
153
+ In this case `messageA_pb.rb` has an embedded message from `header/header_pb.rb`.
154
+ If `class_file` is set to `messageA_pb.rb`, and `class_name` to `MessageA`, `protobuf_root_directory` must be set to `/path/to/pb3`, which includes both definitions.
155
+
156
+
157
+ [id="plugins-{type}s-{plugin}-include_path"]
158
+ ===== `include_path`
159
+
160
+ * Value type is <<array,array>>
161
+ * There is no default value for this setting.
162
+
163
+ Legacy protobuf definition loading mechanism for backwards compatibility:
164
+ List of absolute pathes to files with protobuf definitions.
165
+ When using more than one file, make sure to arrange the files in reverse order of dependency so that each class is loaded before it is
166
+ refered to by another.
167
+
168
+ Example: a class _Unicorn_ referencing another protobuf class _Wings_
169
+ [source,ruby]
170
+ module Animal
171
+ module Mammal
172
+ class Unicorn
173
+ set_fully_qualified_name "Animal.Mammal.Unicorn"
174
+ optional ::Bodypart::Wings, :wings, 1
175
+ optional :string, :name, 2
176
+ ...
177
+
178
+ would be configured as
179
+ [source,ruby]
180
+ include_path => ['/path/to/pb_definitions/wings.pb.rb','/path/to/pb_definitions/unicorn.pb.rb']
181
+
182
+ Please note that protobuf v2 files have the ending `.pb.rb` whereas files compiled for protobuf v3 end in `_pb.rb`.
183
+
184
+ Cannot be used together with `protobuf_root_directory` or `class_file`.
185
+
186
+ [id="plugins-{type}s-{plugin}-protobuf_version"]
187
+ ===== `protobuf_version`
188
+
189
+ * Value type is <<number,number>>
190
+ * Default value is 2
191
+
192
+ Protocol buffers version. Valid settings are 2, 3.
193
+
194
+ [id="plugins-{type}s-{plugin}-stop_on_error"]
195
+ ===== `stop_on_error`
196
+
197
+ * Value type is <<boolean,boolean>>
198
+ * Default value is false
199
+
200
+ Stop entire pipeline when encountering a non decodable message.
201
+
202
+ [id="plugins-{type}s-{plugin}-pb3_encoder_autoconvert_types"]
203
+ ===== `pb3_encoder_autoconvert_types`
204
+
205
+ * Value type is <<boolean,boolean>>
206
+ * Default value is true
207
+
208
+ Convert data types to match the protobuf definition (if possible).
209
+ The protobuf encoder library is very strict with regards to data types. Example: an event has an integer field but the protobuf definition expects a float. This would lead to an exception and the event would be lost.
210
+
211
+ This feature tries to convert the datatypes to the expectations of the protobuf definitions, without modifying the data whatsoever. Examples of conversions it might attempt:
212
+
213
+ `"true" :: string => true :: boolean`
214
+
215
+ `17 :: int => 17.0 :: float`
216
+
217
+ `12345 :: number => "12345" :: string`
218
+
219
+ Available only for protobuf version 3.
220
+
221
+ [id="plugins-{type}s-{plugin}-pb3_set_oneof_metainfo"]
222
+ ===== `pb3_set_oneof_metainfo`
223
+
224
+ * Value type is <<boolean,boolean>>
225
+ * Default value is false
226
+
227
+ Add meta information to `[@metadata][pb_oneof]` about which classes were chosen
228
+ for https://developers.google.com/protocol-buffers/docs/proto3#oneof[oneof]
229
+ fields. A new field of name `[@metadata][pb_oneof][FOO]` will be added, where
230
+ `FOO` is the name of the `oneof` field.
231
+
232
+ Example values: for the protobuf definition
233
+ [source,ruby]
234
+ oneof :horse_type do
235
+ optional :unicorn, :message, 2, "UnicornType"
236
+ optional :pegasus, :message, 3, "PegasusType"
237
+ end
238
+
239
+ the field `[@metadata][pb_oneof][horse_type]` will be set to either `pegasus` or `unicorn`.
240
+ Available only for protobuf version 3.
241
+
@@ -0,0 +1,57 @@
1
+ # Custom google protobuf library version
2
+
3
+ At the time of this writing, the codec uses the [protoc library version 3.22.2](https://rubygems.org/gems/google-protobuf/versions/3.22.2-java).
4
+ If you want to use a different protobuf library version then please follow these instructions on how to manually build the google-protobuf.gem.
5
+
6
+ 0. Get the protobuf sources:
7
+ ```
8
+ git clone https://github.com/protocolbuffers/protobuf.git;
9
+ cd protobuf;
10
+ git checkout $VERSION_YOU_WANT_TO_BUILD
11
+ ```
12
+
13
+ 1. Check your compiler version. It needs to be at least the version that you want to build for.
14
+ `protoc --version`
15
+
16
+ If your compiler is too old, build it from the repo:
17
+
18
+ ```
19
+ git submodule update --init --recursive
20
+ ./autogen.sh
21
+ ./configure
22
+ make
23
+ make check
24
+ sudo make install
25
+ ```
26
+
27
+ (instructions taken from their [src/README.md](https://github.com/protocolbuffers/protobuf/blob/master/src/README.md) - check for updates in there!)
28
+
29
+ 2. Build the gem:
30
+ `cd ruby; rake build && gem build *.gemspec`
31
+
32
+ (Instructions taken from https://github.com/protocolbuffers/protobuf/issues/1594#issuecomment-258029377)
33
+
34
+ 3. Change the dependency in the protobuf codec:
35
+
36
+ Step 3.a: clone this repo.
37
+ `git clone git@github.com:logstash-plugins/logstash-codec-protobuf.git`
38
+
39
+ Step 3.b: edit the dependency in [the gemspec](https://github.com/logstash-plugins/logstash-codec-protobuf/blob/master/logstash-codec-protobuf.gemspec#L23) to the version that you built in step 2.
40
+
41
+ Step 3.c: set the [codec version in the gemspec](https://github.com/logstash-plugins/logstash-codec-protobuf/blob/master/logstash-codec-protobuf.gemspec#L4) to a number that is not available on rubygems.
42
+
43
+ Step 3.d: build the codec. `rake build && gem build logstash-codec-protobuf.gemspec`
44
+
45
+
46
+ 4. Install both gems in your Logstash (in this order):
47
+ ```
48
+ logstash-plugin install --no-verify google-protobuf-$PROTOBUF_GEM_VERSION-java.gem
49
+ logstash-plugin install logstash-codec-protobuf-$PROTOBUF_INPUT_PLUGIN_VERSION.gem
50
+ ```
51
+
52
+ # Notes
53
+
54
+ We experimented with both the `>= ` and the `~>` operators in the dependency specification, such as
55
+ ` s.add_runtime_dependency 'google-protobuf', '>= 3.5.0.pre`
56
+ but it would always lead to the ruby version of the protobuf gem to be pulled. Hints / PRs for pinning this to the Java/Jruby version would be appreciated and would render the steps in section 3 and parts of step 4 unnecessary.
57
+