-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement event bus backend feat: create signal to send tracking log events to event bus refactor: sent event direclty to event bus
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Event tracker backend that emits events to the event-bus.""" | ||
import json | ||
|
||
from openedx_events.analytics.data import TrackingLogData | ||
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED | ||
|
||
from eventtracking.backends.routing import RoutingBackend | ||
from eventtracking.config import SEND_TRACKING_EVENT_EMITTED_SIGNAL | ||
from openedx_events.data import EventsMetadata | ||
from openedx_events.event_bus import get_producer | ||
from attrs import asdict | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
EVENT_BUS_SOURCE = "openedx/eventtracking" | ||
|
||
class EventBusRoutingBackend(RoutingBackend): | ||
""" | ||
Event tracker backend that emits an Open edX public signal. | ||
""" | ||
|
||
def send(self, event): | ||
""" | ||
Emit the TRACKING_EVENT_EMITTED Open edX public signal to allow | ||
other apps to listen for tracking events. | ||
""" | ||
if not SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled(): | ||
return | ||
|
||
data = json.dumps(event.get("data")) | ||
context = json.dumps(event.get("context")) | ||
|
||
tracking_log=TrackingLogData( | ||
name=event.get("name"), | ||
timestamp=event.get("timestamp"), | ||
data=data, | ||
context=context, | ||
) | ||
|
||
logger.info(f"Sending tracking event emitted signal for event for {tracking_log.name}") | ||
get_producer().send( | ||
signal=TRACKING_EVENT_EMITTED, | ||
topic="analytics", | ||
event_key_field="tracking_log.name", | ||
event_data={"tracking_log": tracking_log}, | ||
event_metadata=generate_signal_metadata() | ||
) | ||
|
||
|
||
def generate_signal_metadata(): | ||
""" | ||
Generate the metadata for the signal with a custom source. | ||
""" | ||
metadata = TRACKING_EVENT_EMITTED.generate_signal_metadata() | ||
medata_dict = asdict(metadata) | ||
medata_dict["source"] = EVENT_BUS_SOURCE | ||
metadata = EventsMetadata(**medata_dict) | ||
return metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
Test the async routing backend. | ||
""" | ||
from unittest import TestCase | ||
|
||
from unittest.mock import sentinel, patch | ||
from eventtracking.backends.event_bus import EventBusRoutingBackend | ||
from openedx_events.analytics.data import TrackingLogData | ||
|
||
class TestAsyncRoutingBackend(TestCase): | ||
""" | ||
Test the async routing backend. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.sample_event = { | ||
'name': str(sentinel.name), | ||
'data': 'data', | ||
'timestamp': '2020-01-01T12:12:12.000000+00:00', | ||
'context': {}, | ||
} | ||
|
||
@patch('eventtracking.backends.event_bus.EventBusRoutingBackend.send') | ||
def test_successful_send(self, mocked_send_event): | ||
backend = EventBusRoutingBackend() | ||
backend.send(self.sample_event) | ||
mocked_send_event.assert_called_once_with(self.sample_event) | ||
|
||
@patch('eventtracking.backends.event_bus.TRACKING_EVENT_EMITTED.send_event') | ||
def test_successful_send_event(self, mocked_send_event): | ||
backend = EventBusRoutingBackend() | ||
backend.send(self.sample_event) | ||
mocked_send_event.assert_called_once_with( | ||
tracking_log=TrackingLogData( | ||
name=self.sample_event['name'], | ||
timestamp=self.sample_event['timestamp'], | ||
data=self.sample_event['data'], | ||
context=self.sample_event['context'] | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
This module contains various configuration settings via | ||
waffle switches for the Certificates app. | ||
""" | ||
|
||
from edx_toggles.toggles import SettingToggle | ||
|
||
# .. toggle_name: SEND_TRACKING_EVENT_EMITTED_SIGNAL | ||
# .. toggle_implementation: SettingToggle | ||
# .. toggle_default: False | ||
# .. toggle_description: When True, the system will publish `TRACKING_EVENT_EMITTED` signals to the event bus. The | ||
# `TRACKING_EVENT_EMITTED` signal is emit when a tracking log is emitted. | ||
# .. toggle_use_cases: publish | ||
SEND_TRACKING_EVENT_EMITTED_SIGNAL = SettingToggle('SEND_TRACKING_EVENT_EMITTED_SIGNAL', default=True, module_name=__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters