Skip to content

[FSSDK-11458] Python - Add SDK Multi-Region Support for Data Hosting #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[FSSDK-11458] Python - Add SDK Multi-Region Support for Data Hosting
  • Loading branch information
esrakartalOpt committed Jul 2, 2025
commit d9e8f83dbb5f0734de5ab98959d0334b230b250a
10 changes: 8 additions & 2 deletions optimizely/event/event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ class EventFactory:
to record the events via the Optimizely Events API ("https://developers.optimizely.com/x/events/api/index.html")
"""

EVENT_ENDPOINT: Final = 'https://logx.optimizely.com/v1/events'
EVENT_ENDPOINTS: Final = {
'US': 'https://logx.optimizely.com/v1/events',
'EU': 'https://eu.logx.optimizely.com/v1/events'
}
HTTP_VERB: Final = 'POST'
HTTP_HEADERS: Final = {'Content-Type': 'application/json'}
ACTIVATE_EVENT_KEY: Final = 'campaign_activated'
Expand Down Expand Up @@ -97,7 +100,10 @@ def create_log_event(

event_params = event_batch.get_event_params()

return log_event.LogEvent(cls.EVENT_ENDPOINT, event_params, cls.HTTP_VERB, cls.HTTP_HEADERS)
region = user_context.region or 'US'
endpoint = cls.EVENT_ENDPOINTS.get(region)

return log_event.LogEvent(endpoint, event_params, cls.HTTP_VERB, cls.HTTP_HEADERS)

@classmethod
def _create_visitor(cls, event: Optional[user_event.UserEvent], logger: Logger) -> Optional[payload.Visitor]:
Expand Down
4 changes: 3 additions & 1 deletion optimizely/event/user_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sys import version_info

from optimizely import version
from optimizely.project_config import Region


if version_info < (3, 8):
Expand Down Expand Up @@ -97,10 +98,11 @@ def __init__(
class EventContext:
""" Class respresenting User Event Context. """

def __init__(self, account_id: str, project_id: str, revision: str, anonymize_ip: bool):
def __init__(self, account_id: str, project_id: str, revision: str, anonymize_ip: bool, region: Region):
self.account_id = account_id
self.project_id = project_id
self.revision = revision
self.client_name = CLIENT_NAME
self.client_version = version.__version__
self.anonymize_ip = anonymize_ip
self.region = region or 'US'
4 changes: 2 additions & 2 deletions optimizely/event/user_event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create_impression_event(
variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)

event_context = user_event.EventContext(
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip,
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip, project_config.region
)

return user_event.ImpressionEvent(
Expand Down Expand Up @@ -115,7 +115,7 @@ def create_conversion_event(
"""

event_context = user_event.EventContext(
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip,
project_config.account_id, project_config.project_id, project_config.revision, project_config.anonymize_ip, project_config.region
)

