diff --git a/lib/fluent/plugin/out_s3.rb b/lib/fluent/plugin/out_s3.rb index 9149a0b..8742252 100644 --- a/lib/fluent/plugin/out_s3.rb +++ b/lib/fluent/plugin/out_s3.rb @@ -632,20 +632,40 @@ def compress(chunk, tmp) end class ZstdCompressor < Compressor + require 'zstd-ruby' + + DEFAULT_LEVEL = 3 + + def initialize(level = nil) + @level = level || DEFAULT_LEVEL + end + def ext 'zst'.freeze end - + def content_type 'application/x-zst'.freeze end - + def compress(chunk, tmp) - compressed_data = Zstd.compress(chunk.read, level: @level) - tmp.write(compressed_data) - rescue => e - log.warn "zstd compression failed: #{e.message}" - raise e + begin + original_data = chunk.read + log.debug "Original data size: #{original_data.bytesize}" + + compressed = Zstd.compress(original_data, level: @level) + log.debug "Compressed data size: #{compressed.bytesize}" + + tmp.binmode + bytes_written = tmp.write(compressed) + log.debug "Bytes written: #{bytes_written}" + tmp.flush + tmp.close + rescue => e + log.warn "zstd compression failed: #{e.message}" + log.warn e.backtrace.join("\n") + raise e + end end end