@@ -16,7 +16,8 @@ We support the following Node Frameworks out of the box:
16
16
17
17
- [ Express] ( #express )
18
18
- [ Fastify] ( #fastify )
19
- - Koa
19
+ - [ Connect] ( #connect )
20
+ - [ Koa] ( #koa )
20
21
- Nest.js
21
22
- Hapi
22
23
@@ -52,13 +53,36 @@ Sentry.init({
52
53
const app = express ();
53
54
```
54
55
56
+ We recommend creating a file named ` instrument.js ` that imports and initializes Sentry.
57
+
55
58
``` js
56
- // In v8, in order to ensure express is instrumented,
57
- // you have to initialize before you import:
59
+ // In v8, create a separate file that initializes sentry.
60
+ // Then pass the file to Node via --require or -- import.
58
61
const Sentry = require (' @sentry/node' );
59
62
Sentry .init ({
60
63
// ...
61
64
});
65
+ ```
66
+
67
+ Adjust the Node.js call for your application to use the [ --require] ( https://nodejs.org/api/cli.html#-r---require-module )
68
+ or [ --import] ( https://nodejs.org/api/cli.html#--importmodule ) parameter and point it at ` instrument.js ` . Using
69
+ ` --require ` or ` --import ` is the easiest way to guarantee that Sentry is imported and initialized before any other
70
+ modules in your application
71
+
72
+ ``` bash
73
+ # If you are using CommonJS (CJS)
74
+ node --require ./instrument.js app.js
75
+
76
+ # If you are using ECMAScript Modules (ESM)
77
+ # Note: This is only available for Node v18.19.0 onwards.
78
+ node --import ./instrument.mjs app.mjs
79
+ ```
80
+
81
+ ** Alternatively** , if you cannot run node with ` --require ` or ` --import ` , add a top level import of ` instrument.js ` in
82
+ your application.
83
+
84
+ ``` js
85
+ require (' ./instrument.js' );
62
86
63
87
const express = require (' express' );
64
88
const app = express ();
@@ -75,8 +99,11 @@ See [New Performance APIs](./v8-new-performance-apis.md) for details.
75
99
76
100
### ESM Support
77
101
78
- For now, ESM support is only experimental. For the time being we only fully support CJS-based Node application - we are
79
- working on this during the v8 alpha/beta cycle.
102
+ Instrumentation works out of the box for CommonJS (CJS) applications based on require() calls. This means that as long
103
+ as your application is either natively in CJS, or compiled at build time to CJS, everything will work without any
104
+ further setup.
105
+
106
+ ECMAScript Modules (ESM) are only supported for Node v18.19.0 onwards.
80
107
81
108
### Using Custom OpenTelemetry Instrumentation
82
109
@@ -153,12 +180,6 @@ your Express app.
153
180
154
181
``` js
155
182
const Sentry = require (' @sentry/node' );
156
-
157
- Sentry .init ({
158
- dsn: ' __DSN__' ,
159
- tracesSampleRate: 1 ,
160
- });
161
-
162
183
const express = require (' express' );
163
184
const app = express ();
164
185
@@ -177,12 +198,6 @@ your Fastify app.
177
198
178
199
``` js
179
200
const Sentry = require (' @sentry/node' );
180
-
181
- Sentry .init ({
182
- dsn: ' __DSN__' ,
183
- tracesSampleRate: 1 ,
184
- });
185
-
186
201
const { fastify } = require (' fastify' );
187
202
const app = fastify ();
188
203
Sentry .setupFastifyErrorHandler (app);
@@ -191,3 +206,40 @@ Sentry.setupFastifyErrorHandler(app);
191
206
192
207
app .listen ();
193
208
```
209
+
210
+ ## Connect
211
+
212
+ The following shows how you can setup Connect instrumentation in v8. This will capture performance data & errors for
213
+ your Fastify app.
214
+
215
+ ``` js
216
+ const connect = require (' connect' );
217
+ const Sentry = require (' @sentry/node' );
218
+ const app = connect ();
219
+
220
+ Sentry .setupConnectErrorHandler (app);
221
+
222
+ // Add your routes, etc.
223
+
224
+ app .listen (3030 );
225
+ ```
226
+
227
+ ## Koa
228
+
229
+ The following shows how you can setup Koa instrumentation in v8. This will capture performance data & errors for your
230
+ Fastify app.
231
+
232
+ ``` js
233
+ const Koa = require (' koa' );
234
+ const Router = require (' @koa/router' );
235
+ const Sentry = require (' @sentry/node' );
236
+
237
+ const router = new Router ();
238
+ const app = new Koa ();
239
+
240
+ Sentry .setupKoaErrorHandler (app);
241
+
242
+ // Add your routes, etc.
243
+
244
+ app .listen (3030 );
245
+ ```
0 commit comments