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 all commits
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
12 changes: 10 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 @@ -83,21 +86,26 @@ def create_log_event(
return None

user_context = first_event.event_context
region_value = user_context.region.value if hasattr(user_context.region, 'value') else user_context.region
event_batch = payload.EventBatch(
user_context.account_id,
user_context.project_id,
user_context.revision,
user_context.client_name,
user_context.client_version,
user_context.anonymize_ip,
region_value,
True,
)

event_batch.visitors = visitors

event_params = event_batch.get_event_params()

return log_event.LogEvent(cls.EVENT_ENDPOINT, event_params, cls.HTTP_VERB, cls.HTTP_HEADERS)
region_str = user_context.region.value if hasattr(user_context.region, 'value') else str(user_context.region)
endpoint = cls.EVENT_ENDPOINTS.get(region_str, cls.EVENT_ENDPOINTS['US'])

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
2 changes: 2 additions & 0 deletions optimizely/event/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
anonymize_ip: bool,
enrich_decisions: bool = True,
visitors: Optional[list[Visitor]] = None,
region: str = 'US'
):
self.account_id = account_id
self.project_id = project_id
Expand All @@ -43,6 +44,7 @@ def __init__(
self.anonymize_ip = anonymize_ip
self.enrich_decisions = enrich_decisions
self.visitors = visitors or []
self.region = region

def __eq__(self, other: object) -> bool:
batch_obj = self.get_event_params()
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'
12 changes: 10 additions & 2 deletions optimizely/event/user_event_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ 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 +119,11 @@ 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
26 changes: 21 additions & 5 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 @@ -246,7 +249,8 @@ def _get_required_params_for_conversion(

def create_impression_event(
self, project_config: ProjectConfig, experiment: Experiment,
variation_id: str, user_id: str, attributes: UserAttributes
variation_id: str, user_id: str, attributes: UserAttributes,
region: str = 'US'
) -> Event:
""" Create impression Event to be sent to the logging endpoint.

Expand All @@ -266,11 +270,17 @@ 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)
params['region'] = project_config.region.value

region = project_config.region or 'US'
events_url = self.EVENTS_URLS.get(str(region), self.EVENTS_URLS['US'])

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,
user_id: str, attributes: UserAttributes, event_tags: event_tag_utils.EventTags
user_id: str, attributes: UserAttributes, event_tags: event_tag_utils.EventTags,
region: str = 'US'
) -> Event:
""" Create conversion Event to be sent to the logging endpoint.

Expand All @@ -289,4 +299,10 @@ 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)

params['region'] = project_config.region.value

region = project_config.region or 'US'
events_url = self.EVENTS_URLS.get(str(region), self.EVENTS_URLS['US'])

return Event(events_url, params, http_verb=self.HTTP_VERB, headers=self.HTTP_HEADERS)
13 changes: 13 additions & 0 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import json
from typing import TYPE_CHECKING, Optional, Type, TypeVar, cast, Any, Iterable, List
from sys import version_info
from enum import Enum

from . import entities
from . import exceptions
Expand Down Expand Up @@ -42,6 +43,11 @@
EntityClass = TypeVar('EntityClass')


class Region(str, Enum):
US = 'US'
EU = 'EU'


class ProjectConfig:
""" Representation of the Optimizely project config. """

Expand Down Expand Up @@ -85,6 +91,13 @@ def __init__(self, datafile: str | bytes, logger: Logger, error_handler: Any):
self.host_for_odp: Optional[str] = None
self.all_segments: list[str] = []

region_value = config.get('region')
self.region: Region
if region_value == Region.EU.value:
self.region = Region.EU
else:
self.region = Region.US

# Utility maps for quick lookup
self.group_id_map: dict[str, entities.Group] = self._generate_key_map(self.groups, 'id', entities.Group)
self.experiment_id_map: dict[str, entities.Experiment] = self._generate_key_map(
Expand Down
8 changes: 7 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def fake_server_response(self, status_code: Optional[int] = None,

def setUp(self, config_dict='config_dict'):
self.config_dict = {
'region': 'US',
'revision': '42',
'sdkKey': 'basic-test',
'version': '2',
Expand Down Expand Up @@ -150,6 +151,7 @@ def setUp(self, config_dict='config_dict'):

# datafile version 4
self.config_dict_with_features = {
'region': 'US',
'revision': '1',
'sdkKey': 'features-test',
'accountId': '12001',
Expand Down Expand Up @@ -553,6 +555,7 @@ def setUp(self, config_dict='config_dict'):
}

self.config_dict_with_multiple_experiments = {
'region': 'US',
'revision': '42',
'sdkKey': 'multiple-experiments',
'version': '2',
Expand Down Expand Up @@ -686,6 +689,7 @@ def setUp(self, config_dict='config_dict'):
'accountId': '10367498574',
'events': [{'experimentIds': ['10420810910'], 'id': '10404198134', 'key': 'winning'}],
'revision': '1337',
'region': 'US',
}

self.config_dict_with_typed_audiences = {
Expand Down Expand Up @@ -1078,6 +1082,7 @@ def setUp(self, config_dict='config_dict'):
],
'revision': '3',
'sdkKey': 'typed-audiences',
'region': 'US',
}

self.config_dict_with_audience_segments = {
Expand Down Expand Up @@ -1274,7 +1279,8 @@ def setUp(self, config_dict='config_dict'):
}
],
'revision': '101',
'sdkKey': 'segments-test'
'sdkKey': 'segments-test',
'region': 'US',
}

config = getattr(self, config_dict)
Expand Down
24 changes: 23 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,28 @@ 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
Loading
Loading