kafka
topic_id => "..."
key_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
value_deserializer_class => "org.apache.kafka.common.serialization.ByteArrayDeserializer"
codec => protobuf
class_name => "Animals.Mammals.Unicorn"
class_file => '/path/to/pb_definitions/some_folder/Unicorn_pb.rb'
protobuf_root_directory => "/path/to/pb_definitions/"
protobuf_version => 3
The codec can be used in input and output plugins.
When using the codec in the kafka input plugin please set the deserializer classes as shown above.
When using the codec in an output plugin:
make sure to include all the desired fields in the protobuf definition, including timestamp.
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.
the @
symbol is currently not supported in field names when loading the protobuf definitions for encoding. Make sure to call the timestamp field timestamp
instead of @timestamp
in the protobuf file. Logstash event fields will be stripped of the leading @
before conversion.
fields with a nil value will automatically be removed from the event. Empty fields will not be removed.
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.
When writing to Kafka: set the serializer class: value_serializer => "org.apache.kafka.common.serialization.ByteArraySerializer"
Encoder usage example (protobufg v3):
kafka
codec => protobuf
class_name => "Animals.Mammals.Unicorn"
class_file => '/path/to/pb_definitions/some_folder/Unicorn_pb.rb'
protobuf_root_directory => "/path/to/pb_definitions/"
protobuf_version => 3
value_serializer => "org.apache.kafka.common.serialization.ByteArraySerializer"
Fully qualified name of the class to decode.
Please note that the module delimiter is different depending on the protobuf version. For protobuf v2, use double colons:
class_name => "Animals::Mammals::Unicorn"
For protobuf v3, use single dots:
class_name => "Animals.Mammals.Unicorn"
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:
Animals.Mammals.Unicorn = Google::Protobuf::DescriptorPool.generated_pool.lookup("Animals.Mammals.Unicorn").msgclass
If your class references other definitions: you only have to add the name of the main class here.
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
.
Example:
├── header
│ └── header_pb.rb
├── messageA_pb.rb
In this case messageA_pb.rb
has an embedded message from header/header_pb.rb
.
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.
Legacy protobuf definition loading mechanism for backwards compatibility:
List of absolute pathes to files with protobuf definitions.
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
refered to by another.
Example: a class Unicorn referencing another protobuf class Wings
module Animal
module Mammal
class Unicorn
set_fully_qualified_name "Animal.Mammal.Unicorn"
optional ::Bodypart::Wings, :wings, 1
optional :string, :name, 2
would be configured as
include_path => ['/path/to/pb_definitions/wings.pb.rb','/path/to/pb_definitions/unicorn.pb.rb']
Please note that protobuf v2 files have the ending .pb.rb
whereas files compiled for protobuf v3 end in _pb.rb
.
Cannot be used together with protobuf_root_directory
or class_file
.
Convert data types to match the protobuf definition (if possible).
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.
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:
Add meta information to [@metadata][pb_oneof]
about which classes were chosen
for oneof
fields. A new field of name [@metadata][pb_oneof][FOO]
will be added, where
FOO
is the name of the oneof
field.
Example values: for the protobuf definition
oneof :horse_type do
optional :unicorn, :message, 2, "UnicornType"
optional :pegasus, :message, 3, "PegasusType"
the field [@metadata][pb_oneof][horse_type]
will be set to either pegasus
or unicorn
.
Available only for protobuf version 3.