return user_event.ConversionEvent(
Expand Down
16 changes: 13 additions & 3 deletions optimizely/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class EventBuilder:
""" Class which encapsulates methods to build events for tracking
impressions and conversions using the new V3 event API (batch). """

EVENTS_URL: Final = 'https://logx.optimizely.com/v1/events'
EVENTS_URLS: Final = {
'US': 'https://logx.optimizely.com/v1/events',
'EU': 'https://eu.logx.optimizely.com/v1/events'
}
HTTP_VERB: Final = 'POST'
HTTP_HEADERS: Final = {'Content-Type': 'application/json'}

Expand Down Expand Up @@ -266,7 +269,10 @@ def create_impression_event(

params[self.EventParams.USERS][0][self.EventParams.SNAPSHOTS].append(impression_params)

return Event(self.EVENTS_URL, params, http_verb=self.HTTP_VERB, headers=self.HTTP_HEADERS)
region = project_config.region or 'US'
events_url = self.EVENTS_URLS.get(region)

return Event(events_url, params, http_verb=self.HTTP_VERB, headers=self.HTTP_HEADERS)

def create_conversion_event(
self, project_config: ProjectConfig, event_key: str,
Expand All @@ -289,4 +295,8 @@ def create_conversion_event(
conversion_params = self._get_required_params_for_conversion(project_config, event_key, event_tags)

params[self.EventParams.USERS][0][self.EventParams.SNAPSHOTS].append(conversion_params)
return Event(self.EVENTS_URL, params, http_verb=self.HTTP_VERB, headers=self.HTTP_HEADERS)

region = project_config.region or 'US'
events_url = self.EVENTS_URLS.get(region)

return Event(events_url, params, http_verb=self.HTTP_VERB, headers=self.HTTP_HEADERS)
7 changes: 5 additions & 2 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from .helpers import types

if version_info < (3, 8):
from typing_extensions import Final
from typing_extensions import Final, Literal
else:
from typing import Final # type: ignore
from typing import Final, Literal # type: ignore

if TYPE_CHECKING:
# prevent circular dependenacy by skipping import at runtime
Expand All @@ -41,6 +41,8 @@

EntityClass = TypeVar('EntityClass')

Region = Literal['US', 'EU']


class ProjectConfig:
""" Representation of the Optimizely project config. """
Expand Down Expand Up @@ -84,6 +86,7 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
self.public_key_for_odp: Optional[str] = None
self.host_for_odp: Optional[str] = None
self.all_segments: list[str] = []
self.region: Region = config.get('region') or 'US'

# Utility maps for quick lookup
self.group_id_map: dict[str, entities.Group] = self._generate_key_map(self.groups, 'id', entities.Group)
Expand Down
27 changes: 26 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from optimizely import logger
from optimizely import optimizely
from optimizely.helpers import enums
from optimizely.project_config import ProjectConfig
from optimizely.project_config import ProjectConfig, Region
from . import base


Expand Down Expand Up @@ -154,6 +154,31 @@ def test_init(self):
self.assertEqual(expected_variation_key_map, self.project_config.variation_key_map)
self.assertEqual(expected_variation_id_map, self.project_config.variation_id_map)

def test_region_when_no_region(self):
""" Test that region defaults to 'US' when not specified in the config. """
config_dict = copy.deepcopy(self.config_dict_with_multiple_experiments)
opt_obj = optimizely.Optimizely(json.dumps(config_dict))
project_config = opt_obj.config_manager.get_config()
self.assertEqual(project_config.region, Region.US)


def test_region_when_specified_in_datafile(self):
""" Test that region is set to 'US' when specified in the config. """
config_dict_us = copy.deepcopy(self.config_dict_with_multiple_experiments)
config_dict_us['region'] = 'US'
opt_obj_us = optimizely.Optimizely(json.dumps(config_dict_us))
project_config_us = opt_obj_us.config_manager.get_config()
self.assertEqual(project_config_us.region, Region.US)

""" Test that region is set to 'EU' when specified in the config. """
config_dict_eu = copy.deepcopy(self.config_dict_with_multiple_experiments)
config_dict_eu['region'] = 'EU'
opt_obj_eu = optimizely.Optimizely(json.dumps(config_dict_eu))
project_config_eu = opt_obj_eu.config_manager.get_config()
self.assertEqual(project_config_eu.region, Region.EU)



def test_cmab_field_population(self):
""" Test that the cmab field is populated correctly in experiments."""

Expand Down
84 changes: 84 additions & 0 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def test_activate(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand All @@ -385,6 +386,76 @@ def test_activate(self):
{'Content-Type': 'application/json'},
)

def test_activate_with_eu_hosting(self):
""" Test that activate calls process with right params and returns expected variation. """
""" Test EU hosting for activate method. """

with mock.patch(
'optimizely.decision_service.DecisionService.get_variation',
return_value=(self.project_config.get_variation_from_id('test_experiment', '111129'), []),
) as mock_decision, mock.patch('time.time', return_value=42), mock.patch(
'uuid.uuid4', return_value='a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
), mock.patch(
'optimizely.event.event_processor.BatchEventProcessor.process'
) as mock_process:
self.assertEqual('variation', self.optimizely.activate('test_experiment', 'test_user'))

expected_params = {
'account_id': '12001',
'project_id': '111001',
'visitors': [
{
'visitor_id': 'test_user',
'attributes': [],
'snapshots': [
{
'decisions': [
{'variation_id': '111129', 'experiment_id': '111127', 'campaign_id': '111182',
'metadata': {'flag_key': '',
'rule_key': 'test_experiment',
'rule_type': 'experiment',
'variation_key': 'variation',
'enabled': True},
}
],
'events': [
{
'timestamp': 42000,
'entity_id': '111182',
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
'key': 'campaign_activated',
}
],
}
],
}
],
'client_version': version.__version__,
'client_name': 'python-sdk',
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'EU',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
user_context = mock_decision.call_args[0][2]
user_profile_tracker = mock_decision.call_args[0][3]

mock_decision.assert_called_once_with(
self.project_config, self.project_config.get_experiment_from_key('test_experiment'),
user_context, user_profile_tracker
)
self.assertEqual(1, mock_process.call_count)

self._validate_event_object(
log_event.__dict__,
'https://eu.logx.optimizely.com/v1/events',
expected_params,
'POST',
{'Content-Type': 'application/json'},
)

def test_add_activate_remove_clear_listener(self):
callbackhit = [False]
""" Test adding a listener activate passes correctly and gets called"""
Expand Down Expand Up @@ -764,6 +835,7 @@ def test_activate__with_attributes__audience_match(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -848,6 +920,7 @@ def test_activate__with_attributes_of_different_types(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1044,6 +1117,7 @@ def test_activate__with_attributes__audience_match__forced_bucketing(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1120,6 +1194,7 @@ def test_activate__with_attributes__audience_match__bucketing_id_provided(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1288,6 +1363,7 @@ def test_track__with_attributes(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1424,6 +1500,7 @@ def test_track__with_attributes__bucketing_id_provided(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1504,6 +1581,7 @@ def test_track__with_event_tags(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}
log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)

Expand Down Expand Up @@ -1560,6 +1638,7 @@ def test_track__with_event_tags_revenue(self):
'account_id': '12001',
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1648,6 +1727,7 @@ def test_track__with_event_tags__forced_bucketing(self):
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -1703,6 +1783,7 @@ def test_track__with_invalid_event_tags(self):
'account_id': '12001',
'anonymize_ip': False,
'revision': '42',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -2103,6 +2184,7 @@ def test_is_feature_enabled__returns_true_for_feature_experiment_if_feature_enab
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '1',
'region': 'US',
}

log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)
Expand Down Expand Up @@ -2204,6 +2286,7 @@ def test_is_feature_enabled__returns_false_for_feature_experiment_if_feature_dis
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '1',
'region': 'US',
}
log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)

Expand Down Expand Up @@ -2356,6 +2439,7 @@ def test_is_feature_enabled__returns_true_for_feature_rollout_if_feature_enabled
'enrich_decisions': True,
'anonymize_ip': False,
'revision': '1',
'region': 'US',
}
log_event = EventFactory.create_log_event(mock_process.call_args[0][0], self.optimizely.logger)

Expand Down
Loading
Loading