omaralalwi/laravel-jobs-metrics

Tracks job memory consumption โ€” works with or without Horizon, and enables long-term performance analysis via database logging.

1.0.1 2025-05-23 21:59 UTC

This package is auto-updated.

Last update: 2025-05-23 22:00:33 UTC


README

Latest Version on Packagist Total Downloads MIT Licensed

Tracks job memory consumption and execution time โ€” works with or without Horizon, and enables long-term performance analysis via database logging.

laravel jobs metrics

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 Lexi Translate simplify managing translations for multilingual Eloquent models with power of morph relationships and caching .

  • Gpdf 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 Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications.

  • laravel Deployer laravel Deployer Streamlined Deployment for Laravel and Node.js apps, with Zero-Downtime and various environments and branches.

  • laravel Trash Cleaner laravel Trash Cleaner clean logs and debug files for debugging packages.

  • laravel Time Craft laravel Time Craft simple trait and helper functions that allow you, Effortlessly manage date and time queries in Laravel apps.

  • PHP builders PHP builders sample php traits to add ability to use builder design patterns with easy in PHP applications.

  • PhpPy - PHP Python PhpPy - PHP Python Interact with python in PHP applications.

  • Laravel Py - Laravel Python Laravel Py - Laravel Python interact with python in Laravel applications.

  • Deepseek PHP client 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 deepseek laravel Laravel wrapper for Deepseek PHP client to seamless deepseek AI API integration with Laravel applications.

  • Qwen PHP client Qwen PHP client robust and community-driven PHP client library for seamless integration with the Qwen API .

  • qwen laravel Laravel qwen wrapper for qwen PHP client to seamless Alibaba qwen AI API integration with Laravel applications..

Dashboards

  • Laravel Startkit Laravel Startkit Laravel Admin Dashboard, Admin Template with Frontend Template, for scalable Laravel projects.

  • Kunafa Dashboard Vue Kunafa Dashboard Vue A feature-rich Vue.js 3 dashboard template with multi-language support and full RTL/LTR bidirectional layout capabilities.

References