export async function asyncLocalStorageMiddleware()

in src/async_local_storage.ts [14:40]


export async function asyncLocalStorageMiddleware(
  req: Request,
  res: Response,
  next: NextFunction,
) {
  if (
    semver.lt(process.versions.node, requiredNodeJsVersionForLogExecutionID)
  ) {
    // Skip for unsupported Node.js version.
    next();
    return;
  }
  if (!asyncLocalStorage) {
    const asyncHooks = await import('node:async_hooks');
    asyncLocalStorage = new asyncHooks.AsyncLocalStorage();
  }

  asyncLocalStorage.run(
    {
      executionId: req.executionId,
      spanId: req.spanId,
    },
    () => {
      next();
    },
  );
}