Skip to content

Commit

Permalink
Ignore BI events through segment's webhook.
Browse files Browse the repository at this point in the history
  • Loading branch information
nasthagiri committed Dec 8, 2014
1 parent fd0c9d9 commit d89f9ca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
26 changes: 17 additions & 9 deletions common/djangoapps/track/views/segmentio.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
full_segment_event = request.json

# We mostly care about the properties
segment_event = full_segment_event.get('properties', {})
segment_properties = full_segment_event.get('properties', {})

# Start with the context provided by segment.io in the "client" field if it exists
# We should tightly control which fields actually get included in the event emitted.
Expand All @@ -136,10 +136,21 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
else:
context['event_source'] = event_source

# Ignore types that are unsupported
if 'name' not in segment_properties:
raise EventValidationError(ERROR_MISSING_NAME)

# Ignore event types and names that are unsupported
segment_event_type = full_segment_event.get('type')
segment_event_name = segment_properties['name']
allowed_types = [a.lower() for a in getattr(settings, 'TRACKING_SEGMENTIO_ALLOWED_TYPES', [])]
if not segment_event_type or segment_event_type.lower() not in allowed_types:
disallowed_substring_names = [
a.lower() for a in getattr(settings, 'TRACKING_SEGMENTIO_DISALLOWED_SUBSTRING_NAMES', [])
]
if (
not segment_event_type or
(segment_event_type.lower() not in allowed_types) or
any(disallowed_subs_name in segment_event_name.lower() for disallowed_subs_name in disallowed_substring_names)
):
raise EventValidationError(WARNING_IGNORED_TYPE)

if segment_context:
Expand All @@ -153,7 +164,7 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
del context['client'][field]

# Overlay any context provided in the properties
context.update(segment_event.get('context', {}))
context.update(segment_properties.get('context', {}))

user_id = full_segment_event.get('userId')
if not user_id:
Expand Down Expand Up @@ -195,13 +206,10 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
else:
raise EventValidationError(ERROR_MISSING_RECEIVED_AT)

if 'name' not in segment_event:
raise EventValidationError(ERROR_MISSING_NAME)

context['ip'] = segment_event.get('context', {}).get('ip', '')
context['ip'] = segment_properties.get('context', {}).get('ip', '')

with tracker.get_tracker().context('edx.segmentio', context):
tracker.emit(segment_event['name'], segment_event.get('data', {}))
tracker.emit(segment_event_name, segment_properties.get('data', {}))


def parse_iso8601_timestamp(timestamp):
Expand Down
7 changes: 6 additions & 1 deletion common/djangoapps/track/views/tests/test_segmentio.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def create_request(self, key=None, **kwargs):
def test_segmentio_ignore_actions(self, action):
self.post_segmentio_event(action=action)

@data('edx.bi.some_name', 'EDX.BI.CAPITAL_NAME')
@expect_failure_with_message(segmentio.WARNING_IGNORED_TYPE)
def test_segmentio_ignore_names(self, name):
self.post_segmentio_event(name=name)

def post_segmentio_event(self, **kwargs):
"""Post a fake segment.io event to the view that processes it"""
request = self.create_request(
Expand Down Expand Up @@ -305,7 +310,7 @@ def test_video_event(self, name, event_type):
name=name,
data=input_payload,
context={
'open_in_browser_url':'https://testserver/courses/foo/bar/baz/courseware/Week_1/Activity/2',
'open_in_browser_url': 'https://testserver/courses/foo/bar/baz/courseware/Week_1/Activity/2',
'course_id': course_id,
'application': {
'name': 'edx.mobileapp.android',
Expand Down
9 changes: 8 additions & 1 deletion lms/envs/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,15 @@
# Event tracking
TRACKING_BACKENDS.update(AUTH_TOKENS.get("TRACKING_BACKENDS", {}))
EVENT_TRACKING_BACKENDS.update(AUTH_TOKENS.get("EVENT_TRACKING_BACKENDS", {}))
TRACKING_SEGMENTIO_WEBHOOK_SECRET = AUTH_TOKENS.get("TRACKING_SEGMENTIO_WEBHOOK_SECRET", TRACKING_SEGMENTIO_WEBHOOK_SECRET)
TRACKING_SEGMENTIO_WEBHOOK_SECRET = AUTH_TOKENS.get(
"TRACKING_SEGMENTIO_WEBHOOK_SECRET",
TRACKING_SEGMENTIO_WEBHOOK_SECRET
)
TRACKING_SEGMENTIO_ALLOWED_TYPES = ENV_TOKENS.get("TRACKING_SEGMENTIO_ALLOWED_TYPES", TRACKING_SEGMENTIO_ALLOWED_TYPES)
TRACKING_SEGMENTIO_DISALLOWED_SUBSTRING_NAMES = ENV_TOKENS.get(
"TRACKING_SEGMENTIO_DISALLOWED_SUBSTRING_NAMES",
TRACKING_SEGMENTIO_DISALLOWED_SUBSTRING_NAMES
)
TRACKING_SEGMENTIO_SOURCE_MAP = ENV_TOKENS.get("TRACKING_SEGMENTIO_SOURCE_MAP", TRACKING_SEGMENTIO_SOURCE_MAP)

# Student identity verification settings
Expand Down
1 change: 1 addition & 0 deletions lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@

TRACKING_SEGMENTIO_WEBHOOK_SECRET = None
TRACKING_SEGMENTIO_ALLOWED_TYPES = ['track']
TRACKING_SEGMENTIO_DISALLOWED_SUBSTRING_NAMES = ['.bi.']
TRACKING_SEGMENTIO_SOURCE_MAP = {
'analytics-android': 'mobile',
'analytics-ios': 'mobile',
Expand Down

0 comments on commit d89f9ca

Please sign in to comment.