Fastify
This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas.
@bugsee/fastify adds a request context, an http.server transaction and error reporting to a Fastify 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 Fastify as its target; see Create your app.
Install
- npm
- pnpm
- yarn
- bun
npm install @bugsee/fastify
pnpm add @bugsee/fastify
yarn add @bugsee/fastify
bun add @bugsee/fastify
The adapter declares no peer dependency. It only relies on the shape of your Fastify instance.
Set up
Call launch() at the top of your server entry, then pass the root Fastify instance to setupFastify:
import { launch, setupFastify } from '@bugsee/fastify';
import Fastify from 'fastify';
launch('<your_app_token>');
const app = Fastify();
setupFastify(app);
// ... your routes ...
app.listen({ port: 3000 }).catch((error) => {
app.log.error(error);
process.exit(1);
});
What it does
setupFastify installs lifecycle hooks:
| Hook | What it does |
|---|---|
onRequest | Opens a request context, continues an inbound traceparent and starts an http.server transaction. |
onError | Reports an unhandled route error, with the request context merged in. |
onResponse | Finishes the transaction with the route pattern, such as /users/:id, and the status. |
onRequestAbort | Finishes the transaction as cancelled when the client aborts. |
Fastify hooks are encapsulated per scope. Call setupFastify on the root instance and the hooks cover it and its child plugins. The order relative to your routes does not matter.
By default an error with no status, or with a status of 500 or higher, is reported, and a 4xx is skipped.
Options
Pass options as the second argument of setupFastify.
| Option | Type | Default | Description |
|---|---|---|---|
user | (req) => 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. |
req is a structural view of the Fastify request: method, url, headers and routeOptions.
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/fastify 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