Skip to content

Commit

Permalink
feat: provide a lambda context with logging/error handling included
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Aug 13, 2019
1 parent eefda3e commit 72fe409
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
},
"dependencies": {
"aws-sdk": "^2.508.0"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.31",
"@types/pino": "^5.8.8",
"pino": "^5.13.2"
}
}
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { Const } from './const';
export { Aws } from './aws/index';

export { LambdaFunction } from './lambda';
36 changes: 36 additions & 0 deletions packages/shared/src/lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Logger } from './log';
import { Context, Callback } from 'aws-lambda';
import * as pino from 'pino';

export class LambdaFunction {
/**
* Wrap a lambda function to provide extra functionality
*
* - Log metadata about the call on every request
* - Catch errors and log them before exiting
*/
static wrap<T, K>(fn: (event: T, context: Context, logger: pino.Logger) => Promise<K>) {
return async (event: T, context: Context, callback: Callback<K>) => {
const startTime = Date.now();

const lambda = {
name: process.env['AWS_LAMBDA_FUNCTION_NAME'],
memory: process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'],
version: process.env['AWS_LAMBDA_FUNCTION_VERSION'],
region: process.env['AWS_REGION'],
};

const logger = Logger.child({});
logger.info({ lambda }, 'LambdaStart');

try {
const res = await fn(event, context, logger);
logger.info({ lambda, duration: Date.now() - startTime, status: 200 }, 'LambdaDone');
callback(null, res);
} catch (error) {
logger.info({ lambda, duration: Date.now() - startTime, status: 500, error: error }, 'LambdaError');
callback(error);
}
};
}
}
3 changes: 3 additions & 0 deletions packages/shared/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as pino from 'pino';

export const Logger = pino({ level: 'debug' });
59 changes: 59 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1360,11 +1360,26 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.1.tgz#3b5c3a26393c19b400844ac422bd0f631a94d69d"
integrity sha512-aK9jxMypeSrhiYofWWBf/T7O+KwaiAHzM4sveCdWPn71lzUSMimRnKzhXDKfKwV1kWoBo2P1aGgaIYGLf9/ljw==

"@types/pino@^5.8.8":
version "5.8.8"
resolved "https://registry.yarnpkg.com/@types/pino/-/pino-5.8.8.tgz#930eb30f1f1eb76b97792647477aab59e5852574"
integrity sha512-SxAdLtEpPkVUdnI3iCUjyFC7WVLFiebyzqypvkuJVo7dyK6BPuQ4lTOuZRKpbEdgG386l5HgfqpymL3oWLOUXg==
dependencies:
"@types/node" "*"
"@types/sonic-boom" "*"

"@types/semver@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.0.1.tgz#a984b405c702fa5a7ec6abc56b37f2ba35ef5af6"
integrity sha512-ffCdcrEE5h8DqVxinQjo+2d1q+FV5z7iNtPofw3JsrltSoSVlOGaW0rY8XxtO9XukdTn8TaCGWmk2VFGhI70mg==

"@types/sonic-boom@*":
version "0.6.2"
resolved "https://registry.yarnpkg.com/@types/sonic-boom/-/sonic-boom-0.6.2.tgz#5f6c7bf6b4a0994f9339d778da6a7adcc3d37080"
integrity sha512-vP9Sn1tuz/BTh8L1o776Cbzr+WH4dZGmRXOjQ5L+IVQx40hUmvOS2wfIkqUsID1vL62tThWdlXWIqijwewu3mw==
dependencies:
"@types/node" "*"

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down Expand Up @@ -2945,6 +2960,16 @@ fast-levenshtein@~2.0.4:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

fast-redact@^1.4.4:
version "1.5.0"
resolved "https://registry.yarnpkg.com/fast-redact/-/fast-redact-1.5.0.tgz#302892f566750c4f5eec7b830bfc9bc473484034"
integrity sha512-Afo61CgUjkzdvOKDHn08qnZ0kwck38AOGcMlvSGzvJbIab6soAP5rdoQayecGCDsD69AiF9vJBXyq31eoEO2tQ==

fast-safe-stringify@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz#04b26106cc56681f51a044cfc0d76cf0008ac2c2"
integrity sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg==

fb-watchman@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58"
Expand Down Expand Up @@ -3020,6 +3045,11 @@ flat-cache@^2.0.1:
rimraf "2.6.3"
write "1.0.3"

flatstr@^1.0.12, flatstr@^1.0.9:
version "1.0.12"
resolved "https://registry.yarnpkg.com/flatstr/-/flatstr-1.0.12.tgz#c2ba6a08173edbb6c9640e3055b95e287ceb5931"
integrity sha512-4zPxDyhCyiN2wIAtSLI6gc82/EjqZc1onI4Mz/l0pWrAlsSfYH/2ZIcU+e3oA2wDwbzIWNKwa23F8rh6+DRWkw==

flatted@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08"
Expand Down Expand Up @@ -5524,6 +5554,23 @@ pinkie@^2.0.0:
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
integrity sha1-clVrgM+g1IqXToDnckjoDtT3+HA=

pino-std-serializers@^2.3.0:
version "2.4.2"
resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-2.4.2.tgz#cb5e3e58c358b26f88969d7e619ae54bdfcc1ae1"
integrity sha512-WaL504dO8eGs+vrK+j4BuQQq6GLKeCCcHaMB2ItygzVURcL1CycwNEUHTD/lHFHs/NL5qAz2UKrjYWXKSf4aMQ==

pino@^5.13.2:
version "5.13.2"
resolved "https://registry.yarnpkg.com/pino/-/pino-5.13.2.tgz#773416c9764634276e7b2ae021357679ff7b5634"
integrity sha512-WwOSCy36/gWhinsqWqAnuwIi2WtcH+jvoyeLm3bjUALIrzWIst0AovQjK4jVvSN2l64KFPfi3gd2fjsTovjdLQ==
dependencies:
fast-redact "^1.4.4"
fast-safe-stringify "^2.0.6"
flatstr "^1.0.9"
pino-std-serializers "^2.3.0"
quick-format-unescaped "^3.0.2"
sonic-boom "^0.7.5"

pirates@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
Expand Down Expand Up @@ -5695,6 +5742,11 @@ querystring@0.2.0:
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=

quick-format-unescaped@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/quick-format-unescaped/-/quick-format-unescaped-3.0.2.tgz#0137e94d8fb37ffeb70040535111c378e75396fb"
integrity sha512-FXTaCkwvpIlkdKeGDNgcq07SXWS383noQUuZjvdE1QcTt+eLuqof6/BDiEPqB59FWLie/l91+HtlJSw7iCViSA==

quick-lru@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"
Expand Down Expand Up @@ -6240,6 +6292,13 @@ socks@~2.3.2:
ip "^1.1.5"
smart-buffer "4.0.2"

sonic-boom@^0.7.5:
version "0.7.5"
resolved "https://registry.yarnpkg.com/sonic-boom/-/sonic-boom-0.7.5.tgz#b383d92cdaaa8e66d1f77bdec71b49806d01b5f1"
integrity sha512-1pKrnAV6RfvntPnarY71tpthFTM3pWZWWQdghZY8ARjtDPGzG/inxqSuRwQY/7V1woUjfyxPb437zn4p5phgnQ==
dependencies:
flatstr "^1.0.12"

sort-keys@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/sort-keys/-/sort-keys-2.0.0.tgz#658535584861ec97d730d6cf41822e1f56684128"
Expand Down

0 comments on commit 72fe409

Please sign in to comment.