Node.js SDK
The Node.js SDK in serverless functions
A serverless instance can freeze or end right after it answers, so a timer that would flush at the next minute never fires. The aggregator counts in memory and sends a minute’s bucket once it has closed; for a short invocation that is too late. Flush before the function ends.
export async function handler(event) { metrics.increment('orders.created'); const result = await work(event); await metrics.flush({ final: true }); // sends the open minute too return result;}flush({ final: true }) sends every bucket, the open one included, and never rejects. Each invocation’s delta is sent under its own key, so it never repeats an earlier body’s key.
| Platform | Where to flush |
|---|---|
| AWS Lambda | await metrics.flush({ final: true }) before returning |
| Vercel | waitUntil(metrics.flush({ final: true })) from @vercel/functions, or after() in a Next.js route handler: see Next.js |
| Cloudflare Workers | ctx.waitUntil(metrics.flush({ final: true })) (documented and untested: Workers are not in the SDK’s test matrix) |
For logs, call await logger.flush() the same way: a Logger queues lines and ships them in batches, and its flush() resolves to { linesSent, notSent, reports }.
A Lambda function that only forwards log lines to log ingest, without the SDK, is the AWS Lambda recipe.