1. Configure Next.js with withTuskDrift
Wrap your Next.js configuration with the withTuskDrift function in your next.config.js or next.config.mjs file:
Basic Configuration
- CommonJS (next.config.js)
- ESM (next.config.mjs)
With Debug Logging for Next.js Integration
- CommonJS (next.config.js)
- ESM (next.config.mjs)
What withTuskDrift Does
The withTuskDrift wrapper automatically:
- Enables the Next.js instrumentation hook (for Next.js < 15.0.0-rc.1)
- Configures webpack externals for proper module interception
- Detects your Next.js version and adjusts configuration accordingly
- Preserves your existing Next.js configuration
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
debug | boolean | false | Enable debug logging to see what Tusk Drift is configuring during build. |
disableInstrumentationHook | boolean | false | Disable automatic setting of experimental.instrumentationHook. Not recommended, will break Tusk Drift’s Next.js integration. |
suppressWarnings | boolean | false | Suppress all warnings from Tusk Drift’s Next.js integration. |
2. Create instrumentation file
Create aninstrumentation.ts (or .js) file at the root of your Next.js project (or inside the src folder if using one):
Initialization Parameters
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required if using Tusk Cloud | Your Tusk Drift API key from the dashboard. |
env | string | process.env.NODE_ENV | The environment name (e.g., ‘dev’, ‘staging’, ‘production’). |
logLevel | ’silent’ | ‘error’ | ‘warn’ | ‘info’ | ‘debug' | 'info’ | The logging level for the Tusk Drift SDK. |
samplingRate | number | 1.0 | Override the base sampling rate (0.0 - 1.0) for recording. Takes precedence over TUSK_RECORDING_SAMPLING_RATE and config file base-rate settings. |
3. Update package.json scripts
In yourpackage.json file, manually add a script for "dev:record" that allows you to start the service with Tusk Drift in record mode.
4. Update recording configurations
Update the.tusk/config.yaml file in your project root to include recording configurations.
If you’re testing Tusk Drift out locally for the first time, use fixed mode
with a base rate of 1.0. After testing, we generally recommend adaptive
mode with a base rate between 0.01 and 0.1.
TUSK_RECORDING_SAMPLING_RATE overrides recording.sampling.base_rate.
TUSK_SAMPLING_RATE is still supported as a legacy alias, and
recording.sampling_rate remains a legacy config alias.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
sampling.mode | string | ”fixed” | Selects constant sampling or adaptive load shedding. |
sampling.base_rate | number | 1.0 | The base sampling rate (0.0 - 1.0). In fixed mode this is the effective rate. In adaptive mode the SDK may temporarily reduce below this base rate. |
sampling.min_rate | number | 0.001 in adaptive mode | The minimum steady-state sampling rate for adaptive mode. In critical conditions the SDK can still temporarily pause recording. |
sampling.log_transitions | boolean | true | Controls whether the SDK emits adaptive-sampling transition logs. Can also be overridden by TUSK_RECORDING_SAMPLING_LOG_TRANSITIONS. |
sampling_rate | number | None | Legacy alias for recording.sampling.base_rate. Still supported for backward compatibility. |
export_spans | boolean | false | Whether to export spans to the Tusk backend. If false, spans are only saved locally in .tusk/traces. |
enable_env_var_recording | boolean | false | Whether to record and replay environment variables. Recommended for accurate replay behavior if your app’s logic depends on environment variables. |
Troubleshooting
My packages aren't being instrumented
My packages aren't being instrumented
If logs are showing that your packages aren’t being instrumented:
-
Check file location: Ensure
instrumentation.tsis at the project root (same level asnext.config.js), not in theapporpagesdirectory. -
Check runtime guard: Make sure you have the
NEXT_RUNTIME === 'nodejs'check in yourregister()function. -
Enable debug logging: Set
debug: truein yourwithTuskDriftoptions to see what’s being configured.