Skip to content

Commit

Permalink
CASMCMS-8997: Compact response bodies to single line before logging them
Browse files Browse the repository at this point in the history
  • Loading branch information
mharding-hpe committed May 16, 2024
1 parent bc8c94f commit acfe142
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
- Added more checks to avoid operating on empty lists
- Compact response bodies to single line before logging them

## [2.17.6] - 2024-04-19
### Fixed
Expand Down
14 changes: 13 additions & 1 deletion src/bos/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# MIT License
#
# (C) Copyright 2023 Hewlett Packard Enterprise Development LP
# (C) Copyright 2022-2024 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -59,6 +59,7 @@ def duration_to_timedelta(timestamp: str):
seconds = timeval * seconds_table[durationval]
return datetime.timedelta(seconds=seconds)


class TimeoutHTTPAdapter(HTTPAdapter):
"""
An HTTP Adapter that allows a session level timeout for both read and connect attributes. This prevents interruption
Expand Down Expand Up @@ -95,3 +96,14 @@ def requests_retry_session(retries=10, backoff_factor=0.5,
# Mounting to only http will not work!
session.mount("%s://" % protocol, adapter)
return session


def compact_response_text(response_text: str) -> str:
"""
Often JSON is "pretty printed" in response text, which is undesirable for our logging.
This function transforms the response text into a single line, stripping leading and trailing whitespace from each line,
and then returns it.
"""
if response_text:
return ' '.join([ line.strip() for line in response_text.split('\n') ])
return str(response_text)
4 changes: 2 additions & 2 deletions src/bos/operators/utils/clients/bss.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import logging
import json

from bos.common.utils import requests_retry_session, PROTOCOL
from bos.common.utils import compact_response_text, requests_retry_session, PROTOCOL

LOGGER = logging.getLogger(__name__)
SERVICE_NAME = 'cray-bss'
Expand Down Expand Up @@ -78,7 +78,7 @@ def set_bss(node_set, kernel_params, kernel, initrd, session=None):
try:
resp = session.put(url, data=json.dumps(payload), verify=False)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", resp.status_code,
resp.reason, resp.text)
resp.reason, compact_response_text(resp.text))
resp.raise_for_status()
return resp
except HTTPError as err:
Expand Down
6 changes: 3 additions & 3 deletions src/bos/operators/utils/clients/cfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import logging
from requests.exceptions import HTTPError, ConnectionError

from bos.common.utils import requests_retry_session, PROTOCOL
from bos.common.utils import compact_response_text, requests_retry_session, PROTOCOL

SERVICE_NAME = 'cray-cfs-api'
BASE_ENDPOINT = "%s://%s/v3" % (PROTOCOL, SERVICE_NAME)
Expand All @@ -51,7 +51,7 @@ def get_components(session=None, **params):
LOGGER.debug("GET %s with params=%s", COMPONENTS_ENDPOINT, params)
response = session.get(COMPONENTS_ENDPOINT, params=params)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
try:
response.raise_for_status()
except HTTPError as err:
Expand All @@ -75,7 +75,7 @@ def patch_components(data, session=None):
LOGGER.debug("PATCH %s with body=%s", COMPONENTS_ENDPOINT, data)
response = session.patch(COMPONENTS_ENDPOINT, json=data)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
try:
response.raise_for_status()
except HTTPError as err:
Expand Down
8 changes: 4 additions & 4 deletions src/bos/operators/utils/clients/hsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from requests.exceptions import HTTPError, ConnectionError
from urllib3.exceptions import MaxRetryError

from bos.common.utils import requests_retry_session, PROTOCOL
from bos.common.utils import compact_response_text, requests_retry_session, PROTOCOL

SERVICE_NAME = 'cray-smd'
BASE_ENDPOINT = "%s://%s/hsm/v2/" % (PROTOCOL, SERVICE_NAME)
Expand Down Expand Up @@ -62,7 +62,7 @@ def read_all_node_xnames():
LOGGER.error("Unable to contact HSM service: %s", ce)
raise HWStateManagerException(ce) from ce
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
try:
response.raise_for_status()
except (HTTPError, MaxRetryError) as hpe:
Expand Down Expand Up @@ -131,7 +131,7 @@ def get_components(node_list, enabled=None) -> dict[str,list[dict]]:
LOGGER.debug("POST %s with body=%s", ENDPOINT, payload)
response = session.post(ENDPOINT, json=payload)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
response.raise_for_status()
components = json.loads(response.text)
except (ConnectionError, MaxRetryError) as e:
Expand Down Expand Up @@ -227,7 +227,7 @@ def get(self, path, params=None):
LOGGER.debug("HSM Inventory: GET %s with params=%s", url, params)
response = self._session.get(url, params=params, verify=VERIFY)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
response.raise_for_status()
except HTTPError as err:
LOGGER.error("Failed to get '{}': {}".format(url, err))
Expand Down
6 changes: 3 additions & 3 deletions src/bos/operators/utils/clients/pcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import json
from collections import defaultdict

from bos.common.utils import requests_retry_session, PROTOCOL
from bos.common.utils import compact_response_text, requests_retry_session, PROTOCOL

SERVICE_NAME = 'cray-power-control'
POWER_CONTROL_VERSION = 'v1'
Expand Down Expand Up @@ -103,7 +103,7 @@ def _power_status(xname=None, power_state_filter=None, management_state_filter=N
LOGGER.debug("POST %s with body=%s", POWER_STATUS_ENDPOINT, params)
response = session.post(POWER_STATUS_ENDPOINT, json=params)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
try:
response.raise_for_status()
if not response.ok:
Expand Down Expand Up @@ -217,7 +217,7 @@ def _transition_create(xnames, operation, task_deadline_minutes=None, deputy_key
LOGGER.debug("POST %s with body=%s", TRANSITION_ENDPOINT, params)
response = session.post(TRANSITION_ENDPOINT, json=params)
LOGGER.debug("Response status code=%d, reason=%s, body=%s", response.status_code,
response.reason, response.text)
response.reason, compact_response_text(response.text))
try:
response.raise_for_status()
if not response.ok:
Expand Down

0 comments on commit acfe142

Please sign in to comment.