Skip to content

Commit 04c14c6

Browse files
authored
feat: Add option to enable event payload compression (#299)
1 parent 0c6af17 commit 04c14c6

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

contract-tests/client_entity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self, tag, config):
4141

4242
if config.get("events") is not None:
4343
events = config["events"]
44+
opts["enable_event_compression"] = events.get("enableGzip", False)
4445
if events.get("baseUri") is not None:
4546
opts["events_uri"] = events["baseUri"]
4647
if events.get("capacity") is not None:

contract-tests/service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def status():
7171
'tags',
7272
'migrations',
7373
'event-gzip',
74+
'optional-event-gzip',
7475
'event-sampling',
7576
'polling-gzip',
7677
'inline-context',

ldclient/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def __init__(self,
175175
http: HTTPConfig=HTTPConfig(),
176176
big_segments: Optional[BigSegmentsConfig]=None,
177177
application: Optional[dict]=None,
178-
hooks: Optional[List[Hook]]=None):
178+
hooks: Optional[List[Hook]]=None,
179+
enable_event_compression: bool=False):
179180
"""
180181
:param sdk_key: The SDK key for your LaunchDarkly account. This is always required.
181182
:param base_uri: The base URL for the LaunchDarkly server. Most users should use the default
@@ -241,6 +242,7 @@ def __init__(self,
241242
:class:`HTTPConfig`.
242243
:param application: Optional properties for setting application metadata. See :py:attr:`~application`
243244
:param hooks: Hooks provide entrypoints which allow for observation of SDK functions.
245+
:param enable_event_compression: Whether or not to enable GZIP compression for outgoing events.
244246
"""
245247
self.__sdk_key = sdk_key
246248

@@ -274,6 +276,7 @@ def __init__(self,
274276
self.__big_segments = BigSegmentsConfig() if not big_segments else big_segments
275277
self.__application = validate_application_info(application or {}, log)
276278
self.__hooks = [hook for hook in hooks if isinstance(hook, Hook)] if hooks else []
279+
self.__enable_event_compression = enable_event_compression
277280
self._data_source_update_sink: Optional[DataSourceUpdateSink] = None
278281

279282
def copy_with_new_sdk_key(self, new_sdk_key: str) -> 'Config':
@@ -459,6 +462,10 @@ def hooks(self) -> List[Hook]:
459462
"""
460463
return self.__hooks
461464

465+
@property
466+
def enable_event_compression(self) -> bool:
467+
return self.__enable_event_compression
468+
462469
@property
463470
def data_source_update_sink(self) -> Optional[DataSourceUpdateSink]:
464471
"""

ldclient/impl/events/event_processor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,15 @@ def _post_events_with_retry(
560560
):
561561
hdrs = _headers(config)
562562
hdrs['Content-Type'] = 'application/json'
563-
hdrs['Content-Encoding'] = 'gzip'
563+
if config.enable_event_compression:
564+
hdrs['Content-Encoding'] = 'gzip'
565+
564566
if payload_id:
565567
hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
566568
hdrs['X-LaunchDarkly-Payload-ID'] = payload_id
567569
can_retry = True
568570
context = "posting %s" % events_description
569-
data = gzip.compress(bytes(body, 'utf-8'))
571+
data = gzip.compress(bytes(body, 'utf-8')) if config.enable_event_compression else body
570572
while True:
571573
next_action_message = "will retry" if can_retry else "some events were dropped"
572574
try:

0 commit comments

Comments
 (0)