Commit dbc6b5c0 by Calen Pennington

Minimize time spent with an open file when logging json timing messages

parent 217572ee
...@@ -88,22 +88,34 @@ class JsonFormatter(Formatter): ...@@ -88,22 +88,34 @@ class JsonFormatter(Formatter):
# N.B. This is intended to provide a consistent interface and message format # N.B. This is intended to provide a consistent interface and message format
# across all of Open edX tooling, so it deliberately eschews standard # across all of Open edX tooling, so it deliberately eschews standard
# python logging infrastructure. # python logging infrastructure.
if ANSIBLE_TIMER_LOG is not None: if ANSIBLE_TIMER_LOG is None:
log_path = playbook_timestamp.start.strftime(ANSIBLE_TIMER_LOG) return
messages = []
for name, timestamp in results:
messages.append({
'task': name,
'playbook': playbook_name,
'started_at': timestamp.start.isoformat(),
'ended_at': timestamp.end.isoformat(),
'duration': timestamp.duration.total_seconds(),
})
messages.append({
'playbook': playbook_name,
'started_at': playbook_timestamp.start.isoformat(),
'ended_at': playbook_timestamp.end.isoformat(),
'duration': playbook_timestamp.duration.total_seconds(),
})
log_path = playbook_timestamp.start.strftime(ANSIBLE_TIMER_LOG)
try:
if not exists(dirname(log_path)): if not exists(dirname(log_path)):
os.makedirs(dirname(log_path)) os.makedirs(dirname(log_path))
with open(log_path, 'a') as outfile: with open(log_path, 'a') as outfile:
for name, timestamp in results: for log_message in messages:
log_message = {
'task': name,
'playbook': playbook_name,
'started_at': timestamp.start.isoformat(),
'ended_at': timestamp.end.isoformat(),
'duration': timestamp.duration.total_seconds(),
}
json.dump( json.dump(
log_message, log_message,
outfile, outfile,
...@@ -111,21 +123,8 @@ class JsonFormatter(Formatter): ...@@ -111,21 +123,8 @@ class JsonFormatter(Formatter):
sort_keys=True, sort_keys=True,
) )
outfile.write('\n') outfile.write('\n')
except OSError:
log_message = { LOGGER.exception("Unable to write json timing log messages")
'playbook': playbook_name,
'started_at': playbook_timestamp.start.isoformat(),
'ended_at': playbook_timestamp.end.isoformat(),
'duration': playbook_timestamp.duration.total_seconds(),
}
json.dump(
log_message,
outfile,
separators=(',', ':'),
sort_keys=True,
)
outfile.write('\n')
class LoggingFormatter(Formatter): class LoggingFormatter(Formatter):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment