aeatech/web-snapshot-profiler-event-subscriber
Installation
composer require aeatech/web-snapshot-profiler-event-subscriber
Register the Subscriber
Add the subscriber to your EventServiceProvider (app/Providers/EventServiceProvider.php):
protected $listen = [
// ... other listeners
'kernel.request' => [
\Aeatech\WebSnapshotProfilerEventSubscriber\WebSnapshotProfilerEventSubscriber::class,
],
];
Configure Profiler
Publish the config file (if available) and adjust settings in config/web-snapshot-profiler.php:
php artisan vendor:publish --provider="Aeatech\WebSnapshotProfilerEventSubscriber\WebSnapshotProfilerEventSubscriberServiceProvider"
Enable profiling for specific routes or environments:
'enabled' => env('APP_ENV') === 'production',
'routes' => ['/admin/*', '/api/v1/*'],
First Use Case Trigger a snapshot for a specific request in a controller:
use Aeatech\WebSnapshotProfilerEventSubscriber\Facades\WebSnapshotProfiler;
public function index()
{
WebSnapshotProfiler::snapshot('homepage_load');
return view('home');
}
Enable for Critical Paths Use middleware to enable profiling only for high-traffic or performance-critical routes:
public function handle($request, Closure $next)
{
if ($request->is('/checkout*')) {
WebSnapshotProfiler::enable();
}
return $next($request);
}
Conditional Snapshots Capture snapshots dynamically based on user roles or request data:
if (auth()->user()->isAdmin()) {
WebSnapshotProfiler::snapshot('admin_dashboard');
}
Batch Processing
For CLI commands or queue jobs, use the WebSnapshotProfiler::start() and stop() methods:
WebSnapshotProfiler::start('job_processing');
// ... job logic
WebSnapshotProfiler::stop();
Laravel Debugbar Compatibility
If using barryvdh/laravel-debugbar, disable the profiler in config/debugbar.php to avoid conflicts:
'enabled' => env('APP_ENV') !== 'production',
Storage Backend
Extend the default storage (e.g., database or S3) by implementing Aeatech\WebSnapshotProfilerEventSubscriber\Contracts\SnapshotStorage:
class CustomStorage implements SnapshotStorage {
public function store(Snapshot $snapshot) { ... }
}
API Endpoints Expose snapshots via an API route:
Route::get('/api/snapshots', function () {
return WebSnapshotProfiler::getSnapshots();
});
Performance Overhead
'routes' => ['/performance-critical/*'],
Memory Leaks
WebSnapshotProfiler::pruneOldSnapshots(Carbon::now()->subDays(7));
Event Ordering
kernel.* event.kernel.request for start and kernel.terminate for end:
'kernel.request' => [\Aeatech\WebSnapshotProfilerEventSubscriber\WebSnapshotProfilerEventSubscriber::class . '@onRequest'],
'kernel.terminate' => [\Aeatech\WebSnapshotProfilerEventSubscriber\WebSnapshotProfilerEventSubscriber::class . '@onTerminate'],
Missing Snapshots
EventServiceProvider.APP_DEBUG is false in production (some profilers ignore snapshots in debug mode).Storage Issues
storage/snapshots) is writable:
chmod -R 775 storage/snapshots
Custom Metrics Add custom metrics to snapshots:
WebSnapshotProfiler::addMetric('db_queries', DB::getQueryLog()->count());
Snapshot Filters Filter snapshots by tags or metadata:
WebSnapshotProfiler::snapshot('user_login', ['user_id' => auth()->id()]);
Webhook Notifications Extend the subscriber to send alerts for slow snapshots:
public function onTerminate(Request $request, $response)
{
$snapshot = WebSnapshotProfiler::getLastSnapshot();
if ($snapshot->durationMs > 1000) {
// Send Slack/Email alert
}
}
Environment Variables
Override settings via .env:
WEBSNAPSHOT_PROFILER_ENABLED=true
WEBSNAPSHOT_PROFILER_ROUTES=/admin*,/api/*
Default Storage
Change the default storage path in config/web-snapshot-profiler.php:
'storage' => storage_path('app/snapshots'),
How can I help you explore Laravel packages today?