Skip to content

Commit 2a4ba32

Browse files
authored
Greedily load module/handler in Node.js layer (#3)
1 parent 7beafc4 commit 2a4ba32

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

nodejs/index.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
process.env.NEW_RELIC_APP_NAME = process.env.NEW_RELIC_APP_NAME || process.env.AWS_LAMBDA_FUNCTION_NAME
4+
process.env.NEW_RELIC_DISTRIBUTED_TRACING_ENABLED = process.env.NEW_RELIC_DISTRIBUTED_TRACING_ENABLED || 'true'
45
process.env.NEW_RELIC_NO_CONFIG_FILE = process.env.NEW_RELIC_NO_CONFIG_FILE || 'true'
56
process.env.NEW_RELIC_LOG_ENABLED = process.env.NEW_RELIC_LOG_ENABLED || 'true'
67
process.env.NEW_RELIC_LOG = process.env.NEW_RELIC_LOG || 'stdout'
@@ -13,8 +14,6 @@ if (process.env.LAMBDA_TASK_ROOT && typeof process.env.NEW_RELIC_SERVERLESS_MODE
1314
const newrelic = require('newrelic')
1415
require('@newrelic/aws-sdk')
1516

16-
let wrappedHandler
17-
1817

1918
function getHandler() {
2019
let handler
@@ -64,6 +63,8 @@ function getHandler() {
6463
return userHandler
6564
}
6665

66+
const wrappedHandler = newrelic.setLambdaHandler(getHandler())
67+
6768
const ioMarks = {}
6869

6970
function patchIO(method, payload) {
@@ -113,20 +114,14 @@ const wrapPatch = () => {
113114
}
114115
}
115116

116-
function wrapHandler() {
117-
const ctx = this
117+
function patchedHandler() {
118118
const args = Array.prototype.slice.call(arguments)
119119

120-
if (!wrappedHandler) {
121-
if (args[1] && typeof args[1] === 'object' && !args[1].iopipe) { // process.env.IOPIPE_TOKEN &&
122-
args[1].iopipe = wrapPatch()
123-
}
124-
const userHandler = getHandler()
125-
wrappedHandler = newrelic.setLambdaHandler(
126-
(...wrapperArgs) => userHandler.apply(ctx, wrapperArgs)
127-
)
120+
if (args[1] && typeof args[1] === 'object' && !args[1].iopipe) {
121+
args[1].iopipe = wrapPatch()
128122
}
129-
return wrappedHandler.apply(ctx, args)
123+
124+
return wrappedHandler.apply(this, args)
130125
}
131126

132-
module.exports.handler = wrapHandler
127+
module.exports.handler = patchedHandler

python/newrelic_lambda_wrapper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,3 @@ def get_handler():
8989
def handler(event, context):
9090
context.iopipe = IOpipeNoOp()
9191
return wrapped_handler(event, context)
92-

0 commit comments

Comments
 (0)