11'use strict'
22
33process . 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'
45process . env . NEW_RELIC_NO_CONFIG_FILE = process . env . NEW_RELIC_NO_CONFIG_FILE || 'true'
56process . env . NEW_RELIC_LOG_ENABLED = process . env . NEW_RELIC_LOG_ENABLED || 'true'
67process . 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
1314const newrelic = require ( 'newrelic' )
1415require ( '@newrelic/aws-sdk' )
1516
16- let wrappedHandler
17-
1817
1918function getHandler ( ) {
2019 let handler
@@ -64,6 +63,8 @@ function getHandler() {
6463 return userHandler
6564}
6665
66+ const wrappedHandler = newrelic . setLambdaHandler ( getHandler ( ) )
67+
6768const ioMarks = { }
6869
6970function 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
0 commit comments