From a268a185af601b26ce4e23a8848d4112e8dc949f Mon Sep 17 00:00:00 2001 From: Taimoor Ahmed Date: Mon, 29 Jul 2024 17:08:26 +0500 Subject: [PATCH] feat: Add Forum v2 request logging send request to forum v2 alongside v1 validate both responses and log info in case of diff --- .../comment_client/settings.py | 4 ++++ .../comment_client/utils.py | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/settings.py b/openedx/core/djangoapps/django_comment_common/comment_client/settings.py index 075aba03ede0..e595904424c9 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/settings.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/settings.py @@ -7,3 +7,7 @@ SERVICE_HOST = 'http://localhost:4567' PREFIX = SERVICE_HOST + '/api/v1' + +# V2 url support for differential logging +SERVICE_HOST_V2 = 'http://localhost:8000' +PREFIX_V2 = SERVICE_HOST_V2 + '/api/v2' diff --git a/openedx/core/djangoapps/django_comment_common/comment_client/utils.py b/openedx/core/djangoapps/django_comment_common/comment_client/utils.py index a67cdbdbc483..1bbd27f5a3b7 100644 --- a/openedx/core/djangoapps/django_comment_common/comment_client/utils.py +++ b/openedx/core/djangoapps/django_comment_common/comment_client/utils.py @@ -8,7 +8,7 @@ import requests from django.utils.translation import get_language -from .settings import SERVICE_HOST as COMMENTS_SERVICE +from .settings import PREFIX, PREFIX_V2, SERVICE_HOST as COMMENTS_SERVICE log = logging.getLogger(__name__) @@ -30,6 +30,10 @@ def extract(dic, keys): return strip_none({k: dic.get(k) for k in keys}) +def _get_forum_v2_url(url): + return url.replace(PREFIX, PREFIX_V2) + + def perform_request(method, url, data_or_params=None, raw=False, metric_action=None, metric_tags=None, paged_results=False): # To avoid dependency conflict @@ -71,6 +75,19 @@ def perform_request(method, url, data_or_params=None, raw=False, timeout=config.connection_timeout ) + if method == 'get': + forum_v2_url = _get_forum_v2_url(url) + response_v2 = requests.request( + method, + forum_v2_url, + data=data, + params=params, + headers=headers, + timeout=config.connection_timeout + ) + if response_v2 != response: + log.error(f"Forum v2 diff for endpoint {url} with params={params}. Expected: {response}. Got: {response_v2}.") + metric_tags.append(f'status_code:{response.status_code}') status_code = int(response.status_code) if status_code > 200: