diff --git a/lib/logtail/log_devices/http.rb b/lib/logtail/log_devices/http.rb index 1d35efd..a7baf3f 100755 --- a/lib/logtail/log_devices/http.rb +++ b/lib/logtail/log_devices/http.rb @@ -1,6 +1,7 @@ require "base64" require "msgpack" require "net/https" +require "zlib" require "logtail/config" require "logtail/log_devices/http/flushable_dropping_sized_queue" @@ -168,7 +169,7 @@ def verify_delivery! You can enable internal Logtail debug logging with the following: Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT) -MESSAGE + MESSAGE end end @@ -179,7 +180,7 @@ def verify_delivery! You can enable internal debug logging with the following: Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT) -MESSAGE + MESSAGE end private @@ -205,8 +206,10 @@ def build_request(msgs) req = Net::HTTP::Post.new(path) req['Authorization'] = authorization_payload req['Content-Type'] = CONTENT_TYPE + req['Content-Encoding'] = 'gzip' req['User-Agent'] = USER_AGENT - req.body = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack + uncompressed = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack + req.body = Zlib::Deflate.deflate(uncompressed, Zlib::BEST_SPEED) req end diff --git a/lib/logtail/version.rb b/lib/logtail/version.rb index aeb497a..6a1ffaa 100755 --- a/lib/logtail/version.rb +++ b/lib/logtail/version.rb @@ -1,3 +1,3 @@ module Logtail - VERSION = "0.1.15" + VERSION = "0.1.16" end diff --git a/spec/logtail/log_devices/http_spec.rb b/spec/logtail/log_devices/http_spec.rb index 95ed85e..fad540e 100755 --- a/spec/logtail/log_devices/http_spec.rb +++ b/spec/logtail/log_devices/http_spec.rb @@ -102,7 +102,8 @@ request_queue = http.instance_variable_get(:@request_queue) request_attempt = request_queue.deq expect(request_attempt.request).to be_kind_of(Net::HTTP::Post) - expect(request_attempt.request.body).to start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")) + decompressed_body = Zlib::Inflate.inflate(request_attempt.request.body) + expect(decompressed_body).to start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")) message_queue = http.instance_variable_get(:@msg_queue) expect(message_queue.size).to eq(0) @@ -126,14 +127,16 @@ it "should deliver requests on an interval" do stub = stub_request(:post, "https://in.logs.betterstack.com/"). - with( - :body => start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")), - :headers => { - 'Authorization' => 'Bearer MYKEY', - 'Content-Type' => 'application/msgpack', - 'User-Agent' => "Logtail Ruby/#{Logtail::VERSION} (HTTP)" - } - ). + with do |request| + decompressed_body = Zlib::Inflate.inflate(request.body) + expect(decompressed_body).to start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")) + + expect(request.headers['Authorization']).to eq('Bearer MYKEY') + expect(request.headers['Content-Type']).to eq('application/msgpack') + expect(request.headers['User-Agent']).to eq("Logtail Ruby/#{Logtail::VERSION} (HTTP)") + + true + end. to_return(:status => 200, :body => "", :headers => {}) http = described_class.new("MYKEY", flush_interval: 0.1)