Koa
v2+Lightweight async middleware that wraps your Koa app.
Uses `await next()` to measure the full request/response cycle. Compatible with Koa 2+.
Install
bash
npm install auto-api-observeQuick Start
The minimal setup — import, configure, and you're observing.
koa-quick-start.js
const Koa = require('koa');
const Router = require('@koa/router');
const { koaObservability } = require('auto-api-observe');
const app = new Koa();
app.use(koaObservability({ apiKey: process.env.APILENS_KEY }));
const router = new Router();
router.get('/users', ctx => { ctx.body = { users: [] }; });
app.use(router.routes());
app.listen(3000);Full Example
All available options with custom fields and real-world usage.
koa-full.js
const Koa = require('koa');
const Router = require('@koa/router');
const { koaObservability, addField } = require('auto-api-observe');
const app = new Koa();
app.use(koaObservability({
apiKey: process.env.APILENS_KEY,
slowThreshold: 600,
skipRoutes: ['/health'],
tags: { service: 'koa-api' },
}));
const router = new Router();
router.get('/products/:id', async ctx => {
addField('productId', ctx.params.id);
ctx.body = await Product.findById(ctx.params.id);
});
app.use(router.routes());
app.listen(3000);What's Tracked Automatically
- ctx.method, ctx.status, latency
- Route path from ctx.path
- Client IP from x-forwarded-for or ctx.socket
- DB and outbound calls
Options Reference
| Option | Type | Default |
|---|---|---|
| apiKey | string | — |
| slowThreshold | number | 1000 |
| skipRoutes | string[] | [] |
| tags | Record | {} |
Custom Fields
Attach any business context to the current request via AsyncLocalStorage — scoped per request, works the same on every framework.
const { addField } = require('auto-api-observe');
// In any route handler:
addField('userId', req.user.id);
addField('plan', req.user.plan);
addField('country', req.headers['cf-ipcountry']);
// Sensitive keys are auto-redacted before shipping:
addField('authorization', token); // → "[REDACTED]"
addField('password', pass); // → "[REDACTED]"Ready to observe your Koa app?
Sign up free — get your API key in under 30 seconds. No credit card required.
Get your free API key