Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
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
0a5f2553
Unverified
Commit
0a5f2553
authored
4 years ago
by
Robert Raposa
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #26089 from edx/robrap/fix-cookie-monitoring-middleware
fix cookie monitoring middleware
parents
f90d495e
8b4ea05d
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
openedx/core/lib/request_utils.py
+49
-42
49 additions, 42 deletions
openedx/core/lib/request_utils.py
openedx/core/lib/tests/test_request_utils.py
+41
-2
41 additions, 2 deletions
openedx/core/lib/tests/test_request_utils.py
with
90 additions
and
44 deletions
openedx/core/lib/request_utils.py
+
49
−
42
View file @
0a5f2553
...
@@ -153,54 +153,61 @@ class CookieMonitoringMiddleware(MiddlewareMixin):
...
@@ -153,54 +153,61 @@ class CookieMonitoringMiddleware(MiddlewareMixin):
# Add or update the size for this group.
# Add or update the size for this group.
cookie_groups_to_size
[
grouping_name
]
=
cookie_groups_to_size
.
get
(
grouping_name
,
0
)
+
cookie_size
cookie_groups_to_size
[
grouping_name
]
=
cookie_groups_to_size
.
get
(
grouping_name
,
0
)
+
cookie_size
max_cookie_name
=
max
(
cookie_names_to_size
,
key
=
lambda
name
:
cookie_names_to_size
[
name
])
if
cookie_names_to_size
:
max_cookie_size
=
cookie_names_to_size
[
max_cookie_name
]
self
.
set_custom_attributes_for_top_n
(
cookie_names_to_size
,
top_n_cookies_captured
,
attribute_prefix
=
'
cookies
'
,
)
max_cookie_name
=
max
(
cookie_names_to_size
,
key
=
lambda
name
:
cookie_names_to_size
[
name
])
max_cookie_size
=
cookie_names_to_size
[
max_cookie_name
]
set_custom_attribute
(
'
cookies.max.name
'
,
max_cookie_name
)
set_custom_attribute
(
'
cookies.max.size
'
,
max_cookie_size
)
if
cookie_groups_to_size
:
self
.
set_custom_attributes_for_top_n
(
cookie_groups_to_size
,
top_n_cookie_groups_captured
,
attribute_prefix
=
'
cookies.group
'
,
)
max_group_cookie_name
=
max
(
cookie_groups_to_size
,
key
=
lambda
name
:
cookie_groups_to_size
[
name
])
max_group_cookie_size
=
cookie_groups_to_size
[
max_group_cookie_name
]
# If a single cookies is bigger than any group of cookies, we want max_group... to reflect that.
# Treating an individual cookie as a group of 1 for calculating the max.
if
max_group_cookie_size
<
max_cookie_size
:
max_group_cookie_name
=
max_cookie_name
max_group_cookie_size
=
max_cookie_size
set_custom_attribute
(
'
cookies.max.group.name
'
,
max_group_cookie_name
)
set_custom_attribute
(
'
cookies.max.group.size
'
,
max_group_cookie_size
)
max_group_cookie_name
=
max
(
cookie_groups_to_size
,
key
=
lambda
name
:
cookie_groups_to_size
[
name
])
total_cookie_size
=
sum
(
cookie_names_to_size
.
values
())
max_group_cookie_size
=
cookie_groups_to_size
[
max_group_cookie_name
]
set_custom_attribute
(
'
cookies_total_size
'
,
total_cookie_size
)
log
.
debug
(
u
'
cookies_total_size = %d
'
,
total_cookie_size
)
# If a single cookies is bigger than any group of cookies, we want max_group... to reflect that.
def
set_custom_attributes_for_top_n
(
self
,
names_to_size
,
top_n_captured
,
attribute_prefix
):
# Treating an individual cookie as a group of 1 for calculating the max.
"""
if
max_group_cookie_size
<
max_cookie_size
:
Sets custom metric for the top N biggest cookies or cookie groups.
max_group_cookie_name
=
max_cookie_name
max_group_cookie_size
=
max_cookie_size
# Log only the top N biggest cookies.
Arguments:
names_to_size: Dict of sizes keyed by cookie name or cookie group name
top_n_captured: Number of largest sizes to monitor.
attribute_prefix: Prefix (cookies|cookies.group) to use in the custom attribute name.
"""
top_n_cookies
=
sorted
(
top_n_cookies
=
sorted
(
cookie_
names_to_size
,
names_to_size
,
key
=
lambda
x
:
cookie_
names_to_size
[
x
],
key
=
lambda
x
:
names_to_size
[
x
],
reverse
=
True
,
reverse
=
True
,
)[:
top_n_
cookies_
captured
]
)[:
top_n_captured
]
for
index
,
name
in
enumerate
(
top_n_cookies
,
start
=
1
):
for
index
,
name
in
enumerate
(
top_n_cookies
,
start
=
1
):
size
=
cookie_names_to_size
[
name
]
size
=
names_to_size
[
name
]
name_attribute
=
'
cookies.{}.name
'
.
format
(
index
)
name_attribute
=
'
{}.{}.name
'
.
format
(
attribute_prefix
,
index
)
size_attribute
=
'
cookies.{}.size
'
.
format
(
index
)
size_attribute
=
'
{}.{}.size
'
.
format
(
attribute_prefix
,
index
)
set_custom_attribute
(
name_attribute
,
name
)
set_custom_attribute
(
size_attribute
,
size
)
log
.
debug
(
u
'
%s = %d
'
,
name
,
size
)
# Log only the top N biggest groups.
top_n_cookie_groups
=
sorted
(
cookie_groups_to_size
,
key
=
lambda
x
:
cookie_groups_to_size
[
x
],
reverse
=
True
,
)[:
top_n_cookie_groups_captured
]
for
index
,
name
in
enumerate
(
top_n_cookie_groups
,
start
=
1
):
size
=
cookie_groups_to_size
[
name
]
name_attribute
=
'
cookies.group.{}.name
'
.
format
(
index
)
size_attribute
=
'
cookies.group.{}.size
'
.
format
(
index
)
set_custom_attribute
(
name_attribute
,
name
)
set_custom_attribute
(
name_attribute
,
name
)
set_custom_attribute
(
size_attribute
,
size
)
set_custom_attribute
(
size_attribute
,
size
)
log
.
debug
(
u
'
%s = %d
'
,
name
,
size
)
log
.
debug
(
'
%s = %d
'
,
name
,
size
)
set_custom_attribute
(
'
cookies.max.name
'
,
max_cookie_name
)
set_custom_attribute
(
'
cookies.max.size
'
,
max_cookie_size
)
set_custom_attribute
(
'
cookies.max.group.name
'
,
max_group_cookie_name
)
set_custom_attribute
(
'
cookies.max.group.size
'
,
max_group_cookie_size
)
total_cookie_size
=
sum
(
cookie_names_to_size
.
values
())
set_custom_attribute
(
'
cookies_total_size
'
,
total_cookie_size
)
log
.
debug
(
u
'
cookies_total_size = %d
'
,
total_cookie_size
)
This diff is collapsed.
Click to expand it.
openedx/core/lib/tests/test_request_utils.py
+
41
−
2
View file @
0a5f2553
...
@@ -131,7 +131,7 @@ class RequestUtilTestCase(unittest.TestCase):
...
@@ -131,7 +131,7 @@ class RequestUtilTestCase(unittest.TestCase):
call
(
'
cookies.max.group.name
'
,
'
a
'
),
call
(
'
cookies.max.group.name
'
,
'
a
'
),
call
(
'
cookies.max.group.size
'
,
100
),
call
(
'
cookies.max.group.size
'
,
100
),
call
(
'
cookies_total_size
'
,
189
),
call
(
'
cookies_total_size
'
,
189
),
])
]
,
any_order
=
True
)
@patch
(
"
openedx.core.lib.request_utils.CAPTURE_COOKIE_SIZES
"
)
@patch
(
"
openedx.core.lib.request_utils.CAPTURE_COOKIE_SIZES
"
)
@patch
(
"
openedx.core.lib.request_utils.set_custom_attribute
"
)
@patch
(
"
openedx.core.lib.request_utils.set_custom_attribute
"
)
...
@@ -163,4 +163,43 @@ class RequestUtilTestCase(unittest.TestCase):
...
@@ -163,4 +163,43 @@ class RequestUtilTestCase(unittest.TestCase):
call
(
'
cookies.max.group.name
'
,
'
b
'
),
call
(
'
cookies.max.group.name
'
,
'
b
'
),
call
(
'
cookies.max.group.size
'
,
35
),
call
(
'
cookies.max.group.size
'
,
35
),
call
(
'
cookies_total_size
'
,
45
)
call
(
'
cookies_total_size
'
,
45
)
])
],
any_order
=
True
)
@patch
(
"
openedx.core.lib.request_utils.CAPTURE_COOKIE_SIZES
"
)
@patch
(
"
openedx.core.lib.request_utils.set_custom_attribute
"
)
def
test_cookie_monitoring_no_cookies
(
self
,
mock_set_custom_attribute
,
mock_capture_cookie_sizes
):
mock_capture_cookie_sizes
.
is_enabled
.
return_value
=
True
middleware
=
CookieMonitoringMiddleware
()
mock_request
=
Mock
()
mock_request
.
COOKIES
=
{}
middleware
.
process_request
(
mock_request
)
mock_set_custom_attribute
.
assert_has_calls
([
call
(
'
cookies_total_size
'
,
0
)],
any_order
=
True
)
@patch
(
"
openedx.core.lib.request_utils.CAPTURE_COOKIE_SIZES
"
)
@patch
(
"
openedx.core.lib.request_utils.set_custom_attribute
"
)
def
test_cookie_monitoring_no_groups
(
self
,
mock_set_custom_attribute
,
mock_capture_cookie_sizes
):
mock_capture_cookie_sizes
.
is_enabled
.
return_value
=
True
middleware
=
CookieMonitoringMiddleware
()
mock_request
=
Mock
()
mock_request
.
COOKIES
=
{
"
a
"
:
"
.
"
*
10
,
"
b
"
:
"
.
"
*
15
,
}
middleware
.
process_request
(
mock_request
)
mock_set_custom_attribute
.
assert_has_calls
([
call
(
'
cookies.max.name
'
,
'
b
'
),
call
(
'
cookies.max.size
'
,
15
),
call
(
'
cookies.1.name
'
,
'
b
'
),
call
(
'
cookies.1.size
'
,
15
),
call
(
'
cookies.2.name
'
,
'
a
'
),
call
(
'
cookies.2.size
'
,
10
),
call
(
'
cookies_total_size
'
,
25
),
],
any_order
=
True
)
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