Koa
This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.
@bugsee/koa adds a request context, an http.server transaction and error reporting to a Koa app on Node.js. Read the backend overview first for how context and trace correlation work.
You need an app token. Create a JavaScript application in Bugsee and choose Koa as its target; see Create your app.
Install
- npm
- pnpm
- yarn
- bun
npm install @bugsee/koa
pnpm add @bugsee/koa
yarn add @bugsee/koa
bun add @bugsee/koa
The adapter declares no peer dependency. It only relies on the shape of your Koa app.
Set up
Call launch() at the top of your server entry, then pass the app to setupKoa. Register it before your other middleware so that it wraps the whole chain:
import { launch, setupKoa } from '@bugsee/koa';
import Koa from 'koa';
launch('<your_app_token>');
const app = new Koa();
setupKoa(app);
// ... your middleware and routes ...
app.listen(3000);
What it does
setupKoa installs one middleware. It opens a request context, continues an inbound traceparent, starts an http.server transaction, and reports an error thrown downstream before re-throwing it untouched, so Koa's own onerror still formats the response.
By default a plain throw or a 5xx is reported, and a 4xx such as ctx.throw(404) is skipped. With @koa/router, the transaction is named after the matched route.
Options
Pass options as the second argument of setupKoa.
| Option | Type | Default | Description |
|---|---|---|---|
user | (ctx) => string | undefined | off | The end-user identity for reports raised during the request. |
shouldReport | (error) => boolean | 5xx or no status | Decides whether a thrown error is reported. |
onError | (error) => void | none | Receives a failure of the adapter itself. It never reaches the request. |
ctx is a structural view of the Koa context: method, path, url, status and headers.
Launch options
The adapter does not change the launch options. These are the ones that matter most on a server:
| Option | Default | Description |
|---|---|---|
instrumentIncomingRequests | true | Automatic per-request context and http.server transaction. See the backend overview. |
captureLocalVariables | false | Attach local variables to the stack frames of a reported error. See Diagnostics. |
captureSourceContext | false | Attach the source lines around each stack frame. See Diagnostics. |
detectHangs | true | Report event-loop hangs. See Hangs. |
profiling | false | Attach a rolling CPU profile to reports. See Diagnostics. |
@bugsee/koa installs @bugsee/bugsee for you and re-exports everything from its Node entry, so launch and the SDK types come from this one package. You do not install or import @bugsee/bugsee separately. On Bun, see Running on Bun.
Related pages
- Backend overview — request context, trace correlation and incoming-request instrumentation
- Issue detection — what else is detected on a server
- Performance — transactions and spans