Skip to content
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

feat: use new setting to set ce_source #305

Merged
merged 19 commits into from
Jan 25, 2024
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Change Log
Unreleased
----------

[9.3.0] - 2024-01-24
--------------------
Changed
~~~~~~~
* Allow new EVENTS_SERVICE_NAME setting to override SERVICE_VARIANT for data source.

[9.2.0] - 2023-11-16
--------------------
Added
Expand Down
2 changes: 1 addition & 1 deletion openedx_events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
more information about the project.
"""

__version__ = "9.2.0"
__version__ = "9.3.0"
27 changes: 24 additions & 3 deletions openedx_events/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ def _ensure_utc_time(_, attribute, value):
raise ValueError(f"'{attribute.name}' must have timezone.utc")


def get_service_name():
"""
Get the service name of the producing/consuming service of an event (or None if not set).

Uses EVENTS_SERVICE_NAME setting if present, otherwise looks for SERVICE_VARIANT.
"""
# .. setting_name: EVENTS_SERVICE_NAME
# .. setting_default: None
# .. setting_description: Identifier for the producing/consuming service of an event. For example, "cms" or
# "course-discovery." Used, among other places, to determine the source header of the event.
return getattr(settings, "EVENTS_SERVICE_NAME", None) or getattr(settings, "SERVICE_VARIANT", None)


def _get_source():
"""
Get the source for an event using the service name.

If the service name is set, the full source will be set to openedx/<service_name>/web or
openedx/SERVICE_NAME_UNSET/web if service name is None.
"""
return "openedx/{service}/web".format(service=(get_service_name() or "SERVICE_NAME_UNSET"))


@attr.s(frozen=True)
class EventsMetadata:
"""
Expand Down Expand Up @@ -61,9 +84,7 @@ class EventsMetadata:
)
source = attr.ib(
type=str, default=None,
converter=attr.converters.default_if_none(
attr.Factory(lambda: "openedx/{service}/web".format(service=getattr(settings, "SERVICE_VARIANT", "")))
),
converter=attr.converters.default_if_none(attr.Factory(_get_source)),
validator=attr.validators.instance_of(str),
)
sourcehost = attr.ib(
Expand Down
20 changes: 20 additions & 0 deletions openedx_events/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from datetime import datetime, timezone
from uuid import UUID

import ddt
from django.test import TestCase
from django.test.utils import override_settings

from openedx_events.data import EventsMetadata


@ddt.ddt
class TestEventsMetadata(TestCase):
"""
Tests for the EventsMetadata class.
Expand All @@ -26,3 +29,20 @@ def test_events_metadata_to_and_from_json(self):
as_json = self.metadata.to_json()
from_json = EventsMetadata.from_json(as_json)
self.assertEqual(self.metadata, from_json)

@ddt.data(
('settings_variant', None, 'openedx/settings_variant/web'),
(None, 'my_service', 'openedx/my_service/web'),
(None, None, 'openedx/SERVICE_NAME_UNSET/web'),
('settings_variant', 'my_service', 'openedx/my_service/web')
)
@ddt.unpack
def test_events_metadata_source(self, settings_variant, event_bus_service_name, expected_source):
with override_settings(
SERVICE_VARIANT=settings_variant,
EVENTS_SERVICE_NAME=event_bus_service_name,
):
metadata = EventsMetadata(
event_type='test_type'
)
self.assertEqual(metadata.source, expected_source)
Loading