Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat: override status code with thrown error message (#592)
Browse files Browse the repository at this point in the history
* feat: parse relaxed json and extracted from message

* fix: removed json5 direct dependency

* feat: ff env var & generalized matching
  • Loading branch information
ronnathaniel committed Jun 8, 2022
1 parent 4164da3 commit 6480b8c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ Advanced options can be configured as a parameter to the init() method or as env
|batchSize |EPSAGON_BATCH_SIZE |Integer|`5 ` |The traces' batch size, when batch mode is activated |
|decodeHTTP |EPSAGON_DECODE_HTTP |Boolean|`true` |Whether to decode and decompress HTTP responses into the payload |
|httpErrorStatusCode|EPSAGON_HTTP_ERR_CODE |Integer|`400` |The minimum number of an HTTP response status code to treat as an error |
|- |EPSAGON_ALLOW_ERR_MESSAGE_STATUS|Boolean|`true` |Whether to override `http.status_code` with a Status Code found in a raised Error's Message|
|- |EPSAGON_PROPAGATE_LAMBDA_ID|Boolean|`false` |Insert Lambda request ID into the response payload |
|- |DISABLE_EPSAGON_PATCH |Boolean|`false` |Disable the library patching (instrumentation) |
|isConsolePatched |EPSAGON_PATCH_CONSOLE |Boolean|`false` |Enable console module patching (instrumentation) for labelling |
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const config = {
sendTimeout: (Number(process.env.EPSAGON_SEND_TIMEOUT_SEC) || DEFAULT_TIMEOUT_SEC) * 1000.0,
decodeHTTP: (process.env.EPSAGON_DECODE_HTTP || 'TRUE').toUpperCase() === 'TRUE',
disableHttpResponseBodyCapture: (process.env.EPSAGON_DISABLE_HTTP_RESPONSE || '').toUpperCase() === 'TRUE',
allowErrMessageStatus: (process.env.EPSAGON_ALLOW_ERR_MESSAGE_STATUS || '').toUpperCase() === 'TRUE',
loggingTracingEnabled: (process.env.EPSAGON_LOGGING_TRACING_ENABLED || (!utils.isLambdaEnv).toString()).toUpperCase() === 'TRUE',
sendBatch: (process.env.EPSAGON_SEND_BATCH || (!utils.isLambdaEnv).toString()).toUpperCase() === 'TRUE',
batchSize: (Number(process.env.EPSAGON_BATCH_SIZE) || consts.DEFAULT_BATCH_SIZE),
Expand Down
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ function distinct(arr) {
return [...new Set(arr)];
}

/**
* Attempt to extract an HTTP Status Code from a String.
* Matches and returns only 3-digit numbers in range 2XX-5XX.
* @param {String} message A String containing a Status Code.
* @return {Number} extracted Status Code. If not found -> 0, the only falsy Number.
*/
function extractStatusCode(message) {
// matches a [Non-Digit], [2xx - 5xx], and a [Non-Digit]. globally & multiline.
// \D ([2-5]\d{2}) \D /g m
const statusCodeMatch = /\D([2-5]\d{2})\D/gm;
const statusCodes = message
.match(statusCodeMatch)
.map(m => m.replace(statusCodeMatch, '$1'));
if (statusCodes && statusCodes.length) {
return Number(statusCodes[0]);
}
return 0;
}

module.exports.createTimestampFromTime = createTimestampFromTime;
module.exports.createTimestamp = createTimestamp;
Expand All @@ -220,3 +238,4 @@ module.exports.isLambdaEnv = isLambdaEnv;
module.exports.getValueIfExist = getValueIfExist;
module.exports.truncateMessage = truncateMessage;
module.exports.distinct = distinct;
module.exports.extractStatusCode = extractStatusCode;
12 changes: 10 additions & 2 deletions src/wrappers/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function baseLambdaWrapper(
return Promise.resolve();
}
callbackCalled = true;
const config = getConfig();
if (error) {
// not catching false here, but that seems OK
let reportedError = error;
Expand All @@ -178,7 +179,15 @@ function baseLambdaWrapper(
message: errorMessage,
};
}

// Override status code with Error message
if (config.allowErrMessageStatus) {
const errStatusCode = utils.extractStatusCode(reportedError.message);
if (errStatusCode) {
eventInterface.addToMetadata(runner, {
status_code: errStatusCode,
});
}
}
// Setting this error only if there is no existing error already
if (!runner.getException()) {
utils.debugLog('Setting exception from handleUserExecutionDone');
Expand All @@ -190,7 +199,6 @@ function baseLambdaWrapper(
if (statusCode) {
eventInterface.addToMetadata(runner, { status_code: statusCode });
}
const config = getConfig();
if (error === null && !config.metadataOnly && config.addReturnValue) {
try {
// Taken from AWS Lambda runtime
Expand Down

0 comments on commit 6480b8c

Please sign in to comment.