laravel/nightwatch
Official Laravel Nightwatch package that collects application performance metrics and sends them to the hosted Nightwatch monitoring platform, providing Laravel-optimized insights into requests, errors, and overall app health.
composer require laravel/nightwatch
php artisan vendor:publish --provider="Laravel\Nightwatch\NightwatchServiceProvider" --tag="config"
.env:
NIGHTWATCH_API_TOKEN=your_api_token_here
php artisan nightwatch:status
Nightwatch automatically tracks HTTP requests, exceptions, and command execution. No additional code is needed for basic monitoring. To manually trigger a digest (forcing data transmission):
use Laravel\Nightwatch\Facades\Nightwatch;
Nightwatch::digest(); // Force immediate transmission of buffered data
Automatic Instrumentation
use Laravel\Nightwatch\Facades\Nightwatch;
Route::get('/custom-monitor', function () {
Nightwatch::group('CustomGroup', ['key' => 'value']);
// Your logic here
Nightwatch::ungroup();
});
Sampling Control
.env:
NIGHTWATCH_COMMAND_SAMPLE_RATE=0.1 # 10% of commands
NIGHTWATCH_SCHEDULED_TASK_SAMPLE_RATE=0.5 # 50% of tasks
Nightwatch::sample(0.2); // 20% sample rate for this context
Deploy Tracking
nightwatch:deploy command to mark deployments:
php artisan nightwatch:deploy --message="Hotfix for critical bug"
post-deploy hooks (e.g., Forge, Deployer):
php artisan nightwatch:deploy --message="Production deploy"
Exception Redaction
Nightwatch::redact('password', '****');
Nightwatch::group() to nest job-specific metrics.vapor:schedule) are supported. Configure sampling separately:
NIGHTWATCH_SCHEDULED_TASK_SAMPLE_RATE=0.3
Nightwatch::event('user_login', ['ip' => request()->ip()]);
configure-nightwatch Boost skill to auto-configure sampling and ignore rules.Double Sampling
NIGHTWATCH_COMMAND_SAMPLE_RATE and NIGHTWATCH_SCHEDULED_TASK_SAMPLE_RATE are both set, the latter takes precedence for scheduled tasks. Ensure alignment between them.Ignored Commands
schedule:finish and octane:status are ignored by default. To re-enable:
Nightwatch::ignore(false);
Payload Buffering
Nightwatch::digest() if needed (e.g., before a long-running process).Environment Variables
NIGHTWATCH_DEPLOY falls back to VAPOR_COMMIT_HASH if unset. Ensure consistency in CI/CD pipelines.Windows Compatibility
stdout/stderr) are written synchronously on Windows to avoid buffering issues.Check Status:
php artisan nightwatch:status
Outputs connection status, last digest time, and buffered events.
Log Levels:
Enable debug logs in config/nightwatch.php:
'log_level' => 'debug',
Manual Testing: Simulate events in tests:
$this->artisan('nightwatch:test-event', ['event' => 'test_event']);
Custom Event Handlers
Extend the Nightwatch facade to add domain-specific events:
Nightwatch::extend(function ($nightwatch) {
$nightwatch->on('custom_event', function ($payload) {
// Process payload
});
});
Agent Configuration
Override the agent URL or timeout in config/nightwatch.php:
'agent' => [
'url' => env('NIGHTWATCH_AGENT_URL', 'https://agent.nightwatch.laravel.com'),
'timeout' => 5.0,
],
Ignore Rules Dynamically ignore specific routes or commands:
Nightwatch::ignore(function ($event) {
return str_contains($event->getName(), 'admin.');
});
Sampling Strategies:
Use higher sample rates (e.g., 0.5) for critical paths and lower rates (e.g., 0.1) for high-frequency, low-priority tasks.
Deploy Context: Attach deploy metadata (e.g., Git SHA, author) for traceability:
php artisan nightwatch:deploy --message="Release v1.0" --sha="abc123"
Exception Context: Capture request payloads on unhandled exceptions for debugging:
Nightwatch::captureRequestPayload(true);
Performance Impact:
Nightwatch adds minimal overhead (~1-2ms per request). Monitor with nightwatch:status to verify.
How can I help you explore Laravel packages today?