Skip to content

Commit

Permalink
feat: implement event bus backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 25, 2023
1 parent 44b80ab commit 5bcd508
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions eventtracking/backends/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Event tracker backend that emits events to the event-bus."""
import json

from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED
from openedx_events.analytics.data import TrackingLogData

Expand All @@ -18,11 +20,13 @@ def send(self, event):
other apps to listen for tracking events.
"""
# .. event_implemented_name: TRACKING_EVENT_EMITTED
data = json.dumps(event.get('data'))
context = json.dumps(event.get('context'))
TRACKING_EVENT_EMITTED.send_event(
tracking_log=TrackingLogData(
name=event.get('name'),
timestamp=event.get('timestamp'),
data=event.get('data'),
context=event.get('context')
data=data,
context=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.signals # pylint: disable=unused-import
26 changes: 26 additions & 0 deletions eventtracking/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import logging

from django.dispatch import receiver
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED, TRACKING_EVENT_EMITTED_TO_BUS
from openedx_events.event_bus import get_producer

from eventtracking.config import SEND_TRACKING_EVENT_EMITTED_SIGNAL

logger = logging.getLogger(__name__)

@receiver(TRACKING_EVENT_EMITTED)
def listen_for_tracking_event_emitted_event(sender, signal, **kwargs):
"""
Publish `TRACKING_EVENT_EMITTED` events to the event bus.
"""
metadata = TRACKING_EVENT_EMITTED_TO_BUS.generate_signal_metadata()

if SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled():
logger.info("Sending events to event bus in batch")
get_producer().send(
signal=TRACKING_EVENT_EMITTED_TO_BUS,
topic='analytics',
event_key_field='tracking_log.name',
event_data={'tracking_log': kwargs['tracking_log']},
event_metadata=metadata
)

0 comments on commit 5bcd508

Please sign in to comment.