DataDog/datadog-lambda-js

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

exoego opened this issue · 1 comments

exoego commented

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
"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

  • Datadog Lambda Layer version: 99 and 97
  • Node version: 18.x

Stacktrace

N/A

exoego commented

I've avoid relying on callbackWaitsForEmptyEventLoop = false/