Skip to content

Commit

Permalink
Greedily load module/handler in Node.js layer (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolanos committed Dec 17, 2019
1 parent 7beafc4 commit 2a4ba32
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
23 changes: 9 additions & 14 deletions nodejs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

process.env.NEW_RELIC_APP_NAME = process.env.NEW_RELIC_APP_NAME || process.env.AWS_LAMBDA_FUNCTION_NAME
process.env.NEW_RELIC_DISTRIBUTED_TRACING_ENABLED = process.env.NEW_RELIC_DISTRIBUTED_TRACING_ENABLED || 'true'
process.env.NEW_RELIC_NO_CONFIG_FILE = process.env.NEW_RELIC_NO_CONFIG_FILE || 'true'
process.env.NEW_RELIC_LOG_ENABLED = process.env.NEW_RELIC_LOG_ENABLED || 'true'
process.env.NEW_RELIC_LOG = process.env.NEW_RELIC_LOG || 'stdout'
Expand All @@ -13,8 +14,6 @@ if (process.env.LAMBDA_TASK_ROOT && typeof process.env.NEW_RELIC_SERVERLESS_MODE
const newrelic = require('newrelic')
require('@newrelic/aws-sdk')

let wrappedHandler


function getHandler() {
let handler
Expand Down Expand Up @@ -64,6 +63,8 @@ function getHandler() {
return userHandler
}

const wrappedHandler = newrelic.setLambdaHandler(getHandler())

const ioMarks = {}

function patchIO(method, payload) {
Expand Down Expand Up @@ -113,20 +114,14 @@ const wrapPatch = () => {
}
}

function wrapHandler() {
const ctx = this
function patchedHandler() {
const args = Array.prototype.slice.call(arguments)

if (!wrappedHandler) {
if (args[1] && typeof args[1] === 'object' && !args[1].iopipe) { // process.env.IOPIPE_TOKEN &&
args[1].iopipe = wrapPatch()
}
const userHandler = getHandler()
wrappedHandler = newrelic.setLambdaHandler(
(...wrapperArgs) => userHandler.apply(ctx, wrapperArgs)
)
if (args[1] && typeof args[1] === 'object' && !args[1].iopipe) {
args[1].iopipe = wrapPatch()
}
return wrappedHandler.apply(ctx, args)

return wrappedHandler.apply(this, args)
}

module.exports.handler = wrapHandler
module.exports.handler = patchedHandler
1 change: 0 additions & 1 deletion python/newrelic_lambda_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,3 @@ def get_handler():
def handler(event, context):
context.iopipe = IOpipeNoOp()
return wrapped_handler(event, context)

0 comments on commit 2a4ba32

Please sign in to comment.