Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NR node lambda layer doesn't initialize function when using Provisioned Concurrency & fails to find handler when exported within promise #185

Closed
p-nikul opened this issue Oct 18, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@p-nikul
Copy link

p-nikul commented Oct 18, 2023

Description

When instrumenting node lambda function using the newrelic lambda layer, the layer wrapper doesn't initialize function before first request is received. This behaviour is the same even when we turn on Provisioned Concurrency, it initializes the newrelic lambda extension but doesn't initialize the function wrapped with newrelic-lambda-wrapper.handler.

Top level await Blog by AWS : Link

Related issue: We can minimize cold start with provisioned concurrency for CommonJS via initializing function first and exporting handler via Promise. This works on AWS Lambda without newrelic wrapper but fails when we use newrelic lambda wrapper

Issues:

  1. When module exports Promise which resolves handler wrapper fails to find handler. An AWS Lambda without the newrelic lambda layer works fine

  2. When using newrelic node lambda layer, it doesn't initialize function init code. This affects usage of provisioned concurrency.

Example handler code ESM

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

console.log(Date.now() + ": start init");
await sleep(3000);
console.log(Date.now() + ": end init");

export function handler() {
  console.log(Date.now() + ": executing handler");
  return Promise.resolve({
    body: 'foo',
    statusCode: 200,
  })
}

Example handler code CommonJS

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

async function init(){
    console.log(Date.now() + ": start init");
    await sleep(3000);
    console.log(Date.now() + ": end init");
}

function handler() {
    console.log(Date.now() + ": executing handler");
    return Promise.resolve({
        body: 'foo',
        statusCode: 200,
    })
}

const main = async () => {
    await init();
    return { handler };
};

module.exports = main();

Behaviour without Newrelic lambda layer

handler: dist/main.handler
Provisioned Concurrency Initialization log (5.35 seconds)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                              message                                                                               |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697506346256 | INIT_START Runtime Version: nodejs:18.v13 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:98792929c89e9ef325cbf51e2dd60dd8cfb8a23a70f14859c5609e9c9ae8a094  |
| 1697506348599 | 2023-10-17T01:32:28.599Z undefined INFO 1697506348599: start init                                                                                                  |
| 1697506351605 | 2023-10-17T01:32:31.605Z undefined INFO 1697506351604: end init                                                                                                    |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First Request with Provisioned Concurrency log (9.30 ms)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                message                                                                                 |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697506550230 | START RequestId: be00b478-faf1-4596-acd7-076346e06b15 Version: 13                                                                                                      |
| 1697506550231 | 2023-10-17T01:35:50.231Z be00b478-faf1-4596-acd7-076346e06b15 INFO 1697506550231: executing handler                                                                    |
| 1697506550239 | END RequestId: be00b478-faf1-4596-acd7-076346e06b15                                                                                                                    |
| 1697506550239 | REPORT RequestId: be00b478-faf1-4596-acd7-076346e06b15 Duration: 9.30 ms Billed Duration: 10 ms Memory Size: 128 MB Max Memory Used: 66 MB Init Duration: 5383.31 ms   |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Second Request with Provisioned Concurrency log (1.60 ms)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                message                                                                                 |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697506552906 | START RequestId: 2f49ed3d-5196-4248-91d1-33ecf1afda37 Version: 13                                                                                                      |
| 1697506552906 | 2023-10-17T01:35:52.906Z 2f49ed3d-5196-4248-91d1-33ecf1afda37 INFO 1697506552906: executing handler                                                                    |
| 1697506552907 | END RequestId: 2f49ed3d-5196-4248-91d1-33ecf1afda37                                                                                                                    |
| 1697506552907 | REPORT RequestId: 2f49ed3d-5196-4248-91d1-33ecf1afda37 Duration: 1.60 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 67 MB                              |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Steps to Reproduce (with newrelic lambda layer)

commonjs

NEW_RELIC_USE_ESM: false
NEW_RELIC_LAMBDA_HANDLER: dist/main.handler
handler: newrelic-lambda-wrapper.handler
Provisioned Concurrency Initialization log (9.30 seconds)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                              message                                                                               |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697508381686 | INIT_START Runtime Version: nodejs:18.v13 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:98792929c89e9ef325cbf51e2dd60dd8cfb8a23a70f14859c5609e9c9ae8a094  |
| 1697508382729 | [NR_EXT] New Relic Lambda Extension starting up                                                                                                                    |
| 1697508382790 | [NR_EXT] Extension telemetry processing disabled                                                                                                                   |
| 1697508382790 | [NR_EXT] Starting no-op mode, no telemetry will be sent                                                                                                            |
| 1697508390989 | EXTENSION Name: newrelic-lambda-extension State: Ready Events: [INVOKE, SHUTDOWN]                                                                                  |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Request Failed (400.31 ms)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                                                                                                                                                                                                                message                                                                                                                                                                                                                                                                                 |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697508559990 | START RequestId: 1fc4140f-e86e-47c8-96ec-97668f46df17 Version: 15                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| 1697508560250 | 2023-10-17T02:09:20.250Z 1fc4140f-e86e-47c8-96ec-97668f46df17 INFO 1697508560250: start init                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| 1697508560252 | 2023-10-17T02:09:20.252Z 1fc4140f-e86e-47c8-96ec-97668f46df17 ERROR Invoke Error  {"errorType":"Error","errorMessage":"Handler 'handler' missing on module 'dist/main'","stack":["Error: Handler 'handler' missing on module 'dist/main'","    at validateHandlerDefinition (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:93:11)","    at importHandler (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:110:3)","    at async Runtime.patchESModuleHandler [as handler] (/opt/nodejs/node_modules/newrelic-lambda-wrapper/index.js:127:23)"]}  |
| 1697508560389 | END RequestId: 1fc4140f-e86e-47c8-96ec-97668f46df17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| 1697508560389 | REPORT RequestId: 1fc4140f-e86e-47c8-96ec-97668f46df17 Duration: 400.31 ms Billed Duration: 401 ms Memory Size: 128 MB Max Memory Used: 99 MB Init Duration: 9302.64 ms                                                                                                                                                                                                                                                                                                                                                                                                |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ESM

NEW_RELIC_USE_ESM: true
NEW_RELIC_LAMBDA_HANDLER: dist/main.handler
handler: newrelic-lambda-wrapper.handler
Provisioned Concurrency initialization log (9.45 seconds)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697505473563 | INIT_START Runtime Version: nodejs:18.v13 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:98792929c89e9ef325cbf51e2dd60dd8cfb8a23a70f14859c5609e9c9ae8a094                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 1697505474637 | [NR_EXT] New Relic Lambda Extension starting up                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| 1697505474716 | [NR_EXT] Extension telemetry processing disabled                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| 1697505474716 | [NR_EXT] Starting no-op mode, no telemetry will be sent                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| 1697505483017 | EXTENSION Name: newrelic-lambda-extension State: Ready Events: [INVOKE, SHUTDOWN]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First Request with Provisioned Concurrency log (3.68 seconds)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697505690855 | START RequestId: 254e62f4-1827-46c3-88b6-0873e72fb372 Version: 12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 1697505691255 | 2023-10-17T01:21:31.255Z 254e62f4-1827-46c3-88b6-0873e72fb372 INFO 1697505691255: start init                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| 1697505694297 | 2023-10-17T01:21:34.297Z 254e62f4-1827-46c3-88b6-0873e72fb372 INFO 1697505694297: end init                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| 1697505694304 | 2023-10-17T01:21:34.304Z 254e62f4-1827-46c3-88b6-0873e72fb372 INFO 1697505694304: executing handler                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| 1697505694515 | [1,"NR_LAMBDA_MONITORING","H4sIAAAAAAAAA+1XW3PaRhT+Kx09g7T3i94cx67dSVJPcJIHT4ZZpDWoEZKyWtkmHv57jyRwTQLEl6TNQ80YlmX1ne9c95zbYG69SY03QXwbGFcEcfsem="]  |
| 1697505694535 | END RequestId: 254e62f4-1827-46c3-88b6-0873e72fb372                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| 1697505694535 | REPORT RequestId: 254e62f4-1827-46c3-88b6-0873e72fb372 Duration: 3680.39 ms Billed Duration: 3681 ms Memory Size: 128 MB Max Memory Used: 100 MB Init Duration: 9453.07 ms                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Second Request log (7 ms)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697505859238 | START RequestId: d8076f0c-ed80-4983-abc2-27dd0965659a Version: 12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| 1697505859240 | 2023-10-17T01:24:19.240Z d8076f0c-ed80-4983-abc2-27dd0965659a INFO 1697505859240: executing handler                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| 1697505859294 | [1,"NR_LAMBDA_MONITORING","H4sIAAAAAAAAA+1X23LiOBD9lS0/g5Bk3ey3TC6bVGWmUgvZeUhNUcIW4B1js7JMwqT4920ZyMKEy+YyMy"]                                                                                                                                                                                                                                                                  |
| 1697505859313 | END RequestId: d8076f0c-ed80-4983-abc2-27dd0965659a                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| 1697505859313 | REPORT RequestId: d8076f0c-ed80-4983-abc2-27dd0965659a Duration: 74.82 ms Billed Duration: 75 ms Memory Size: 128 MB Max Memory Used: 100 MB                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Expected behaviour with newrelic lambda layer

See below comment #185 (comment)

Relevant Logs / Console output

See below comment #185 (comment)

@p-nikul p-nikul added the bug Something isn't working label Oct 18, 2023
@p-nikul
Copy link
Author

p-nikul commented Oct 18, 2023

Expected behaviour with newrelic lambda layer

We expect newrelic lambda layer to also initialize function as well similar to AWS does when not using the layer. When first/second request is received there should not be any time spent on initialization if provisioned concurrency is enabled.

ESM & CommonJS

NEW_RELIC_USE_ESM: true / false
NEW_RELIC_LAMBDA_HANDLER: dist/main.handler
handler: newrelic-lambda-wrapper.handler
Expected Provisioned Concurrency Initialization log (6.43 seconds)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                              message                                                                               |
|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697506821075 | INIT_START Runtime Version: nodejs:18.v13 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:98792929c89e9ef325cbf51e2dd60dd8cfb8a23a70f14859c5609e9c9ae8a094  |
| 1697506822128 | [NR_EXT] New Relic Lambda Extension starting up                                                                                                                    |
| 1697506822188 | [NR_EXT] Extension telemetry processing disabled                                                                                                                   |
| 1697506822188 | [NR_EXT] Starting no-op mode, no telemetry will be sent                                                                                                            |
| 1697506824468 | 2023-10-17T01:40:24.468Z undefined INFO 1697506824468: start init                                                                                                  |
| 1697506827473 | 2023-10-17T01:40:27.473Z undefined INFO 1697506827472: end init                                                                                                    |
| 1697506827508 | EXTENSION Name: newrelic-lambda-extension State: Ready Events: [INVOKE, SHUTDOWN]                                                                                  |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expected First Request with Provisioned Concurrency log (35.30 ms)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                 message                                                                                 |
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697507059577 | START RequestId: 7b2656de-ffc7-4aea-86c0-aa9773383255 Version: 14                                                                                                       |
| 1697507059578 | 2023-10-17T01:44:19.578Z 7b2656de-ffc7-4aea-86c0-aa9773383255 INFO 1697507059578: executing handler                                                                     |
| 1697507059590 | [1,"NR_LAMBDA_MONITORING","H4sIAAAAAAAAA+1XW3PaRhT+Kx09g7T3i94cx67dSVJPcJIHT4ZZpDWoEZKyWtkmHv57jyRwTQLEl6TNQ80YlmX1ne9c95zbYG69SY03QXwbGFcEcfsem="]                     |
| 1697507059612 | END RequestId: 7b2656de-ffc7-4aea-86c0-aa9773383255                                                                                                                     |
| 1697507059612 | REPORT RequestId: 7b2656de-ffc7-4aea-86c0-aa9773383255 Duration: 35.30 ms Billed Duration: 36 ms Memory Size: 128 MB Max Memory Used: 80 MB Init Duration: 6453.34 ms   |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expected Second Request with Provisioned Concurrency log (68.57 ms)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                                 message                                                                                 |
|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1697507121784 | START RequestId: 90e64430-d4db-49b3-a05e-8a196e81d4f1 Version: 14                                                                                                       |
| 1697507121813 | 2023-10-17T01:45:21.813Z 90e64430-d4db-49b3-a05e-8a196e81d4f1 INFO 1697507121813: executing handler                                                                     |
| 1697507121825 | [1,"NR_LAMBDA_MONITORING","H4sIAAAAAAAAA+1X23LiOBD9lS0/g5Bk3ey3TC6bVGWmUgvZeUhNUcIW4B1js7JMwqT4920ZyMKEy+YyMy"]                     |
| 1697507121853 | END RequestId: 90e64430-d4db-49b3-a05e-8a196e81d4f1                                                                                                                     |
| 1697507121853 | REPORT RequestId: 90e64430-d4db-49b3-a05e-8a196e81d4f1 Duration: 68.57 ms Billed Duration: 69 ms Memory Size: 128 MB Max Memory Used: 81 MB                             |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@mrickard
Copy link
Member

Thank you for the report and the repro case, @p-nikul . We'll investigate.

@p-nikul
Copy link
Author

p-nikul commented Mar 7, 2024

Hi, is there any update on this ask ? Without this support using Provision concurrency with node.js newrelic lambda layers is not useful at all. It is blocking us to fully utilize newrelic in Production.

@chaudharysaket chaudharysaket self-assigned this May 23, 2024
@aavinash-nr
Copy link

aavinash-nr commented Oct 23, 2024

Hello @p-nikul,

We've thoroughly reviewed the bug and the AWS documentation link you provided. After examining the sample code, we successfully identified the issue. Interestingly, when we bypassed the main function and directly used the top-level handler as per the document, it worked as expected. We also observed a notable optimization: when running the code for the second or third time, the total duration is significantly reduced since the init function only executes during the first run.

Please find the code snippet below for your reference:

  • Environment variables
NEW_RELIC_USE_ESM: false
NEW_RELIC_LAMBDA_HANDLER: index.handler
  • Code snippet
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

async function init() {
    console.log(`${Date.now()}: start init`);
    await sleep(3000);
    console.log(`${Date.now()}: end init`);
}

const initPromise = init();

async function handler(event, context) {
    
    await initPromise;

    console.log(`${Date.now()}: executing handler`);

    return {
        body: 'foo',
        statusCode: 200,
    };
}
module.exports.handler = handler;

Could you please confirm whether it works for you as well? If you encounter any issues or have feedback on the implementation, we'd love to hear about it.

@workato-integration workato-integration bot changed the title NR node lambda layer doesn't initialize function when using Provisioned Concurrency & fails to find handler when exported within promise Timeouts with arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS20X:39 Oct 23, 2024
@workato-integration workato-integration bot changed the title Timeouts with arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS20X:39 Timeouts with arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS20X:39 Oct 23, 2024
@workato-integration workato-integration bot changed the title Timeouts with arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS20X:39 NR node lambda layer doesn't initialize function when using Provisioned Concurrency & fails to find handler when exported within promise Oct 23, 2024
@workato-integration workato-integration bot changed the title NR node lambda layer doesn't initialize function when using Provisioned Concurrency & fails to find handler when exported within promise NR node lambda layer doesn't initialize function when using Provisioned Concurrency & fails to find handler when exported within promise Oct 23, 2024
@p-nikul
Copy link
Author

p-nikul commented Oct 28, 2024

@aavinash-nr Main difference when using suggested code by you init function is not called until handler is triggered. So when using provisioned concurrency init function is not initialized until first event trigger handler.

End goal here is able to initialize function without calling/triggering handler when using Provisioned concurrency and reduce cold start time.

Logs with layer and provisioned concurrency with suggested code
Timestamp Message
2024-10-28T20:54:50.304Z INIT_START Runtime Version: nodejs:20.v42 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:5d2526a9c5e8e7de181a82a9c5f3486d1d552ff4e4f3d15ae645652f6acf2e68
2024-10-28T20:54:51.422Z [NR_EXT] New Relic Lambda Extension starting up
2024-10-28T20:54:51.422Z [NR_EXT] Initializing version 2.3.14 of the New Relic Lambda Extension...
2024-10-28T20:54:51.463Z [NR_EXT] Extension telemetry processing disabled
2024-10-28T20:54:51.463Z [NR_EXT] Starting no-op mode, no telemetry will be sent
2024-10-28T20:54:53.203Z 2024-10-28T20:54:53.203Z undefined INFO 1730148893203: start init
2024-10-28T20:54:53.283Z EXTENSION Name: newrelic-lambda-extension State: Ready Events: [INVOKE, SHUTDOWN]

With below code snippet (without NR layer) I'm able to initialize my function without needing of handler getting triggered/called with AWS lambda

const main = async () => {
    await init();
    return { handler };
};

module.exports = main();

@aavinash-nr
Copy link

aavinash-nr commented Nov 7, 2024

Hello @p-nikul, To reduce first run duration you could utilize layerless approach.

@workato-integration
Copy link

Work has been completed on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants