Repository navigation
docs(javascript): JavaScript SDK documentation (beta) #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krassx
wants to merge
21
commits into
main
Choose a base branch
from
docs/javascript-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7ebe6aa
docs(javascript): add the JavaScript SDK documentation (beta)
krassx ae1207c
docs(javascript): make Installation an index of per-package guides; m…
krassx efca4d4
docs(javascript): describe how a JavaScript app is created (type, the…
krassx fb2c510
docs(javascript): install framework adapters on their own; drop the @…
krassx 43f5e83
docs(javascript): link the installation tables to the packages on npm
krassx a06a8ca
docs(javascript): link packages to npm with an external-link icon, no…
krassx 2cfe48f
docs(javascript): list Bun next to Node.js in the installation prereq…
krassx 7444133
docs(javascript): drop the Deno notes
krassx 9a4e478
docs(javascript): give the component name plugins their own page unde…
krassx d06a461
docs(javascript): address review threads on Bun, relaunch, privacy, s…
krassx 9e15750
docs(javascript): qualify Bun transaction claims, soften benchmark fi…
krassx c90744f
docs(javascript): mark the captureLocalVariables overhead as an appro…
krassx 4586fc3
docs(javascript): CORS note for trace headers, tsup POSIX note, host-…
krassx ab78c06
docs(javascript): keep the browser launch promise out of top-level aw…
krassx 2956b43
docs(javascript): one launch idiom (shared module with top-level awai…
krassx 0e2c6d4
docs(javascript): make the two browser launch snippets alternatives
krassx 5ce24ef
docs(javascript): align Hono runtime wording, scope the version troub…
krassx 6d9d8de
docs(javascript): endBlackout always runs, shared bugsee module is im…
krassx 00c5111
docs(javascript): avoid top-level await in server examples, align the…
krassx 13a24d0
docs(javascript): align the browser app version and build sources
krassx 6e04e98
docs(javascript): correct the host-boundary target regexes, add the C…
krassx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| --- | ||
| title: "Elysia" | ||
| description: "Report errors and trace requests in an Elysia app on Bun or Node.js with the Bugsee JavaScript SDK: per-request context, http.server transactions and error capture." | ||
| sidebar_position: 8 | ||
| slug: "/sdk/javascript/backend/elysia" | ||
| --- | ||
|
|
||
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
|
|
||
| :::warning[Beta] | ||
| This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas. | ||
| ::: | ||
|
|
||
| `@bugsee/elysia` adds a request context, error reporting and, where the performance extension is present (Node.js, not `@bugsee/bun`), an `http.server` transaction to an Elysia app. It runs on Bun, and on Node.js through the app's fetch handler. Read the [backend overview](/sdk/javascript/backend/overview) first for how context and trace correlation work. | ||
|
|
||
| You need an app token. Create a JavaScript application in Bugsee and choose **Elysia** as its target; see [Create your app](/sdk/javascript/installation#create-your-app). | ||
|
|
||
| ## Install | ||
|
|
||
| <Tabs groupId="package-manager"> | ||
| <TabItem value="npm" label="npm"> | ||
|
|
||
| ```bash | ||
| npm install @bugsee/elysia | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="pnpm" label="pnpm"> | ||
|
|
||
| ```bash | ||
| pnpm add @bugsee/elysia | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="yarn" label="yarn"> | ||
|
|
||
| ```bash | ||
| yarn add @bugsee/elysia | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="bun" label="bun"> | ||
|
|
||
| ```bash | ||
| bun add @bugsee/elysia | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| On Bun, also install `@bugsee/bun` and import `launch` from it, as described in [Running on Bun](/sdk/javascript/backend/overview#running-on-bun). In that setup the request context and error reporting work, but there is no `http.server` transaction. | ||
|
|
||
| The adapter declares no peer dependency. It only relies on the shape of your Elysia app. On Node.js it needs Node.js 18 or newer. | ||
|
|
||
| ## Set up | ||
|
|
||
| Call `launch()` at the top of your server entry, then pass the app to `setupElysia`: | ||
|
|
||
| ```ts title="server.ts" | ||
| import { launch, setupElysia } from '@bugsee/elysia'; | ||
| import { Elysia } from 'elysia'; | ||
|
|
||
| launch('<your_app_token>'); | ||
|
|
||
| const app = new Elysia(); | ||
| setupElysia(app); | ||
|
|
||
| // ... your routes ... | ||
| ``` | ||
|
|
||
| Hooks are scoped to the Elysia instance they are added to, so register `setupElysia` on the instance that owns your routes. | ||
|
|
||
| If your Elysia app type does not unify with the adapter's structural type, cast it: | ||
|
|
||
| ```ts | ||
| import { type ElysiaAppLike, setupElysia } from '@bugsee/elysia'; | ||
|
|
||
| setupElysia(app as unknown as ElysiaAppLike); | ||
| ``` | ||
|
|
||
| ## Node.js and Bun | ||
|
|
||
| On Bun, the full Elysia server works. | ||
|
|
||
| On Node.js, Elysia's `.listen` is not supported: the adapter is tested only through the app's fetch handler, `app.fetch`, so use that. (Elysia's separate Node adapter is not covered by this SDK's tests.) | ||
|
|
||
| ## What it does | ||
|
|
||
| Elysia hooks are additive, so `setupElysia` adds three without replacing yours: | ||
|
|
||
| | Hook | What it does | | ||
| | --- | --- | | ||
| | `onRequest` | Opens a request context, continues an inbound `traceparent` and starts an `http.server` transaction. | | ||
| | `onError` | Reports server errors, meaning a plain throw or a 5xx status. Framework control flow such as `NOT_FOUND` and `VALIDATION` is skipped. | | ||
| | `mapResponse` | Finishes the transaction without altering the response. | | ||
|
|
||
| ## Options | ||
|
|
||
| Pass options as the second argument of `setupElysia`. | ||
|
|
||
| | Option | Type | Default | Description | | ||
| | --- | --- | --- | --- | | ||
| | `user` | `(c) => string \| undefined` | off | The end-user identity for reports raised during the request. | | ||
| | `shouldReport` | `(error) => boolean` | server errors only | Decides whether a thrown error is reported. | | ||
| | `onError` | `(error) => void` | none | Receives a failure of the adapter itself. It never reaches the request. | | ||
|
|
||
| `c` is a structural view of the Elysia context: `request`, `set` and `route`. | ||
|
|
||
| ## 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](/sdk/javascript/backend/overview#incoming-request-instrumentation). | | ||
| | `captureLocalVariables` | `false` | Attach local variables to the stack frames of a reported error. Always off on Bun. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
| | `captureSourceContext` | `false` | Attach the source lines around each stack frame. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
| | `detectHangs` | `true` | Report event-loop hangs. See [Hangs](/sdk/javascript/issue-detection/hangs). | | ||
| | `profiling` | `false` | Attach a rolling CPU profile to reports. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
|
|
||
| `@bugsee/elysia` 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, use `@bugsee/bun` for `launch()` and note that it has no `http.server` transaction; see [Running on Bun](/sdk/javascript/backend/overview#running-on-bun). | ||
|
|
||
| ## Related pages | ||
|
|
||
| - [Backend overview](/sdk/javascript/backend/overview) — request context, trace correlation and incoming-request instrumentation | ||
| - [Issue detection](/sdk/javascript/issue-detection/overview) — what else is detected on a server | ||
| - [Performance](/sdk/javascript/performance) — transactions and spans | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| --- | ||
| title: "Express" | ||
| description: "Report errors and trace requests in an Express app with the Bugsee JavaScript SDK: per-request context, http.server transactions and error capture." | ||
| sidebar_position: 2 | ||
| slug: "/sdk/javascript/backend/express" | ||
| --- | ||
|
|
||
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
|
|
||
| :::warning[Beta] | ||
| This page documents the Bugsee JavaScript SDK, which is in beta (0.1.x) and can change between betas. | ||
| ::: | ||
|
|
||
| `@bugsee/express` adds a request context, an `http.server` transaction and error reporting to an Express app on Node.js. Read the [backend overview](/sdk/javascript/backend/overview) first for how context and trace correlation work. | ||
|
|
||
| You need an app token. Create a JavaScript application in Bugsee and choose **Express** as its target; see [Create your app](/sdk/javascript/installation#create-your-app). | ||
|
|
||
| ## Install | ||
|
|
||
| <Tabs groupId="package-manager"> | ||
| <TabItem value="npm" label="npm"> | ||
|
|
||
| ```bash | ||
| npm install @bugsee/express | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="pnpm" label="pnpm"> | ||
|
|
||
| ```bash | ||
| pnpm add @bugsee/express | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="yarn" label="yarn"> | ||
|
|
||
| ```bash | ||
| yarn add @bugsee/express | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="bun" label="bun"> | ||
|
|
||
| ```bash | ||
| bun add @bugsee/express | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| The adapter declares no peer dependency. It only relies on the shape of your Express app (Express 4 or 5). | ||
|
|
||
| ## Set up | ||
|
|
||
| Call `launch()` at the top of your server entry, then pass the app to `setupExpress`: | ||
|
|
||
| ```ts title="server.ts" | ||
| import { launch, setupExpress } from '@bugsee/express'; | ||
| import express from 'express'; | ||
|
|
||
| launch('<your_app_token>'); | ||
|
|
||
| const app = express(); | ||
| setupExpress(app); | ||
|
|
||
| // ... your routes ... | ||
|
|
||
| app.listen(3000); | ||
| ``` | ||
|
|
||
| `setupExpress` installs the request middleware immediately, so call it before you register routes. It installs the error handler for you: | ||
|
|
||
| - It wraps `app.listen`, so the handler is added when the server starts listening, after your routes exist. | ||
| - An app that never calls `app.listen` (for example one passed to `http.createServer`) gets the handler on its first request. | ||
| - The handler is placed in front of any error middleware you already registered, so an error-response middleware that ends the response without calling `next(err)` does not hide the error from Bugsee. | ||
|
|
||
| ## Place the error handler yourself | ||
|
|
||
| To control the position, turn off the automatic handler and add it with `setupExpressErrorHandler`: | ||
|
|
||
| ```ts title="server.ts" | ||
| import { setupExpress, setupExpressErrorHandler } from '@bugsee/express'; | ||
|
|
||
| setupExpress(app, { autoErrorHandler: false }); | ||
|
|
||
| // ... your routes ... | ||
|
|
||
| setupExpressErrorHandler(app); // reports, then calls next(err) | ||
|
|
||
| app.use((err, req, res, next) => { | ||
| // your error response | ||
| }); | ||
| ``` | ||
|
|
||
| For full control, the two middlewares are also exported on their own: `requestHandler()` (mount it first) and `errorHandler()` (mount it after your routes). | ||
|
|
||
| ## What it does | ||
|
|
||
| - Opens a request context for each request, so everything reported inside a route is tagged with that request. | ||
| - Continues an inbound `traceparent` header and records an `http.server` transaction named after the route pattern, such as `/users/:id`. | ||
| - Reports an error that reaches the error handler, with the request context merged in, and then passes it on with `next(err)`. Your own error handling still runs. | ||
| - Does nothing, and passes every request through, when no client is launched. | ||
|
|
||
| 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 `setupExpress`. `setupExpressErrorHandler`, `requestHandler` and `errorHandler` take the same options, without `autoErrorHandler`. | ||
|
|
||
| | 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. | | ||
| | `autoErrorHandler` | `boolean` | `true` | Set `false` to place the error handler yourself. `setupExpress` only. | | ||
|
|
||
| `req` is a structural view of the Express request: `method`, `url`, `originalUrl`, `route`, `baseUrl` and `headers`. Cast it to read fields added by your own middleware: | ||
|
|
||
| ```ts | ||
| setupExpress(app, { | ||
| user: (req) => (req as { user?: { id: string } }).user?.id, | ||
| }); | ||
| ``` | ||
|
|
||
| ## 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](/sdk/javascript/backend/overview#incoming-request-instrumentation). | | ||
| | `captureLocalVariables` | `false` | Attach local variables to the stack frames of a reported error. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
| | `captureSourceContext` | `false` | Attach the source lines around each stack frame. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
| | `detectHangs` | `true` | Report event-loop hangs. See [Hangs](/sdk/javascript/issue-detection/hangs). | | ||
| | `profiling` | `false` | Attach a rolling CPU profile to reports. See [Diagnostics](/sdk/javascript/issue-detection/diagnostics). | | ||
|
|
||
| `@bugsee/express` 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](/sdk/javascript/backend/overview#running-on-bun). | ||
|
|
||
| ## Related pages | ||
|
|
||
| - [Backend overview](/sdk/javascript/backend/overview) — request context, trace correlation and incoming-request instrumentation | ||
| - [Issue detection](/sdk/javascript/issue-detection/overview) — what else is detected on a server | ||
| - [Performance](/sdk/javascript/performance) — transactions and spans |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.