-
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.
test: add test for event bus backend
fix: only generate metadata fix: assert for subset ignoring runtime generated only metadata chore: quality fixes chore: improve docstring fix: convert timestamp str to datetime object test: add test for str datetime fix: rename tracking log event emitted fix: rename tracking log event emitted config fix: rename tracking event chore: handle comments feat: add settings for allowed events in the event bus chore: use opt_in for tracking logs event bus toggle fix: serialize tracking log data and context dates as logger chore: use send_event instead of custom metadata test: update unit test docs: add event bus routing documentation
- Loading branch information
Showing
10 changed files
with
209 additions
and
68 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
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
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 |
---|---|---|
@@ -1,59 +1,52 @@ | ||
"""Event tracker backend that emits events to the event-bus.""" | ||
|
||
import json | ||
import logging | ||
from datetime import datetime | ||
|
||
from django.conf import settings | ||
from openedx_events.analytics.data import TrackingLogData | ||
from openedx_events.analytics.signals import TRACKING_EVENT_EMITTED | ||
|
||
from eventtracking.backends.logger import DateTimeJSONEncoder | ||
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. | ||
Event tracker backend for the event bus. | ||
""" | ||
|
||
def send(self, event): | ||
""" | ||
Emit the TRACKING_EVENT_EMITTED Open edX public signal to allow | ||
other apps to listen for tracking events. | ||
Send the tracking log event to the event bus by emitting the | ||
TRACKING_EVENT_EMITTED signal using custom metadata. | ||
""" | ||
if not SEND_TRACKING_EVENT_EMITTED_SIGNAL.is_enabled(): | ||
return | ||
|
||
data = json.dumps(event.get("data")) | ||
context = json.dumps(event.get("context")) | ||
name = event.get("name") | ||
|
||
if name not in getattr(settings, "EVENT_BUS_TRACKING_LOGS", []): | ||
return | ||
|
||
data = json.dumps(event.get("data"), cls=DateTimeJSONEncoder) | ||
context = json.dumps(event.get("context"), cls=DateTimeJSONEncoder) | ||
|
||
timestamp = event.get("timestamp") | ||
|
||
tracking_log=TrackingLogData( | ||
if isinstance(timestamp, str): | ||
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z") | ||
|
||
tracking_log = TrackingLogData( | ||
name=event.get("name"), | ||
timestamp=event.get("timestamp"), | ||
timestamp=timestamp, | ||
data=data, | ||
context=context, | ||
) | ||
TRACKING_EVENT_EMITTED.send_event(tracking_log=tracking_log) | ||
|
||
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 | ||
logger.info(f"Tracking log {tracking_log.name} emitted to the event bus.") |
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
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
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
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
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
Oops, something went wrong.