Skip to content

Commit 229e15d

Browse files
committed
docs(express): add v7 docs for express
1 parent 5c60bba commit 229e15d

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Express
3+
description: "Learn about using Sentry with Express."
4+
sdk: sentry.javascript.node
5+
fallbackGuide: javascript.node
6+
categories:
7+
- server
8+
---
9+
10+
<PlatformContent includePath="getting-started-primer" />
11+
12+
This guide explains how to set up Sentry in your Express application.
13+
14+
<PlatformContent includePath="getting-started-node" />
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
2+
<OnboardingOptionButtons
3+
options={["error-monitoring", "performance", "profiling"]}
4+
/>
5+
<PlatformContent includePath="getting-started-install" />
6+
7+
Sentry should be initialized as early in your app as possible:
8+
9+
```javascript {tabTitle:CommonJS} {filename: instrument.js} {"onboardingOptions": {"performance": "10-14", "profiling": "3, 15-18, 22"}}
10+
const express = require('express');
11+
const Sentry = require('@sentry/node');
12+
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
13+
14+
const app = express();
15+
16+
// Sentry must be initialized as early as possible
17+
Sentry.init({
18+
dsn: "___DSN___",
19+
20+
// Add Performance Monitoring by setting tracesSampleRate and adding integration
21+
// Set tracesSampleRate to 1.0 to capture 100% of transactions
22+
// We recommend adjusting this value in production
23+
tracesSampleRate: 1.0,
24+
25+
// Set sampling rate for profiling
26+
// This is relative to tracesSampleRate
27+
profilesSampleRate: 1.0,
28+
29+
integrations: [
30+
expressIntegration({ app }),
31+
nodeProfilingIntegration(),
32+
],
33+
34+
// It is possible to specify the routes that should be traced by passing a router instance to the `router` option:
35+
// expressIntegration({ router: someRouterInstance })
36+
});
37+
38+
// The Sentry request handler middleware must be added before any other handlers
39+
app.use(Sentry.Handlers.requestHandler());
40+
// Performance Monitoring: Create spans and traces for every incoming request
41+
app.use(Sentry.Handlers.tracingHandler());
42+
43+
// All controllers should live here
44+
app.get("/", function rootHandler(req, res) {
45+
res.end("Hello world!");
46+
});
47+
48+
// The Sentry error handler middleware must be registered before any other error middleware and after all controllers
49+
app.use(Sentry.Handlers.errorHandler());
50+
51+
// Optional fallthrough error handler
52+
app.use(function onError(err, req, res, next) {
53+
// The ID of error events captured by the Sentry error middleware is attached to `res.sentry`.
54+
// You can use this ID for debugging, or to correlate requests with errors in Sentry.
55+
res.statusCode = 500;
56+
res.end(res.sentry + "\n");
57+
});
58+
59+
app.listen(3000);
60+
```
61+
62+
```javascript {tabTitle:ESM} {filename: instrument.mjs} {"onboardingOptions": {"performance": "10-14", "profiling": "3, 15-18, 22"}}
63+
import express from "express";
64+
import * as Sentry from "@sentry/node";
65+
import { nodeProfilingIntegration } from '@sentry/profiling-node';
66+
67+
const app = express();
68+
69+
// Sentry must be initialized as early as possible
70+
Sentry.init({
71+
dsn: "___DSN___",
72+
73+
// Add Performance Monitoring by setting tracesSampleRate and adding integration
74+
// Set tracesSampleRate to 1.0 to capture 100% of transactions
75+
// We recommend adjusting this value in production
76+
tracesSampleRate: 1.0,
77+
78+
// Set sampling rate for profiling
79+
// This is relative to tracesSampleRate
80+
profilesSampleRate: 1.0,
81+
82+
integrations: [
83+
expressIntegration({ app }),
84+
nodeProfilingIntegration(),
85+
],
86+
87+
// It is possible to specify the routes that should be traced by passing a router instance to the `router` option:
88+
// expressIntegration({ router: someRouterInstance })
89+
});
90+
91+
// The Sentry request handler middleware must be added before any other handlers
92+
app.use(Sentry.Handlers.requestHandler());
93+
// Performance Monitoring: Create spans and traces for every incoming request
94+
app.use(Sentry.Handlers.tracingHandler());
95+
96+
// All controllers should live here
97+
app.get("/", function rootHandler(req, res) {
98+
res.end("Hello world!");
99+
});
100+
101+
// The Sentry error handler middleware must be registered before any other error middleware and after all controllers
102+
app.use(Sentry.Handlers.errorHandler());
103+
104+
// Optional fallthrough error handler
105+
app.use(function onError(err, req, res, next) {
106+
// The ID of error events captured by the Sentry error middleware is attached to `res.sentry`.
107+
// You can use this ID for debugging, or to correlate requests with errors in Sentry.
108+
res.statusCode = 500;
109+
res.end(res.sentry + "\n");
110+
});
111+
112+
app.listen(3000);
113+
```
114+
115+
You can verify the Sentry integration by creating a route that will throw an error:
116+
117+
```javascript
118+
app.get("/debug-sentry", function mainHandler(req, res) {
119+
throw new Error("My first Sentry error!");
120+
});
121+
```
122+
123+
`requestHandler` accepts some options that let you decide what data should be included in the event sent to Sentry.
124+
125+
Possible options are:
126+
127+
```javascript
128+
// keys to be extracted from req
129+
request?: boolean | string[]; // default: true = ['cookies', 'data', 'headers', 'method', 'query_string', 'url']
130+
131+
// server name
132+
serverName?: boolean; // default: true
133+
134+
// generate transaction name
135+
// path == request.path (eg. "/foo")
136+
// methodPath == request.method + request.path (eg. "GET|/foo")
137+
// handler == function name (eg. "fooHandler")
138+
transaction?: boolean | 'path' | 'methodPath' | 'handler'; // default: true = 'methodPath'
139+
140+
// keys to be extracted from req.user
141+
user?: boolean | string[]; // default: true = ['id', 'username', 'email']
142+
143+
// client ip address
144+
ip?: boolean; // default: false
145+
146+
// node version
147+
version?: boolean; // default: true
148+
149+
// timeout for fatal route errors to be delivered
150+
flushTimeout?: number; // default: undefined
151+
```
152+
153+
For example, if you want to skip the server name and add just user, you would use requestHandler like this:
154+
155+
```javascript
156+
app.use(
157+
Sentry.Handlers.requestHandler({
158+
serverName: false,
159+
user: ["email"],
160+
})
161+
);
162+
```
163+
164+
By default, `errorHandler` will capture only errors with a status code of `500` or higher. If you want to change it, provide it with the `shouldHandleError` callback, which accepts middleware errors as its argument and decides, whether an error should be sent or not, by returning an appropriate boolean value.
165+
166+
```javascript
167+
app.use(
168+
Sentry.Handlers.errorHandler({
169+
shouldHandleError(error) {
170+
// Capture all 404 and 500 errors
171+
if (error.status === 404 || error.status === 500) {
172+
return true;
173+
}
174+
return false;
175+
},
176+
})
177+
);
178+
```
179+
180+
## Monitor Performance
181+
182+
You can monitor the performance of your Express application by setting a `tracesSampleRate`, adding the `expressIntegration`, and registering the `tracingHandler` middleware.
183+
184+
Spans are created for the following operations:
185+
186+
- HTTP requests made with `request`
187+
- `get` calls using native `http` and `https` modules
188+
- Middleware (Express.js only)
189+
190+
### Troubleshooting
191+
When capturing errors locally, ensure that your project's data filter for filtering localhost events is toggled off:
192+
193+
![Issue alert](./img/express-data-filters.png)
194+
195+
This ensures that errors produced by your browser (such as errors produced by HTTP methods) are properly captured.

0 commit comments

Comments
 (0)