omaralalwi / laravel-jobs-metrics
Tracks job memory consumption โ works with or without Horizon, and enables long-term performance analysis via database logging.
Requires
- php: ^7.4|^8.0|^8.1|^8.2|^8.3
- illuminate/console: ^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
This package is auto-updated.
Last update: 2025-05-23 22:00:33 UTC
README
Tracks job memory consumption and execution time โ works with or without Horizon, and enables long-term performance analysis via database logging.
Table of Contents
Installation
You can install the package via composer:
composer require omaralalwi/laravel-jobs-metrics
After installation, publish the configuration file (optional):
php artisan vendor:publish --provider="Omaralalwi\JobsMetrics\JobsMetricsServiceProvider" --tag="config"
Run the migrations to create the jobs_metrics table:
php artisan migrate
Usage
Basic Usage
Add the HasJobsMetricTracker
trait to your job classes:
<?php namespace App\Jobs; use Omaralalwi\JobsMetrics\Traits\HasJobsMetricTracker; class ProcessPodcast implements ShouldQueue { use HasJobsMetricTracker; // Your job implementation... }
That's it! Metrics will be automatically recorded in the jobs_metrics
table.
Configuration
The package can be configured via the config/jobs-metrics.php
file. Here are the available options:
return [ // Enable or disable metrics tracking globally 'track_jobs_metrics' => (bool) env('TRACK_JOBS_METRICS', true), // Log errors that occur during metrics tracking 'log_errors' => (bool) env('JOBS_METRICS_LOG_ERRORS', true), ];
Viewing Job Metrics Report
The package provides a simple command to view job metrics:
# View job metrics with default options php artisan jobs-metrics:top # Customize display options php artisan jobs-metrics:top --limit=20 --sort=time --days=2
The command displays a comprehensive info in CLI.
Available options:
--limit=N
: Number of jobs to display (default: 10)--sort=memory|time
: Sort by memory usage or execution time (default: memory)--days=N
: Show data from the last N days (default: 7)
Exporting Job Metrics
You can export all job metrics to a single JSON file:
# Export job metrics to JSON with default options php artisan jobs-metrics:export # Specify a custom time period php artisan jobs-metrics:export --days=2
The export command creates a comprehensive JSON file in the storage/app/jobs-metrics-export
directory with timestamps in the filename.
Manually Cleaning Up Old Records
You can manually clean up old metrics records using the provided artisan command:
php artisan jobs-metrics:cleanup
To specify how many days of data to keep:
php artisan jobs-metrics:cleanup --days=5
Automatic Cleanup
To enable automatic cleanup, ensure your Laravel scheduler is running, and add following scheduler
Schedule::command('jobs-metrics:cleanup')->weekly();
Features
- ๐ Records detailed metrics for each job execution (memory usage, duration)
- ๐ Works with or without Laravel Horizon
- ๐ Per-job statistics including average executions per day
- ๐ Queue-based metrics to identify bottlenecks
- ๐ Export capabilities to JSON for further analysis
- ๐งน Automatic and manual cleanup of old records
- ๐๏ธ Highly configurable with simple interface
- ๐ Simple integration with a single trait
- ๐ Tested.
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
๐ Helpful Open Source Packages & Projects
Packages
-
Lexi Translate simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .
-
Gpdf Open Source HTML to PDF converter for PHP & Laravel Applications, supports Arabic content out-of-the-box and other languages.
-
laravel Taxify Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.
-
laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.
-
laravel Trash Cleaner clean logs and debug files for debugging packages.
-
laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.
-
PHP builders sample php traits to add ability to use builder design patterns with easy in PHP applications.
-
PhpPy - PHP Python Interact with python in PHP applications.
-
Laravel Py - Laravel Python interact with python in Laravel applications.
-
deepseek PHP client robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities .
-
deepseek laravel Laravel wrapper for Deepseek PHP client to seamless deepseek AI API integration with Laravel applications.
-
Qwen PHP client robust and community-driven PHP client library for seamless integration with the Qwen API .
-
Laravel qwen wrapper for qwen PHP client to seamless Alibaba qwen AI API integration with Laravel applications..
Dashboards
-
Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.
-
Kunafa Dashboard Vue A feature-rich Vue.js 3 dashboard template with multi-language support and full RTL/LTR bidirectional layout capabilities.
References
-
Clean Code Summary summarize and notes for books and practices about clean code.
-
SOLID Principles Summary summarize and notes for books about SOLID Principles.