Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve and simplify logging #4194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions insights/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def __init__(self, config=None, from_phase=True, **kwargs):
if from_phase:
_init_client_config_dirs()
self.set_up_logging()
logger.debug(
"path={path}, version={version}, phase={phase}, arguments={arguments}".format(
path=client.__file__,
version=package_info.get("VERSION", "unknown"),
phase=os.getenv("INSIGHTS_PHASE", "unknown"),
arguments=" ".join(sys.argv[1:]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm are you sure logging sys.argv is a good idea? it might contain credentials, eg --username and --password currently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are not settable via CLI, only through the configuration file. Should we try to filter them anyways, in case someone enters it there by mistake?

)
)
try_auto_configuration(self.config)
self.initialize_tags()
else: # from wrapper
Expand Down
26 changes: 23 additions & 3 deletions insights/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,28 @@ def _http_request(self, url, method, log_response_text=True, **kwargs):
Returns
HTTP response object
'''
logger.log(NETWORK, "%s %s", method, url)
log_message = "{method} {url}".format(method=method, url=url)
if "data" in kwargs.keys():
log_message += " data={data}".format(data=kwargs["data"])
if "json" in kwargs.keys():
log_message += " json={json}".format(json=json.dumps(kwargs["json"]))
if "headers" in kwargs.keys():
log_message += " headers={headers}".format(headers=kwargs["headers"])
if "files" in kwargs.keys():
attachments = []
for name, content in six.iteritems(kwargs["files"]):
if isinstance(content, tuple):
attachments.append("{name}:{file}".format(name=name, file=content[0]))
m-horky marked this conversation as resolved.
Show resolved Hide resolved
else:
attachments.append(name)
log_message += " attachments={files}".format(files=",".join(attachments))
logger.log(NETWORK, log_message)
try:
res = self.session.request(url=url, method=method, timeout=self.config.http_timeout, **kwargs)
except Exception:
raise
logger.log(NETWORK, "HTTP Status: %d %s", res.status_code, res.reason)
if log_response_text or res.status_code != 200:
if log_response_text or res.status_code // 100 != 2:
m-horky marked this conversation as resolved.
Show resolved Hide resolved
logger.log(NETWORK, "HTTP Response Text: %s", res.text)
return res

Expand Down Expand Up @@ -1140,7 +1155,12 @@ def checkin(self):
url = self.inventory_url + "/hosts/checkin"
logger.debug("Sending check-in request to %s with %s" % (url, canonical_facts))
try:
response = self.post(url, headers={"Content-Type": "application/json"}, data=json.dumps(canonical_facts))
response = self.post(
url,
headers={"Content-Type": "application/json"},
data=json.dumps(canonical_facts),
log_response_text=False,
m-horky marked this conversation as resolved.
Show resolved Hide resolved
)
# Change to POST when the API is fixed.
except REQUEST_FAILED_EXCEPTIONS as exception:
_api_request_failed(exception)
Expand Down
4 changes: 0 additions & 4 deletions insights/client/phase/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import runpy

import insights
from insights.client import InsightsClient
from insights.client.config import InsightsConfig
from insights.client.constants import InsightsConstants as constants
Expand All @@ -28,9 +27,6 @@ def _f():
sys.stderr.write('ERROR: ' + str(e) + '\n')
sys.exit(constants.sig_kill_bad)

logger.debug("Core path: %s", os.path.dirname(insights.__path__[0]))
logger.debug("Core version: %s", client.version())

try:
func(client, config)
except Exception:
Expand Down
6 changes: 3 additions & 3 deletions insights/tests/client/connection/test_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_canonical_facts_request(get_proxies, post, get_canonical_facts, rm_conf
expected_headers = {"Content-Type": "application/json"}
expected_data = get_canonical_facts.return_value
post.assert_called_once_with(
expected_url, headers=expected_headers, data=dumps(expected_data)
expected_url, headers=expected_headers, data=dumps(expected_data), log_response_text=False
)


Expand Down Expand Up @@ -78,7 +78,7 @@ def test_canonical_facts_request_cleaned(get_proxies, post, get_canonical_facts,
expected_data = get_canonical_facts.return_value
expected_data = connection._clean_facts(expected_data)
post.assert_called_once_with(
expected_url, headers=expected_headers, data=dumps(expected_data)
expected_url, headers=expected_headers, data=dumps(expected_data), log_response_text=False
)


Expand Down Expand Up @@ -106,7 +106,7 @@ def test_insights_id_request(get_proxies, post, get_canonical_facts, generate_ma
expected_headers = {"Content-Type": "application/json"}
expected_data = {"insights_id": generate_machine_id.return_value}
post.assert_called_once_with(
expected_url, headers=expected_headers, data=dumps(expected_data)
expected_url, headers=expected_headers, data=dumps(expected_data), log_response_text=False
)


Expand Down
Loading