Skip to content

Commit

Permalink
[Reporting] Sanitize 409 error log message (#42495)
Browse files Browse the repository at this point in the history
* [Reporting] Sanitize 409 error log message

* [Reporting] Sanitize 409 error log message

* feedback changes
  • Loading branch information
tsullivan authored Aug 3, 2019
1 parent 9fbb95f commit c9e2a22
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function formatJobObject(job) {
};
}

const MAX_PARTIAL_ERROR_LENGTH = 1000; // 1000 of beginning, 1000 of end
const ERROR_PARTIAL_SEPARATOR = '...';
const MAX_ERROR_LENGTH = (MAX_PARTIAL_ERROR_LENGTH * 2) + ERROR_PARTIAL_SEPARATOR.length;

function getLogger(opts, id, logLevel) {
return (msg, err) => {
const logger = opts.logger || function () {};
Expand All @@ -29,7 +33,26 @@ function getLogger(opts, id, logLevel) {
const tags = ['worker', logLevel];

if (err) {
logger(`${message}: ${err.stack ? err.stack : err }`, tags);
// The error message string could be very long if it contains the request
// body of a request that was too large for Elasticsearch.
// This takes a partial version of the error message without scanning
// every character of the string, which would block Node.
const errString = `${message}: ${err.stack ? err.stack : err}`;
const errLength = errString.length;
const subStr = String.prototype.substring.bind(errString);
if (errLength > MAX_ERROR_LENGTH) {
const partialError =
subStr(0, MAX_PARTIAL_ERROR_LENGTH) +
ERROR_PARTIAL_SEPARATOR +
subStr(errLength - MAX_PARTIAL_ERROR_LENGTH);

logger(partialError, tags);
logger(
`A partial version of the entire error message was logged. ` +
`The entire error message length is: ${errLength} characters.`,
tags
);
}
return;
}

Expand Down

0 comments on commit c9e2a22

Please sign in to comment.