Skip to content

docs: update continuous profiling docs #12998

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

Merged
merged 9 commits into from
Mar 24, 2025
62 changes: 57 additions & 5 deletions docs/platforms/javascript/common/profiling/node-profiling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ You have to have the <PlatformSdkPackageName fallback="@sentry/node"/> (minimum

## Enabling Profiling

Profiling supports two modes - `manual` and `trace`. The two modes are mutually exclusive, and cannot be used at the same time.

In `manual` mode, the profiling data collection can be managed via calls to `Sentry.profiler.startProfiler` and `Sentry.profiler.stopProfiler`. You are entirely in the in control of when the profiler runs.

In `trace` mode, the profiler manages its own start and stop calls, which are based on spans: the profiler continues to run while there is at least one active span, and stops when there are no active spans.

### Enabling Trace Lifecycle Profiling

To enable profiling, add `@sentry/profiling-node` to your imports and set up `nodeProfilingIntegration` in your Sentry config.

```javascript {diff}
Expand All @@ -47,24 +55,67 @@ Sentry.init({
+ nodeProfilingIntegration(),
],
tracesSampleRate: 1.0,
+ // Set sampling rate for profiling - this is relative to tracesSampleRate
+ profilesSampleRate: 1.0,
+ profileSessionSampleRate: 1.0,
+ profileLifecycle: 'trace',
});

// Profiling happens automatically after setting it up with `Sentry.init()`.
// All spans captured in your application will have profiling data attached to them.
// You can also manually capture spans with `startSpan`, as shown below:
// All spans (unless those discarded by sampling) will have profiling data attached to them.
Sentry.startSpan(
{
op: "rootSpan",
name: "My root span",
},
() => {
// Any code in this callback will be profiled.
// The code executed here will be profiled
}
);
```

### Enabling Manual Lifecycle Profiling

To enable profiling, add `@sentry/profiling-node` to your imports and set up `nodeProfilingIntegration` in your Sentry config.

```javascript {diff}
const { nodeProfilingIntegration } = require("@sentry/profiling-node");

Sentry.init({
dsn: "___PUBLIC_DSN___",
integrations: [
// Add our Profiling integration
+ nodeProfilingIntegration(),
],
tracesSampleRate: 1.0,
+ profileSessionSampleRate: 1.0,
+ profileLifecycle: 'manual',
});

// All spans (unless those discarded by sampling) will have profiling data attached to them.
Sentry.profiler.startProfiler();
// Code executed between these two calls will be profiled
Sentry.profiler.stopProfiler();
```

### Managing profile sampling rates

Sentry SDK supports an additional `profileSessionSampleRate` that will enable or disable profiling for the entire session. This can be used if you want to control session sampling rates at the service level as the sampling decision is evaluated only once at SDK init.

This is useful for cases where you deploy your service many times, but would only like a subset of those services to be profiled.

```javascript {diff}
const { nodeProfilingIntegration } = require("@sentry/profiling-node");

Sentry.init({
dsn: "___PUBLIC_DSN___",
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],
tracesSampleRate: 1.0,
+ profileSessionSampleRate: 0.0
});


## How Does It Work?

Under the hood, the Sentry profiler uses V8's [CpuProfiler](https://v8docs.nodesource.com/node-18.2/d2/d34/classv8_1_1_cpu_profiler.html) to collect stack samples. This means that `sentry/profiling-node` is written as a [native add-on](https://nodejs.org/docs/latest-v18.x/api/addons.html) for Node and won't run in environments like Deno or Bun. Profiling enhances tracing by providing profiles for individual transactions. This allows you to look at higher level performance information like transaction and span durations before diving deeper and looking at profiles.
Expand All @@ -81,3 +132,4 @@ Starting from version `0.1.0`, the `@sentry/profiling-node` package precompiles
- Windows x64: Node v16, v18, v20, v22

The set of common architectures should cover a wide variety of use cases, but if you have feedback or experience different behavior, please open an issue in the [Sentry JavaScript SDK](https://github.com/getsentry/sentry-javascript) repository.
```
Loading