lukaszaleckas/laravel-alerts

There is no license information available for the latest version (v1.1.0) of this package.

v1.1.0 2023-01-21 12:08 UTC

This package is auto-updated.

Last update: 2024-04-21 14:48:52 UTC


README

Installation

  1. Run:
composer require lukaszaleckas/laravel-alerts

Service provider should be automatically registered, if not add

LaravelAlerts\LaravelAlertsServiceProvider::class

to your application's app.php.

  1. Publish laravel-alerts.php config file:
    php artisan vendor:publish --tag=laravel-alerts
  1. Schedule RunChecksCommand in your App\Console\Kernel schedule method:
    use LaravelAlerts\RunChecksCommand;

    protected function schedule(Schedule $schedule): void
    {
        $schedule->command(RunChecksCommand::class)
            ->everyMinute()
            ->withoutOverlapping();
    }

In the example above, we are running all of the checks registered in config file, every minute.

To run only specific alert checks, use the example below:

    protected function schedule(Schedule $schedule): void
    {
        $schedule->command(
            RunChecksCommand::class,
            [
                RunChecksCommand::ARGUMENT_IDENTIFIERS => [
                    'first_alert',
                    'second_alert'
                ]
            ]
        )
            ->everyMinute()
            ->withoutOverlapping();
    }

Creating alerts

Alert can be created using these steps:

  1. Create a new class anywhere in you project.
  2. Extend it from LaravelAlerts\Contracts\AbstractAlert:
class MySuperDuperAlert extends AbstractAlert
{
    ...
  1. Add required method buildAlertResult which should return LaravelAlerts\DTOs\AlertResultDto DTO. This DTO contains a boolean if alert was triggered, alert message and any additional context which will be appended to the alert's log.

  2. Next, we need to register this alert in the config files alerts array. Below is an example of a failed_jobs table row count alert configuration:

[
    ConfigKeys::ALERT      => TableSizeAlert::class,
    ConfigKeys::IDENTIFIER => 'failed_jobs_alert',
    ConfigKeys::CONFIG     => [
        TableSizeAlert::CONFIG_CONNECTION => 'mysql',
        TableSizeAlert::CONFIG_TABLE_NAME => 'failed_jobs',
        TableSizeAlert::CONFIG_THRESHOLD  => 30
    ]
]

Additional alert customization

Configuration parameters

To make alerts reusable, they can require configuration parameters. If you want to use this feature:

  1. Add ConfigKeys::CONFIG in you laravel-alerts.php alerts' configuration and specify any number of parameters. (See an example above).
  2. Override LaravelAlerts\Contracts\AbstractAlert configure method, which will receive an array containing all of the parameters you added in the first step.

Logging options

Log level and log channels can be configured by overriding LaravelAlerts\Contracts\AbstractAlert classes getAlertLeveland getLogChannels methods respectively.

Available alerts

This package contains some predefined alerts that you can use. They are in the LaravelAlerts\Alerts namespace.

QueueSizeAlert

Alert is triggered if queue size, on a particular connection, exceeds threshold.

Configuration

  • connection (required) (string) - Queue connection name
  • queue (required) (string) - Queue name
  • threshold (required) (int) - Queue size (job count) threshold

TableSizeAlert

Alert is triggered if database table size exceeds threshold.

Configuration

  • connection (required) (string) - Database connection name
  • table_name (required) (string) - Table name
  • threshold (required) (int) - Table size (row count) threshold