Skip to content

Commit 8bed049

Browse files
authored
Merge pull request #34 from logtail/ah/compress-requests
Compress requests in HTTP device
2 parents 7e7148b + 1b56ca2 commit 8bed049

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

lib/logtail/log_devices/http.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "base64"
22
require "msgpack"
33
require "net/https"
4+
require "zlib"
45

56
require "logtail/config"
67
require "logtail/log_devices/http/flushable_dropping_sized_queue"
@@ -168,7 +169,7 @@ def verify_delivery!
168169
You can enable internal Logtail debug logging with the following:
169170
170171
Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT)
171-
MESSAGE
172+
MESSAGE
172173
end
173174
end
174175

@@ -179,7 +180,7 @@ def verify_delivery!
179180
You can enable internal debug logging with the following:
180181
181182
Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT)
182-
MESSAGE
183+
MESSAGE
183184
end
184185

185186
private
@@ -205,8 +206,10 @@ def build_request(msgs)
205206
req = Net::HTTP::Post.new(path)
206207
req['Authorization'] = authorization_payload
207208
req['Content-Type'] = CONTENT_TYPE
209+
req['Content-Encoding'] = 'gzip'
208210
req['User-Agent'] = USER_AGENT
209-
req.body = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack
211+
uncompressed = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack
212+
req.body = Zlib::Deflate.deflate(uncompressed, Zlib::BEST_SPEED)
210213
req
211214
end
212215

lib/logtail/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Logtail
2-
VERSION = "0.1.15"
2+
VERSION = "0.1.16"
33
end

spec/logtail/log_devices/http_spec.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@
102102
request_queue = http.instance_variable_get(:@request_queue)
103103
request_attempt = request_queue.deq
104104
expect(request_attempt.request).to be_kind_of(Net::HTTP::Post)
105-
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"))
105+
decompressed_body = Zlib::Inflate.inflate(request_attempt.request.body)
106+
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"))
106107

107108
message_queue = http.instance_variable_get(:@msg_queue)
108109
expect(message_queue.size).to eq(0)
@@ -126,14 +127,16 @@
126127

127128
it "should deliver requests on an interval" do
128129
stub = stub_request(:post, "https://in.logs.betterstack.com/").
129-
with(
130-
:body => start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")),
131-
:headers => {
132-
'Authorization' => 'Bearer MYKEY',
133-
'Content-Type' => 'application/msgpack',
134-
'User-Agent' => "Logtail Ruby/#{Logtail::VERSION} (HTTP)"
135-
}
136-
).
130+
with do |request|
131+
decompressed_body = Zlib::Inflate.inflate(request.body)
132+
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"))
133+
134+
expect(request.headers['Authorization']).to eq('Bearer MYKEY')
135+
expect(request.headers['Content-Type']).to eq('application/msgpack')
136+
expect(request.headers['User-Agent']).to eq("Logtail Ruby/#{Logtail::VERSION} (HTTP)")
137+
138+
true
139+
end.
137140
to_return(:status => 200, :body => "", :headers => {})
138141

139142
http = described_class.new("MYKEY", flush_interval: 0.1)

0 commit comments

Comments
 (0)