Skip to content

Commit 978f6ea

Browse files
authoredJul 25, 2024··
feat: Add option to enable compression of event payloads (#291)
1 parent 0295938 commit 978f6ea

File tree

7 files changed

+92
-26
lines changed

7 files changed

+92
-26
lines changed
 

‎contract-tests/client_entity.rb

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def initialize(log, config)
3636
opts[:private_attributes] = events[:globalPrivateAttributes]
3737
opts[:flush_interval] = (events[:flushIntervalMs] / 1_000) unless events[:flushIntervalMs].nil?
3838
opts[:omit_anonymous_contexts] = !!events[:omitAnonymousContexts]
39+
opts[:compress_events] = !!events[:enableGzip]
3940
else
4041
opts[:send_events] = false
4142
end

‎contract-tests/service.rb

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
'secure-mode-hash',
3535
'tags',
3636
'migrations',
37+
'event-gzip',
38+
'optional-event-gzip',
3739
'event-sampling',
3840
'context-comparison',
3941
'polling-gzip',

‎launchdarkly-server-sdk.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939
spec.add_runtime_dependency "concurrent-ruby", "~> 1.1"
4040
spec.add_runtime_dependency "ld-eventsource", "2.2.2"
4141
spec.add_runtime_dependency "observer", "~> 0.1.2"
42+
spec.add_runtime_dependency "zlib", "~> 3.1" unless RUBY_PLATFORM == "java"
4243
# Please keep ld-eventsource dependency as an exact version so that bugfixes to
4344
# that LD library are always associated with a new SDK version.
4445

‎lib/ldclient-rb/config.rb

+20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def initialize(opts = {})
6565
@all_attributes_private = opts[:all_attributes_private] || false
6666
@private_attributes = opts[:private_attributes] || []
6767
@send_events = opts.has_key?(:send_events) ? opts[:send_events] : Config.default_send_events
68+
@compress_events = opts.has_key?(:compress_events) ? opts[:compress_events] : Config.default_compress_events
6869
@context_keys_capacity = opts[:context_keys_capacity] || Config.default_context_keys_capacity
6970
@context_keys_flush_interval = opts[:context_keys_flush_interval] || Config.default_context_keys_flush_interval
7071
@data_source = opts[:data_source]
@@ -254,6 +255,17 @@ def offline?
254255
#
255256
attr_reader :send_events
256257

258+
#
259+
# Should the event payload sent to LaunchDarkly use gzip compression. By default this is false to prevent backward
260+
# breaking compatibility issues with older versions of the relay proxy.
261+
#
262+
# Customers not using the relay proxy are strongly encouraged to enable this feature to reduce egress bandwidth
263+
# cost.
264+
#
265+
# @return [Boolean]
266+
#
267+
attr_reader :compress_events
268+
257269
#
258270
# The number of context keys that the event processor can remember at any one time. This reduces the
259271
# amount of duplicate context details sent in analytics events.
@@ -539,6 +551,14 @@ def self.default_send_events
539551
true
540552
end
541553

554+
#
555+
# The default value for {#compress_events}.
556+
# @return [Boolean] false
557+
#
558+
def self.default_compress_events
559+
false
560+
end
561+
542562
#
543563
# The default value for {#context_keys_capacity}.
544564
# @return [Integer] 1000

‎lib/ldclient-rb/impl/event_sender.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require "securerandom"
44
require "http"
5+
require "stringio"
6+
require "zlib"
57

68
module LaunchDarkly
79
module Impl
@@ -42,14 +44,24 @@ def send_event_data(event_data, description, is_diagnostic)
4244
@logger.debug { "[LDClient] sending #{description}: #{event_data}" }
4345
headers = {}
4446
headers["content-type"] = "application/json"
47+
headers["content-encoding"] = "gzip" if @config.compress_events
4548
Impl::Util.default_http_headers(@sdk_key, @config).each { |k, v| headers[k] = v }
4649
unless is_diagnostic
4750
headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
4851
headers["X-LaunchDarkly-Payload-ID"] = payload_id
4952
end
53+
54+
body = event_data
55+
if @config.compress_events
56+
gzip = Zlib::GzipWriter.new(StringIO.new)
57+
gzip << event_data
58+
59+
body = gzip.close.string
60+
end
61+
5062
response = http_client.request("POST", uri, {
5163
headers: headers,
52-
body: event_data,
64+
body: body,
5365
})
5466
rescue StandardError => exn
5567
@logger.warn { "[LDClient] Error sending events: #{exn.inspect}." }

‎spec/http_util.rb

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
require "webrick"
22
require "webrick/httpproxy"
33
require "webrick/https"
4+
require "stringio"
5+
require "zlib"
46

57
class StubHTTPServer
68
attr_reader :requests, :port
79

810
@@next_port = 50000
911

10-
def initialize
12+
def initialize(enable_compression: false)
1113
@port = StubHTTPServer.next_port
14+
@enable_compression = enable_compression
1215
begin
1316
base_opts = {
1417
BindAddress: '127.0.0.1',
@@ -73,14 +76,16 @@ def record_request(req, res)
7376
@requests_queue << [req, req.body]
7477
end
7578

76-
def await_request
77-
r = @requests_queue.pop
78-
r[0]
79-
end
80-
8179
def await_request_with_body
8280
r = @requests_queue.pop
83-
[r[0], r[1]]
81+
request = r[0]
82+
body = r[1]
83+
84+
return [request, body.to_s] unless @enable_compression
85+
86+
gz = Zlib::GzipReader.new(StringIO.new(body.to_s))
87+
88+
[request, gz.read]
8489
end
8590
end
8691

@@ -90,8 +95,8 @@ def method_missing(*)
9095
end
9196
end
9297

93-
def with_server(server = nil)
94-
server = StubHTTPServer.new if server.nil?
98+
def with_server(enable_compression: false)
99+
server = StubHTTPServer.new(enable_compression: enable_compression)
95100
begin
96101
server.start
97102
yield server

‎spec/impl/event_sender_spec.rb

+41-16
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,48 @@ module Impl
1111
subject { EventSender }
1212

1313
let(:sdk_key) { "sdk_key" }
14-
let(:fake_data) { '{"things":[]}' }
14+
let(:fake_data) { '{"things":[],"stuff":false,"other examples":["you", "me", "us", "we"]}' }
1515

16-
def make_sender(server)
17-
make_sender_with_events_uri(server.base_uri.to_s)
16+
def make_sender(config_options = {})
17+
config_options = {logger: $null_log}.merge(config_options)
18+
subject.new(sdk_key, Config.new(config_options), nil, 0.1)
1819
end
1920

20-
def make_sender_with_events_uri(events_uri)
21-
subject.new(sdk_key, Config.new(events_uri: events_uri, logger: $null_log, application: {id: "id", version: "version"}), nil, 0.1)
21+
def with_sender_and_server(config_options = {})
22+
enable_compression = config_options[:compress_events] || false
23+
with_server(enable_compression: enable_compression) do |server|
24+
config_options[:events_uri] = server.base_uri.to_s
25+
yield make_sender(config_options), server
26+
end
2227
end
2328

24-
def with_sender_and_server
25-
with_server do |server|
26-
yield make_sender(server), server
29+
it "sends analytics event data without compression enabled" do
30+
with_sender_and_server(compress_events: false) do |es, server|
31+
server.setup_ok_response("/bulk", "")
32+
33+
result = es.send_event_data(fake_data, "", false)
34+
35+
expect(result.success).to be true
36+
expect(result.must_shutdown).to be false
37+
expect(result.time_from_server).not_to be_nil
38+
39+
req, body = server.await_request_with_body
40+
expect(body).to eq fake_data
41+
expect(req.header).to include({
42+
"authorization" => [ sdk_key ],
43+
"content-type" => [ "application/json" ],
44+
"user-agent" => [ "RubyClient/" + LaunchDarkly::VERSION ],
45+
"x-launchdarkly-event-schema" => [ "4" ],
46+
"connection" => [ "Keep-Alive" ],
47+
})
48+
expect(req.header['x-launchdarkly-payload-id']).not_to eq []
49+
expect(req.header['content-encoding']).to eq []
50+
expect(req.header['content-length'][0].to_i).to eq fake_data.length
2751
end
2852
end
2953

30-
it "sends analytics event data" do
31-
with_sender_and_server do |es, server|
54+
it "sends analytics event data with compression enabled" do
55+
with_sender_and_server(compress_events: true) do |es, server|
3256
server.setup_ok_response("/bulk", "")
3357

3458
result = es.send_event_data(fake_data, "", false)
@@ -37,17 +61,18 @@ def with_sender_and_server
3761
expect(result.must_shutdown).to be false
3862
expect(result.time_from_server).not_to be_nil
3963

40-
req = server.await_request
41-
expect(req.body).to eq fake_data
64+
req, body = server.await_request_with_body
65+
expect(body).to eq fake_data
4266
expect(req.header).to include({
4367
"authorization" => [ sdk_key ],
68+
"content-encoding" => [ "gzip" ],
4469
"content-type" => [ "application/json" ],
4570
"user-agent" => [ "RubyClient/" + LaunchDarkly::VERSION ],
4671
"x-launchdarkly-event-schema" => [ "4" ],
47-
"x-launchdarkly-tags" => [ "application-id/id application-version/version" ],
4872
"connection" => [ "Keep-Alive" ],
4973
})
5074
expect(req.header['x-launchdarkly-payload-id']).not_to eq []
75+
expect(req.header['content-length'][0].to_i).to be > fake_data.length
5176
end
5277
end
5378

@@ -63,8 +88,8 @@ def with_sender_and_server
6388
result = es.send_event_data(fake_data, "", false)
6489

6590
expect(result.success).to be true
66-
req = server.await_request
67-
expect(req.body).to eq fake_data
91+
req, body = server.await_request_with_body
92+
expect(body).to eq fake_data
6893
expect(req.host).to eq "fake-event-server"
6994
end
7095
end
@@ -123,7 +148,7 @@ def with_sender_and_server
123148
begin
124149
ENV["http_proxy"] = proxy.base_uri.to_s
125150

126-
es = make_sender_with_events_uri(fake_target_uri)
151+
es = make_sender(events_uri: fake_target_uri)
127152

128153
result = es.send_event_data(fake_data, "", false)
129154

0 commit comments

Comments
 (0)
Please sign in to comment.