Skip to content

Commit

Permalink
feat: add event bus backend
Browse files Browse the repository at this point in the history
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
Ian2012 committed Feb 13, 2024
1 parent e95981b commit 176dc89
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
59 changes: 59 additions & 0 deletions eventtracking/backends/event_bus.py
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
41 changes: 41 additions & 0 deletions eventtracking/backends/tests/test_event_bus.py
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']
)
)
14 changes: 14 additions & 0 deletions eventtracking/config.py
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__)
2 changes: 2 additions & 0 deletions eventtracking/django/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ def ready(self):
# pylint: disable=import-outside-toplevel
from eventtracking.django.django_tracker import override_default_tracker
override_default_tracker()

import eventtracking.handlers # pylint: disable=unused-import

0 comments on commit 176dc89

Please sign in to comment.