22require 'net/https'
33require 'yajl'
44require 'httpclient'
5+ require 'zlib'
6+ require 'stringio'
57
68class SumologicConnection
79
810 attr_reader :http
911
10- def initialize ( endpoint , verify_ssl , connect_timeout , proxy_uri , disable_cookies , sumo_client )
12+ COMPRESS_DEFLATE = 'deflate'
13+ COMPRESS_GZIP = 'gzip'
14+
15+ def initialize ( endpoint , verify_ssl , connect_timeout , proxy_uri , disable_cookies , sumo_client , compress_enabled , compress_encoding )
1116 @endpoint = endpoint
1217 @sumo_client = sumo_client
1318 create_http_client ( verify_ssl , connect_timeout , proxy_uri , disable_cookies )
19+ @compress = compress_enabled
20+ @compress_encoding = ( compress_encoding ||= COMPRESS_GZIP ) . downcase
21+
22+ unless [ COMPRESS_DEFLATE , COMPRESS_GZIP ] . include? @compress_encoding
23+ raise "Invalid compression encoding #{ @compress_encoding } must be gzip or deflate"
24+ end
1425 end
1526
1627 def publish ( raw_data , source_host = nil , source_category = nil , source_name = nil , data_type , metric_data_type , collected_fields )
17- response = http . post ( @endpoint , raw_data , request_headers ( source_host , source_category , source_name , data_type , metric_data_type , collected_fields ) )
28+ response = http . post ( @endpoint , compress ( raw_data ) , request_headers ( source_host , source_category , source_name , data_type , metric_data_type , collected_fields ) )
1829 unless response . ok?
1930 raise RuntimeError , "Failed to send data to HTTP Source. #{ response . code } - #{ response . body } "
2031 end
@@ -27,6 +38,11 @@ def request_headers(source_host, source_category, source_name, data_type, metric
2738 'X-Sumo-Host' => source_host ,
2839 'X-Sumo-Client' => @sumo_client ,
2940 }
41+
42+ if @compress
43+ headers [ 'Content-Encoding' ] = @compress_encoding
44+ end
45+
3046 if data_type == 'metrics'
3147 case metric_data_format
3248 when 'graphite'
@@ -57,6 +73,29 @@ def create_http_client(verify_ssl, connect_timeout, proxy_uri, disable_cookies)
5773 @http . cookie_manager = nil
5874 end
5975 end
76+
77+ def compress ( content )
78+ if @compress
79+ if @compress_encoding == COMPRESS_GZIP
80+ result = gzip ( content )
81+ result . bytes . to_a . pack ( "c*" )
82+ else
83+ Zlib ::Deflate . deflate ( content )
84+ end
85+ else
86+ content
87+ end
88+ end # def compress
89+
90+ def gzip ( content )
91+ stream = StringIO . new ( "w" )
92+ stream . set_encoding ( "ASCII" )
93+ gz = Zlib ::GzipWriter . new ( stream )
94+ gz . mtime = 1 # Ensure that for same content there is same output
95+ gz . write ( content )
96+ gz . close
97+ stream . string . bytes . to_a . pack ( "c*" )
98+ end # def gzip
6099end
61100
62101class Fluent ::Plugin ::Sumologic < Fluent ::Plugin ::Output
@@ -92,6 +131,10 @@ class Fluent::Plugin::Sumologic < Fluent::Plugin::Output
92131 config_param :custom_fields , :string , :default => nil
93132 desc 'Name of sumo client which is send as X-Sumo-Client header'
94133 config_param :sumo_client , :string , :default => 'fluentd-output'
134+ desc 'Compress payload'
135+ config_param :compress , :bool , :default => false
136+ desc 'Encoding method of compresssion (either gzip or deflate)'
137+ config_param :compress_encoding , :string , :default => SumologicConnection ::COMPRESS_GZIP
95138
96139 config_section :buffer do
97140 config_set_default :@type , DEFAULT_BUFFER_TYPE
@@ -150,7 +193,9 @@ def configure(conf)
150193 conf [ 'open_timeout' ] . to_i ,
151194 conf [ 'proxy_uri' ] ,
152195 conf [ 'disable_cookies' ] ,
153- conf [ 'sumo_client' ]
196+ conf [ 'sumo_client' ] ,
197+ conf [ 'compress' ] ,
198+ conf [ 'compress_encoding' ]
154199 )
155200 super
156201 end
0 commit comments