Official AWS X-Ray middleware for Koa 2.x that records incoming/outgoing requests and responses. Version 3.12.0 is latest stable; released with the aws-xray-sdk-node monorepo on a monthly cadence. Key differentiator: deep AWS service integration with automatic segment/subsegment management via cls-hooked. Supports ESM and CJS; ships TypeScript types. Requires aws-xray-sdk-core and Koa 2.x. Offers both automatic (default) and manual tracing modes. Minimal overhead but can cause context issues if middleware order is wrong.
npm install aws-xray-sdk-koa2Verified import paths — ran on the pinned version, not inferred.
Shows ESM imports, middleware setup, and segment retrieval in automatic mode with Koa and @koa/router.
Place app.use(openSegment(...)) immediately before your router or route middleware.
Update to >=3.8.0, preferably 3.12.0.
Use manual mode with ctx.segment if you encounter context loss.
Use AWSXRay.getSegment()?.close() or let the SDK handle it via automatic mode.
Ensure aws-xray-sdk-core is installed and imported: import AWSXRay from 'aws-xray-sdk-core'.
Add app.use(openSegment('name')) before routes; use manual mode if async context is lost.Provide a name string: app.use(openSegment('myApp')).