Skip to content

Commit

Permalink
feat: add ip_chain.safest_client_ip to emitted XForwardedForMiddlewar…
Browse files Browse the repository at this point in the history
…e metrics (#33720)

This commit adds the result of get_safest_client_ip() to the emitted metrics of
XForwardedForMiddleware.
  • Loading branch information
pshiu authored Nov 15, 2023
1 parent dcd6786 commit 94658f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion openedx/core/lib/x_forwarded_for/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def process_request(self, request):
# Only used to support ip.legacy switch.
request.META['ORIGINAL_REMOTE_ADDR'] = request.META['REMOTE_ADDR']

safest_client_ip = ip.get_safest_client_ip(request)

try:
# Give some observability into IP chain length and composition. Useful
# for monitoring in case of unexpected network config changes, etc.
Expand All @@ -66,6 +68,8 @@ def process_request(self, request):
external_chain = ip.get_all_client_ips(request)
set_custom_attribute('ip_chain.external.count', len(external_chain))
set_custom_attribute('ip_chain.external.types', '-'.join(_ip_type(s) for s in external_chain))

set_custom_attribute('ip_chain.safest_client_ip', safest_client_ip)
except BaseException:
warnings.warn('Error while computing IP chain metrics')

Expand Down Expand Up @@ -107,4 +111,4 @@ def process_request(self, request):
if legacy_ip.USE_LEGACY_IP.is_enabled():
request.META['REMOTE_ADDR'] = legacy_ip.get_legacy_ip(request)
else:
request.META['REMOTE_ADDR'] = ip.get_safest_client_ip(request)
request.META['REMOTE_ADDR'] = safest_client_ip
19 changes: 14 additions & 5 deletions openedx/core/lib/x_forwarded_for/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,20 @@ def test_overrides(self, add_meta, expected_meta_include):

@ddt.unpack
@ddt.data(
(None, '127.0.0.1', 1, 'priv'),
('1.2.3.4', '1.2.3.4, 127.0.0.1', 2, 'pub-priv'),
('XXXXXXXX, 1.2.3.4, 5.5.5.5', 'XXXXXXXX, 1.2.3.4, 5.5.5.5, 127.0.0.1', 4, 'unknown-pub-pub-priv'),
(None, '127.0.0.1', 1, 'priv', '127.0.0.1'),
('1.2.3.4', '1.2.3.4, 127.0.0.1', 2, 'pub-priv', '1.2.3.4'),
('XXXXXXXX, 1.2.3.4, 5.5.5.5', 'XXXXXXXX, 1.2.3.4, 5.5.5.5, 127.0.0.1', 4, 'unknown-pub-pub-priv', '5.5.5.5'),
)
@patch("openedx.core.lib.x_forwarded_for.middleware.set_custom_attribute")
def test_xff_metrics(self, xff, expected_raw, expected_count, expected_types, mock_set_custom_attribute):
def test_xff_metrics(
self,
xff,
expected_raw,
expected_count,
expected_types,
expected_safest_client_ip,
mock_set_custom_attribute,
):
request = RequestFactory().get('/somewhere')
if xff is not None:
request.META['HTTP_X_FORWARDED_FOR'] = xff
Expand All @@ -76,4 +84,5 @@ def test_xff_metrics(self, xff, expected_raw, expected_count, expected_types, mo
call('ip_chain.raw', expected_raw),
call('ip_chain.count', expected_count),
call('ip_chain.types', expected_types),
])
call('ip_chain.safest_client_ip', expected_safest_client_ip),
], any_order=True)

0 comments on commit 94658f5

Please sign in to comment.