What is @sentry/node?
The @sentry/node package is a tool designed for real-time monitoring and fixing crashes in Node.js applications. It provides error tracking and performance monitoring, helping developers to quickly identify, diagnose, and fix problems in their applications. Sentry integrates seamlessly with your existing codebase, offering a range of features to enhance application reliability and user experience.
What are @sentry/node's main functionalities?
Error Tracking
Automatically capture exceptions and errors in your Node.js applications. The code initializes Sentry with your project's DSN and demonstrates how an uncaught exception is automatically reported to Sentry.
const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'YOUR_DSN_HERE' });
app.get('/', function mainHandler(req, res) {
throw new Error('Broke!');
});
Performance Monitoring
Track the performance of your application, including request times and slow operations. This code sample starts a transaction, simulates an operation with a timeout, and then finishes the transaction, which is then reported to Sentry for performance analysis.
const Sentry = require('@sentry/node');
const transaction = Sentry.startTransaction({ op: 'test', name: 'My First Test Transaction' });
setTimeout(() => {
transaction.finish();
}, 99);
Custom Event Capturing
Send custom messages or events to Sentry. This is useful for capturing non-exception events that are significant for your application's health monitoring and diagnostics.
const Sentry = require('@sentry/node');
Sentry.captureMessage('Something went wrong', 'error');
Other packages similar to @sentry/node
bugsnag-js
Bugsnag provides error monitoring for web, mobile, and server applications. Similar to @sentry/node, it offers real-time error reporting and allows for detailed error diagnostics and performance monitoring. Bugsnag differentiates itself with features tailored to mobile app monitoring.
rollbar
Rollbar offers real-time error tracking and debugging tools for developers. Like @sentry/node, it supports multiple programming languages and frameworks, including Node.js. Rollbar emphasizes its ability to help teams with workflow integrations and automated error grouping for efficient management.
raygun
Raygun provides crash reporting, real-user monitoring, and deployment tracking. It's similar to @sentry/node in its core functionalities of error tracking and performance monitoring but also offers unique features like user journey tracking and version comparisons to understand the impact of deployments.
Official Sentry SDK for Node (EXPERIMENTAL)
This is a WIP, proof of concept implementation of a Node SDK that uses OpenTelemetry for performance instrumentation under the hood.
THIS MAY/WILL BREAK IN MANY UNEXPECTED WAYS. We may remove, add, change any of the integrations, add/remove any exports, etc.
This package is NOT READY TO USE IN ANY FORM OF PRODUCTION ENVIRONMENT!
This SDK is considered experimental and in an alpha state. It may experience breaking changes, and may be discontinued at any time. Please reach out on
GitHub if you have any feedback/concerns.
Installation
npm install @sentry/node-experimental
yarn add @sentry/node-experimental
Usage
const Sentry = require('@sentry/node-experimental');
import * as Sentry from '@sentry/node-experimental';
Sentry.init({
dsn: '__DSN__',
});
Note that it is necessary to initialize Sentry before you import any package that may be instrumented by us.
Status of this Experiment
Currently, this SDK:
- Will capture errors (same as @sentry/node)
- Auto-instrument for performance - see below for which performance integrations are available.
- Provide some manual instrumentation APIs
- Sync OpenTelemetry Context with our Sentry Hub/Scope
Manual Instrumentation
You can manual instrument using the following APIs:
const Sentry = require('@sentry/node-experimental');
Sentry.startSpan({ description: 'outer' }, function (span) {
span.setData(customData);
doSomethingSlow();
Sentry.startSpan({ description: 'inner' }, function() {
doSomethingVerySlow();
});
});
You can also create spans without marking them as the active span.
Note that for most scenarios, we recommend the startSpan
syntax.
const Sentry = require('@sentry/node-experimental');
const span = Sentry.startInactiveSpan({ description: 'non-active span' });
doSomethingSlow();
span.end();
Finally you can also get the currently active span, if you need to do more with it:
const Sentry = require('@sentry/node-experimental');
const span = Sentry.getActiveSpan();
Async Context
We leverage the OpenTelemetry context forking in order to ensure isolation of parallel requests.
This means that as long as you are using an OpenTelemetry instrumentation for your framework of choice
(currently: Express or Fastify), you do not need to setup any requestHandler
or similar.
ESM Support
Due to the way OpenTelemetry handles instrumentation, this only works out of the box for CommonJS (require
) applications.
There is experimental support for running OpenTelemetry with ESM ("type": "module"
):
node --experimental-loader=@opentelemetry/instrumentation/hook.mjs ./app.js
You'll need to install @opentelemetry/instrumentation
in your app to ensure this works.
See OpenTelemetry Instrumentation Docs for details on this -
but note that this is a) experimental, and b) does not work with all integrations.
Available (Performance) Integrations
- Http
- Express
- Fastify
- Nest
- Mysql
- Mysql2
- GraphQL
- Mongo
- Mongoose
- Postgres
- Prisma
All of these are auto-discovered, you don't need to configure anything for performance.
You still need to register middlewares etc. for error capturing.
Other, non-performance integrations from @sentry/node
are also available (except for Undici).
Links
8.0.0-alpha.2
This alpha release fixes a build problem that prevented 8.0.0-alpha.1 from being properly released.
Important Changes
- feat: Remove
@sentry/opentelemetry-node
package (#10906)
The @sentry/opentelemetry-node
package has been removed. Instead, you can either use @sentry/node
with built-in
OpenTelemetry support, or use @sentry/opentelemetry
to manually connect Sentry with OpenTelemetry.
Removal/Refactoring of deprecated functionality
- ref: Refactor some deprecated
startSpan
options (#10825) - feat(v8/core): remove void from transport return (#10794)
- ref(integrations): Delete deprecated class integrations (#10887)
Other Changes
- feat(core): Use serialized spans in transaction event (#10912)
- feat(deps): bump @sentry/cli from 2.28.6 to 2.29.1 (#10908)
- feat(node): Allow to configure
skipOpenTelemetrySetup
(#10907) - feat(esm): Import rather than require
inspector
(#10910) - fix(browser): Don't use chrome variable name (#10874)
- chore(sveltekit): Fix punctuation in a console.log (#10895)
- fix(opentelemetry): Ensure DSC propagation works correctly (#10904)
- feat(browser): Exclude span exports from non-performance CDN bundles (#10879)
- ref: Refactor span status handling to be OTEL compatible (#10871)
- feat(core): Fix span scope handling & transaction setting (#10886)
- ref(ember): Avoid namespace import to hopefully resolve minification issue (#10885)
Work in this release contributed by @harish-talview & @bfontaine. Thank you for your contributions!