$npx -y skills add laravel/agent-skills --skill configure-nightwatchConfigures Laravel Nightwatch data collection, sampling rates, filtering rules, and redaction policies. Use when setting up Nightwatch, managing data volume, protecting sensitive data (PII), or optimizing event collection for production workloads.
| 1 | # Nightwatch Configuration Guide |
| 2 | |
| 3 | This skill helps configure Laravel Nightwatch data collection to balance observability, performance, and privacy. Covers sampling strategies, filtering rules, and redaction methods across all event types. |
| 4 | |
| 5 | ## Documentation Reference |
| 6 | |
| 7 | The [Nightwatch Documentation](https://nightwatch.laravel.com/docs) is the definitive and up-to-date source of information for all Nightwatch configuration options. This skill provides practical guidance and common patterns, but always consult the official documentation as the primary source of truth for specific details, environment variables, and API behavior. The documentation includes comprehensive coverage of: |
| 8 | |
| 9 | - [Filtering and Configuration](https://nightwatch.laravel.com/docs/filtering) - Core concepts for sampling, filtering, and redaction |
| 10 | - Individual event type pages with specific configuration options: |
| 11 | - [Requests](https://nightwatch.laravel.com/docs/requests) - Request sampling, header handling, payload capture |
| 12 | - [Commands](https://nightwatch.laravel.com/docs/commands) - Command sampling and redaction |
| 13 | - [Queries](https://nightwatch.laravel.com/docs/queries) - Query filtering and redaction |
| 14 | - [Cache](https://nightwatch.laravel.com/docs/cache) - Cache event filtering by key or pattern |
| 15 | - [Jobs](https://nightwatch.laravel.com/docs/jobs) - Job filtering and sampling decoupling |
| 16 | - [Mail](https://nightwatch.laravel.com/docs/mail) - Mail event filtering |
| 17 | - [Notifications](https://nightwatch.laravel.com/docs/notifications) - Notification filtering by channel |
| 18 | - [Exceptions](https://nightwatch.laravel.com/docs/exceptions) - Exception sampling and throttling |
| 19 | - [Outgoing Requests](https://nightwatch.laravel.com/docs/outgoing-requests) - HTTP request filtering |
| 20 | - [reference.md](reference.md) - Quick lookup table by event type, production presets, and verification checklist |
| 21 | |
| 22 | ## Data Collection Flow |
| 23 | |
| 24 | Nightwatch processes events through three stages: |
| 25 | |
| 26 | 1. **Sampling** - Controls which entry points are captured (requests, commands, scheduled tasks) |
| 27 | 2. **Filtering** - Excludes specific events after sampling (queries, cache, mail, etc.) |
| 28 | 3. **Redaction** - Modifies captured data to remove/obfuscate sensitive information |
| 29 | |
| 30 | ``` |
| 31 | Request/Command/Scheduled Task |
| 32 | | |
| 33 | v |
| 34 | [Sampling?] ----NO----> Drop entire trace |
| 35 | | YES |
| 36 | v |
| 37 | Events generated |
| 38 | | |
| 39 | v |
| 40 | [Filtering?] ----YES---> Drop specific event |
| 41 | | NO |
| 42 | v |
| 43 | [Redaction] ----------> Store modified data |
| 44 | ``` |
| 45 | |
| 46 | --- |
| 47 | |
| 48 | ## Sampling Configuration |
| 49 | |
| 50 | Sampling determines which entry points (requests, commands, scheduled tasks) trigger full trace collection. When an entry point is sampled, all related events are captured. |
| 51 | |
| 52 | ### Global Sample Rates |
| 53 | |
| 54 | Configure via environment variables: |
| 55 | |
| 56 | ```bash |
| 57 | # Default: 100% sampling (all requests/commands captured) |
| 58 | NIGHTWATCH_REQUEST_SAMPLE_RATE=0.1 # Recommended: 10% of requests |
| 59 | NIGHTWATCH_COMMAND_SAMPLE_RATE=1.0 # Capture all commands |
| 60 | NIGHTWATCH_EXCEPTION_SAMPLE_RATE=1.0 # Always capture exceptions |
| 61 | ``` |
| 62 | |
| 63 | **Recommendation**: Start with `0.1` (10%) for requests in production, adjust based on volume and needs. |
| 64 | |
| 65 | ### Route-Based Sampling |
| 66 | |
| 67 | Apply different rates to specific routes using the `Sample` middleware: |
| 68 | |
| 69 | ```php routes/web.php |
| 70 | use Illuminate\Support\Facades\Route; |
| 71 | use Laravel\Nightwatch\Http\Middleware\Sample; |
| 72 | |
| 73 | // Sample admin routes at 100% |
| 74 | Route::middleware(Sample::rate(1.0))->prefix('admin')->group(function () { |
| 75 | // All admin routes sampled fully |
| 76 | }); |
| 77 | |
| 78 | // Sample API routes at 5% |
| 79 | Route::middleware(Sample::rate(0.05))->prefix('api')->group(function () { |
| 80 | // API routes sampled sparingly |
| 81 | }); |
| 82 | |
| 83 | // Always sample critical endpoints |
| 84 | Route::post('/checkout', [CheckoutController::class, 'process']) |
| 85 | ->middleware(Sample::always()); |
| 86 | |
| 87 | // Never sample health checks |
| 88 | Route::get('/health', [HealthController::class, 'check']) |
| 89 | ->middleware(Sample::never()); |
| 90 | ``` |
| 91 | |
| 92 | ### Unmatched Route Sampling |
| 93 | |
| 94 | Handle 404/bot traffic with reduced sampling: |
| 95 | |
| 96 | ```php routes/web.php |
| 97 | Route::fallback(fn () => abort(404)) |
| 98 | ->middleware(Sample::rate(0.01)); // 1% sampling for unmatched routes |
| 99 | ``` |
| 100 | |
| 101 | ### Dynamic Sampling |
| 102 | |
| 103 | Sample based on runtime conditions (user role, request attributes): |
| 104 | |
| 105 | ```php app/Http/Middleware/SampleAdminRequests.php |
| 106 | use Closure; |
| 107 | use Illuminate\Http\Request; |
| 108 | use Laravel\Nightwatch\Facades\Nightwatch; |
| 109 | |
| 110 | class SampleAdminRequests |
| 111 | { |
| 112 | public function handle(Request $request, Closure $next) |
| 113 | { |
| 114 | if ($request->user()?->isAdmin()) { |
| 115 | Nightwatch::sample(); // Always sample admin requests |
| 116 | } |
| 117 | return $next($request); |
| 118 | } |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | ### Command Sampling |
| 123 | |
| 124 | Exclude specific c |