Handle the case where the log metadata JSON cannot be parsed

This can happen if the JSON overflowed the text field in the table, for example

Fixes https://sentry.io/coreos/backend-production/issues/670957592/
This commit is contained in:
Joseph Schorr 2018-09-07 13:34:22 -04:00
parent f9414c256d
commit a4fff886a6

View file

@ -106,13 +106,24 @@ class LogRotateWorker(Worker):
def log_dict(log):
""" Pretty prints a LogEntry in JSON. """
return {'kind_id': log.kind_id,
try:
metadata_json = json.loads(str(log.metadata_json))
except ValueError:
logger.exception('Could not parse metadata JSON for log entry %s', log.id)
metadata_json = {'__raw': log.metadata_json}
except TypeError:
logger.exception('Could not parse metadata JSON for log entry %s', log.id)
metadata_json = {'__raw': log.metadata_json}
return {
'kind_id': log.kind_id,
'account_id': log.account_id,
'performer_id': log.performer_id,
'repository_id': log.repository_id,
'datetime': str(log.datetime),
'ip': str(log.ip),
'metadata_json': json.loads(str(log.metadata_json))}
'metadata_json': metadata_json,
}
def main():