Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
9d72a785
Unverified
Commit
9d72a785
authored
4 years ago
by
Troy Sankey
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #25630 from edx/robrap/fix-segment-error
track: catch unhandled exceptions and add monitoring
parents
d984db4c
186f5fbc
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/djangoapps/track/views/segmentio.py
+15
-1
15 additions, 1 deletion
common/djangoapps/track/views/segmentio.py
common/djangoapps/track/views/tests/test_segmentio.py
+22
-0
22 additions, 0 deletions
common/djangoapps/track/views/tests/test_segmentio.py
with
37 additions
and
1 deletion
common/djangoapps/track/views/segmentio.py
+
15
−
1
View file @
9d72a785
...
...
@@ -10,6 +10,7 @@ from django.contrib.auth.models import User
from
django.http
import
HttpResponse
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.http
import
require_POST
from
edx_django_utils.monitoring
import
set_custom_attribute
from
eventtracking
import
tracker
from
opaque_keys
import
InvalidKeyError
from
opaque_keys.edx.keys
import
CourseKey
...
...
@@ -23,6 +24,8 @@ ERROR_UNAUTHORIZED = 'Unauthorized'
ERROR_MISSING_USER_ID
=
'
Required user_id missing from context
'
ERROR_USER_NOT_EXIST
=
'
Specified user does not exist
'
ERROR_INVALID_USER_ID
=
'
Unable to parse userId as an integer
'
ERROR_INVALID_CONTEXT_FIELD_TYPE
=
'
The properties.context field is not a dict.
'
ERROR_INVALID_DATA_FIELD_TYPE
=
'
The properties.data field is not a dict.
'
ERROR_MISSING_DATA
=
'
The data field must be specified in the properties dictionary
'
ERROR_MISSING_NAME
=
'
The name field must be specified in the properties dictionary
'
ERROR_MISSING_TIMESTAMP
=
'
Required timestamp field not found
'
...
...
@@ -155,11 +158,22 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
if
any
(
disallowed_subs_name
in
segment_event_name
.
lower
()
for
disallowed_subs_name
in
disallowed_substring_names
):
return
set_custom_attribute
(
'
segment_event_name
'
,
segment_event_name
)
set_custom_attribute
(
'
segment_event_source
'
,
event_source
)
# Attempt to extract and validate the data field.
if
'
data
'
not
in
segment_properties
:
raise
EventValidationError
(
ERROR_MISSING_DATA
)
segment_event_data
=
segment_properties
.
get
(
'
data
'
,
{})
if
type
(
segment_event_data
)
is
not
dict
:
set_custom_attribute
(
'
segment_unexpected_data
'
,
str
(
segment_event_data
))
raise
EventValidationError
(
ERROR_INVALID_DATA_FIELD_TYPE
)
# create and populate application field if it doesn't exist
app_context
=
segment_properties
.
get
(
'
context
'
,
{})
if
type
(
app_context
)
is
not
dict
:
set_custom_attribute
(
'
segment_unexpected_context
'
,
str
(
app_context
))
raise
EventValidationError
(
ERROR_INVALID_CONTEXT_FIELD_TYPE
)
if
'
application
'
not
in
app_context
:
context
[
'
application
'
]
=
{
'
name
'
:
app_context
.
get
(
'
app_name
'
,
''
),
...
...
@@ -231,7 +245,7 @@ def track_segmentio_event(request): # pylint: disable=too-many-statements
context
[
'
course_id
'
]
=
segment_properties
[
'
course_id
'
]
with
tracker
.
get_tracker
().
context
(
'
edx.segmentio
'
,
context
):
tracker
.
emit
(
segment_event_name
,
segment_
properties
.
get
(
'
data
'
,
{})
)
tracker
.
emit
(
segment_event_name
,
segment_
event_data
)
def
_get_segmentio_event_name
(
event_properties
):
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/track/views/tests/test_segmentio.py
+
22
−
0
View file @
9d72a785
...
...
@@ -180,6 +180,28 @@ class SegmentIOTrackingTestCase(SegmentIOTrackingTestCaseBase):
segmentio
.
track_segmentio_event
(
request
)
self
.
assert_events_emitted
()
@data
(
None
,
'
a string
'
,
[
'
a
'
,
'
list
'
],
)
@expect_failure_with_message
(
segmentio
.
ERROR_INVALID_CONTEXT_FIELD_TYPE
)
def
test_invalid_context_field_type
(
self
,
invalid_value
):
sample_event_raw
=
self
.
create_segmentio_event
()
sample_event_raw
[
'
properties
'
][
'
context
'
]
=
invalid_value
self
.
post_modified_segmentio_event
(
sample_event_raw
)
@data
(
None
,
'
a string
'
,
[
'
a
'
,
'
list
'
],
)
@expect_failure_with_message
(
segmentio
.
ERROR_INVALID_DATA_FIELD_TYPE
)
def
test_invalid_data_field_type
(
self
,
invalid_value
):
sample_event_raw
=
self
.
create_segmentio_event
()
sample_event_raw
[
'
properties
'
][
'
data
'
]
=
invalid_value
self
.
post_modified_segmentio_event
(
sample_event_raw
)
@expect_failure_with_message
(
segmentio
.
ERROR_MISSING_NAME
)
def
test_missing_name
(
self
):
sample_event_raw
=
self
.
create_segmentio_event
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment