DataDog / datadog-lambda-js

The Datadog AWS Lambda Library for Node
Apache License 2.0
109 stars 35 forks source link

"datadog:handler not initialized" if metrics is sent after Lambda response with callbackWaitsForEmptyEventLoop = false #433

Closed exoego closed 9 months ago

exoego commented 10 months ago

Expected Behavior

A metric hello_from_queue is sent.

Actual Behavior

A metric hello_from_queue is not sent for most of invocations. Instead, error los are written in a CloudWatch log stream:

{
    "status": "error",
    "message": "datadog:handler not initialized"
}

Steps to Reproduce the Problem

1. Create a Node.js 18.x Lambda with the attached code ```javascript "use strict"; const { sendDistributionMetric } = require("datadog-lambda-js"); const queue = []; exports.handler = (event, context, callback) => { context.callbackWaitsForEmptyEventLoop = false; // This metric succeeds, but... sendDistributionMetric("hello", 1); queue.push(new Promise((resolve, reject) => { setTimeout(() => { try { // This metric rarely succeeds sendDistributionMetric("hello_from_queue", 1); resolve("ok"); } catch (e) { reject(e); } }, 100 * Math.random()); })); callback(null, { statusCode: 200, body: JSON.stringify({ message: "Hello from Lambda!", }), }); }; ```
  1. Attache DD Lambda Layer
  2. Configure handler and env var as well
  3. Test-run the Lambda 10 times
  4. See the metrics. hello metric will be recorded 10, but hello_from_queue is less than 10 (even 0 or 1).

This is the minimal repro. Actually, I am using https://github.com/vendia/serverless-express in a "CALLBACK" resolution mode. I want to submit a DD metrics even after Lambda callback is executed.

Specifications


Stacktrace

N/A

exoego commented 9 months ago

I've avoid relying on callbackWaitsForEmptyEventLoop = false/