Skip to content
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

docs(node): Update Node README.md #12916

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ yarn add @sentry/node

## Usage

Sentry should be initialized as early in your app as possible. It is essential that you call `Sentry.init` before you
require any other modules in your application, otherwise auto-instrumentation of these modules will **not** work.

You need to create a file named `instrument.js` that imports and initializes Sentry:

```js
// CJS Syntax
const Sentry = require('@sentry/node');
Expand All @@ -33,26 +38,39 @@ Sentry.init({
});
```

Note that it is necessary to initialize Sentry **before you import any package that may be instrumented by us**.
You need to require or import the `instrument.js` file before importing any other modules in your application. This is
necessary to ensure that Sentry can automatically instrument all modules in your application:

```js
// Import this first!
import './instrument';

// Now import other modules
import http from 'http';

[More information on how to set up Sentry for Node in v8.](https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-node.md)
// Your application code goes here
```

### ESM Support

Due to the way OpenTelemetry handles instrumentation, this only works out of the box for CommonJS (`require`)
applications.
When running your application in ESM mode, you should use the Node.js
[`--import`](https://nodejs.org/api/cli.html#--importmodule) command line option to ensure that Sentry is loaded before
the application code is evaluated.

There is experimental support for running OpenTelemetry with ESM (`"type": "module"`):
Adjust the Node.js call for your application to use the `--import` parameter and point it at `instrument.js`, which
contains your `Sentry.init`() code:

```bash
node --experimental-loader=@opentelemetry/instrumentation/hook.mjs ./app.js
# Note: This is only available for Node v18.19.0 onwards.
node --import ./instrument.mjs app.mjs
```

You'll need to install `@opentelemetry/instrumentation` in your app to ensure this works.
If it is not possible for you to pass the `--import` flag to the Node.js binary, you can alternatively use the
`NODE_OPTIONS` environment variable as follows:

See
[OpenTelemetry Instrumentation Docs](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation#instrumentation-for-es-modules-in-nodejs-experimental)
for details on this - but note that this is a) experimental, and b) does not work with all integrations.
```bash
NODE_OPTIONS="--import ./instrument.mjs" npm run start
```

## Links

Expand Down
Loading