From 8eb2d4c9eb90522de8a54bce1aba8784c470af99 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Thu, 20 Jun 2024 10:33:41 -0700 Subject: [PATCH 1/4] feat: [add ability to export configurations] (FF-2472) --- README.md | 22 ++++++++++++++++++++++ eppo_client/client.py | 8 ++++++++ eppo_client/configuration_requestor.py | 3 +++ eppo_client/configuration_store.py | 4 ++++ test/client_test.py | 5 +++++ test/configuration_store_test.py | 7 +++++++ 6 files changed, 49 insertions(+) diff --git a/README.md b/README.md index 7edef23..6d46eeb 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,28 @@ class SegmentAssignmentLogger(AssignmentLogger): client_config = Config(api_key="", assignment_logger=SegmentAssignmentLogger()) ``` +## Export configuration + +To support the use-case of needing to bootstrap a front-end client, the Eppo SDK provides a function to export flag configurations to a JSON string. + +Use the `get_flag_configurations` function to export flag configurations to a JSON string and then send it to the front-end client. + +```python +from fastapi import JSONResponse + +import eppo_client +import json + +client = eppo_client.get_instance() +flag_configurations = client.get_flag_configurations() + +# Convert flag configurations to a JSON string +flag_config_json = json.dumps(flag_configurations) + +# Create a JSONResponse object with the stringified JSON +response = JSONResponse(content={"flagConfigurations": flag_config_json}) +``` + ## Philosophy Eppo's SDKs are built for simplicity, speed and reliability. Flag configurations are compressed and distributed over a global CDN (Fastly), typically reaching your servers in under 15ms. Server SDKs continue polling Eppo’s API at 30-second intervals. Configurations are then cached locally, ensuring that each assignment is made instantly. Evaluation logic within each SDK consists of a few lines of simple numeric and string comparisons. The typed functions listed above are all developers need to understand, abstracting away the complexity of the Eppo's underlying (and expanding) feature set. diff --git a/eppo_client/client.py b/eppo_client/client.py index 640482a..cc7852c 100644 --- a/eppo_client/client.py +++ b/eppo_client/client.py @@ -10,6 +10,7 @@ ContextAttributes, ActionContexts, ) +from eppo_client.models import Flag from eppo_client.configuration_requestor import ( ExperimentConfigurationRequestor, ) @@ -386,6 +387,13 @@ def get_flag_keys(self): """ return self.__config_requestor.get_flag_keys() + def get_flag_configurations(self) -> Dict[str, Flag]: + """ + Returns a dictionary of all flag configurations that have been initialized. + This can be useful to debug the initialization process or to bootstrap a front-end client. + """ + return self.__config_requestor.get_flag_configurations() + def get_bandit_keys(self): """ Returns a list of all bandit keys that have been initialized. diff --git a/eppo_client/configuration_requestor.py b/eppo_client/configuration_requestor.py index 7dbb966..a0e5d96 100644 --- a/eppo_client/configuration_requestor.py +++ b/eppo_client/configuration_requestor.py @@ -36,6 +36,9 @@ def get_bandit_model(self, bandit_key: str) -> Optional[BanditData]: def get_flag_keys(self): return self.__flag_config_store.get_keys() + def get_flag_configurations(self): + return self.__flag_config_store.get_configurations() + def get_bandit_keys(self): return self.__bandit_config_store.get_keys() diff --git a/eppo_client/configuration_store.py b/eppo_client/configuration_store.py index 87b7be5..475e884 100644 --- a/eppo_client/configuration_store.py +++ b/eppo_client/configuration_store.py @@ -21,3 +21,7 @@ def set_configurations(self, configs: Dict[str, T]): def get_keys(self): with self.__lock.reader(): return set(self.__cache.keys()) + + def get_configurations(self): + with self.__lock.reader(): + return self.__cache diff --git a/test/client_test.py b/test/client_test.py index a260c88..3856c4a 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -218,6 +218,11 @@ def test_client_has_flags(): assert len(client.get_flag_keys()) > 0, "No flags have been loaded by the client" +def test_client_flag_configurations(): + client = get_instance() + assert len(client.get_flag_configurations()) > 0, "No flags have been loaded by the client" + + @pytest.mark.parametrize("test_case", test_data) def test_assign_subject_in_sample(test_case): client = get_instance() diff --git a/test/configuration_store_test.py b/test/configuration_store_test.py index 3bda8d0..5f79a7d 100644 --- a/test/configuration_store_test.py +++ b/test/configuration_store_test.py @@ -34,6 +34,13 @@ def test_get_keys(): assert len(keys) == 2 +def test_get_configurations(): + config = {"flag1": mock_flag, "flag2": mock_flag} + store.set_configurations(config) + configurations = store.get_configurations() + assert configurations == config + + def test_evicts_old_entries_when_max_size_exceeded(): store.set_configurations({"item_to_be_evicted": mock_flag}) assert store.get_configuration("item_to_be_evicted") == mock_flag From 35616a712c9ca6aefac53ba0f9e430f9c38b9dd0 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Thu, 20 Jun 2024 11:12:10 -0700 Subject: [PATCH 2/4] bump to 3.3.0 --- eppo_client/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eppo_client/version.py b/eppo_client/version.py index 1da6a55..88c513e 100644 --- a/eppo_client/version.py +++ b/eppo_client/version.py @@ -1 +1 @@ -__version__ = "3.2.1" +__version__ = "3.3.0" From 6b18d5ef1c526c3317d3621327a20f58b5c076d7 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Thu, 20 Jun 2024 11:13:45 -0700 Subject: [PATCH 3/4] lint --- test/client_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/client_test.py b/test/client_test.py index 3856c4a..858d136 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -220,7 +220,9 @@ def test_client_has_flags(): def test_client_flag_configurations(): client = get_instance() - assert len(client.get_flag_configurations()) > 0, "No flags have been loaded by the client" + assert len( + client.get_flag_configurations() + ) > 0, "No flags have been loaded by the client" @pytest.mark.parametrize("test_case", test_data) From dccc7769975387d956f0a1ee3f49419d3d6a72a8 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Thu, 20 Jun 2024 11:17:41 -0700 Subject: [PATCH 4/4] sigh --- test/client_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/client_test.py b/test/client_test.py index 858d136..9d340dc 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -220,9 +220,9 @@ def test_client_has_flags(): def test_client_flag_configurations(): client = get_instance() - assert len( - client.get_flag_configurations() - ) > 0, "No flags have been loaded by the client" + assert ( + len(client.get_flag_configurations()) > 0 + ), "No flags have been loaded by the client" @pytest.mark.parametrize("test_case", test_data)