From a4fff886a6c77cf414f244f1133ba8f5d1b7aba2 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 7 Sep 2018 13:34:22 -0400 Subject: [PATCH] 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/ --- workers/logrotateworker.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/workers/logrotateworker.py b/workers/logrotateworker.py index ddf4f9ad0..e8486403c 100644 --- a/workers/logrotateworker.py +++ b/workers/logrotateworker.py @@ -106,13 +106,24 @@ class LogRotateWorker(Worker): def log_dict(log): """ Pretty prints a LogEntry in 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))} + 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': metadata_json, + } def main():