Skip to content

Commit 1e1c2b6

Browse files
authored
[Logging] Truncate extras values (#4759)
It truncates the extras value for logging, as it can be used to log full exception stacktracing, blowing our log size threshold. --------- Signed-off-by: Javan Lacerda <javanlacerda@google.com>
1 parent 7f4a1e0 commit 1e1c2b6

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/clusterfuzz/_internal/metrics/logs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def get_logging_config_dict(name):
182182

183183
def truncate(msg, limit):
184184
"""We need to truncate the message in the middle if it gets too long."""
185-
if len(msg) <= limit:
185+
if not isinstance(msg, str) or len(msg) <= limit:
186186
return msg
187187

188188
half = limit // 2
@@ -225,7 +225,12 @@ def format(self, record: logging.LogRecord) -> str:
225225
entry['task_payload'] = initial_payload
226226

227227
entry['location'] = getattr(record, 'location', {'error': True})
228-
entry['extras'] = getattr(record, 'extras', {})
228+
# This is needed to truncate the extras value, as it can be used
229+
# to log exceptions stacktrace.
230+
entry['extras'] = {
231+
k: truncate(v, STACKDRIVER_LOG_MESSAGE_LIMIT)
232+
for k, v in getattr(record, 'extras', {}).items()
233+
}
229234
update_entry_with_exc(entry, record.exc_info)
230235

231236
if not entry['extras']:

src/clusterfuzz/_internal/tests/core/metrics/logs_test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def get_record(self):
190190
levelname='INFO',
191191
exc_info='exc_info',
192192
created=10,
193+
# This extras field is needed because the call
194+
# getattr(record, 'extras', {}) returns None and not the
195+
# default for the case of running against a mock
196+
extras={},
193197
location={
194198
'path': 'path',
195199
'line': 123,
@@ -208,16 +212,16 @@ def test_format_record(self):
208212
'message': 'log message',
209213
'created': '1970-01-01T00:00:10Z',
210214
'docker_image': '',
215+
'extras': {
216+
'a': 1
217+
},
211218
'severity': 'INFO',
212219
'bot_name': 'linux-bot',
213220
'task_payload': 'fuzz fuzzer1 job1',
214221
'fuzz_target': 'fuzz_target1',
215222
'name': 'logger_name',
216223
'pid': 1337,
217224
'release': 'prod',
218-
'extras': {
219-
'a': 1,
220-
},
221225
'location': {
222226
'path': 'path',
223227
'line': 123,
@@ -231,7 +235,6 @@ def test_format_record(self):
231235
def test_no_extras(self):
232236
"""Test format record with no extras."""
233237
record = self.get_record()
234-
record.extras = None
235238
self.assertEqual({
236239
'message': 'log message',
237240
'created': '1970-01-01T00:00:10Z',
@@ -255,7 +258,6 @@ def test_worker_bot_name(self):
255258
"""Test format record with a worker bot name."""
256259
os.environ['WORKER_BOT_NAME'] = 'worker'
257260
record = self.get_record()
258-
record.extras = None
259261

260262
self.assertEqual({
261263
'docker_image': '',

0 commit comments

Comments
 (0)