Skip to content

Commit

Permalink
feat: Add option to enable event payload compression (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Jul 25, 2024
1 parent 0c6af17 commit 04c14c6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions contract-tests/client_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, tag, config):

if config.get("events") is not None:
events = config["events"]
opts["enable_event_compression"] = events.get("enableGzip", False)
if events.get("baseUri") is not None:
opts["events_uri"] = events["baseUri"]
if events.get("capacity") is not None:
Expand Down
1 change: 1 addition & 0 deletions contract-tests/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def status():
'tags',
'migrations',
'event-gzip',
'optional-event-gzip',
'event-sampling',
'polling-gzip',
'inline-context',
Expand Down
9 changes: 8 additions & 1 deletion ldclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def __init__(self,
http: HTTPConfig=HTTPConfig(),
big_segments: Optional[BigSegmentsConfig]=None,
application: Optional[dict]=None,
hooks: Optional[List[Hook]]=None):
hooks: Optional[List[Hook]]=None,
enable_event_compression: bool=False):
"""
:param sdk_key: The SDK key for your LaunchDarkly account. This is always required.
:param base_uri: The base URL for the LaunchDarkly server. Most users should use the default
Expand Down Expand Up @@ -241,6 +242,7 @@ def __init__(self,
:class:`HTTPConfig`.
:param application: Optional properties for setting application metadata. See :py:attr:`~application`
:param hooks: Hooks provide entrypoints which allow for observation of SDK functions.
:param enable_event_compression: Whether or not to enable GZIP compression for outgoing events.
"""
self.__sdk_key = sdk_key

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

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

@property
def enable_event_compression(self) -> bool:
return self.__enable_event_compression

@property
def data_source_update_sink(self) -> Optional[DataSourceUpdateSink]:
"""
Expand Down
6 changes: 4 additions & 2 deletions ldclient/impl/events/event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,15 @@ def _post_events_with_retry(
):
hdrs = _headers(config)
hdrs['Content-Type'] = 'application/json'
hdrs['Content-Encoding'] = 'gzip'
if config.enable_event_compression:
hdrs['Content-Encoding'] = 'gzip'

if payload_id:
hdrs['X-LaunchDarkly-Event-Schema'] = str(__CURRENT_EVENT_SCHEMA__)
hdrs['X-LaunchDarkly-Payload-ID'] = payload_id
can_retry = True
context = "posting %s" % events_description
data = gzip.compress(bytes(body, 'utf-8'))
data = gzip.compress(bytes(body, 'utf-8')) if config.enable_event_compression else body
while True:
next_action_message = "will retry" if can_retry else "some events were dropped"
try:
Expand Down

0 comments on commit 04c14c6

Please sign in to comment.