Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gzip compression #215

Merged
merged 4 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 3.4.2
ODudek marked this conversation as resolved.
Show resolved Hide resolved
- Feature - Add option to support gzip compression.

## 3.4.1

- Enhancement - Add stream name to debug logs to identify : [#214](https://github.com/awslabs/aws-fluent-plugin-kinesis/pull/214)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Also, there are some format related options below:
If your record contains a field whose string should be sent to Amazon Kinesis directly (without formatter), use this parameter to specify the field. In that case, other fields than **data_key** are thrown away and never sent to Amazon Kinesis. Default `nil`, which means whole record will be formatted and sent.

### compression
Specifing compression way for data of each record. Current accepted options are `zlib`. Otherwise, no compression will be preformed.
Specifying compression way for data of each record. Current accepted options are `zlib` and `gzip`. Otherwise, no compression will be preformed.

### log_truncate_max_size
Integer, default 1024. When emitting the log entry, the message will be truncated by this size to avoid infinite loop when the log is also sent to Kinesis. The value 0 means no truncation.
Expand Down
3 changes: 3 additions & 0 deletions lib/fluent/plugin/kinesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'fluent/plugin/output'
require 'fluent/plugin/kinesis_helper/client'
require 'fluent/plugin/kinesis_helper/api'
require 'fluent/plugin/kinesis_helper/compression'
require 'zlib'

module Fluent
Expand Down Expand Up @@ -121,6 +122,8 @@ def compressor_create
case @compression
when "zlib"
->(data) { Zlib::Deflate.deflate(data) }
when "gzip"
->(data) { Gzip.compress(data) }
else
->(data) { data }
end
Expand Down
27 changes: 27 additions & 0 deletions lib/fluent/plugin/kinesis_helper/compression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "stringio"
require "zlib"

class Stream < StringIO
def initialize(*)
super
set_encoding "BINARY"
end

def close
rewind;
end
end

class Gzip
def self.compress(string, level = Zlib::DEFAULT_COMPRESSION, strategy = Zlib::DEFAULT_STRATEGY)
output = Stream.new
gz = Zlib::GzipWriter.new(output, level, strategy)
gz.write(string)
gz.close
output.string
end

def self.decompress(string)
Zlib::GzipReader.wrap(StringIO.new(string), &:read)
end
end
2 changes: 1 addition & 1 deletion lib/fluent_plugin_kinesis/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# language governing permissions and limitations under the License.

module FluentPluginKinesis
VERSION = '3.4.1'
VERSION = '3.4.2'
ODudek marked this conversation as resolved.
Show resolved Hide resolved
end
13 changes: 13 additions & 0 deletions test/plugin/test_kinesis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

require_relative '../helper'
require 'fluent/plugin/kinesis'
require 'fluent/plugin/kinesis_helper/compression'

module Fluent
module Plugin
Expand Down Expand Up @@ -87,6 +88,18 @@ def test_format_compression(data)
assert_equal expected, MessagePack.unpack(result).first
end

data(
'gzip' => ['gzip', Gzip.compress("foo2")],
)
def test_format_compression_gzip(data)
compression, expected = data
d = create_driver(default_config + "data_key a\ncompression #{compression}")
driver_run(d, [{"a"=>"foo2"}])
result = d.formatted.first
unpacked = MessagePack.unpack(result).first
assert_equal Gzip.decompress(expected), Gzip.decompress(unpacked)
end

data(
'not_exceeded' => [{"a"=>"a"*30}, ["a".b*30].to_msgpack],
'exceeded' => [{"a"=>"a"*31}, ''],
Expand Down