lukaszaleckas / laravel-alerts
Requires
- php: ^8.0
- ext-json: *
- laravel/framework: ^8.83|^9.0
Requires (Dev)
- mockery/mockery: ^1.5
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.24
- phpstan/phpstan-mockery: ^1.0
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^6.4
- squizlabs/php_codesniffer: ^3.6
README
Installation
- Run:
composer require lukaszaleckas/laravel-alerts
Service provider should be automatically registered, if not add
LaravelAlerts\LaravelAlertsServiceProvider::class
to your application's app.php
.
- Publish
laravel-alerts.php
config file:
php artisan vendor:publish --tag=laravel-alerts
- Schedule
RunChecksCommand
in yourApp\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:
- Create a new class anywhere in you project.
- Extend it from
LaravelAlerts\Contracts\AbstractAlert
:
class MySuperDuperAlert extends AbstractAlert { ...
-
Add required method
buildAlertResult
which should returnLaravelAlerts\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. -
Next, we need to register this alert in the config files
alerts
array. Below is an example of afailed_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:
- Add
ConfigKeys::CONFIG
in yoularavel-alerts.php
alerts' configuration and specify any number of parameters. (See an example above). - 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 getAlertLevel
and 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 namequeue
(required) (string) - Queue namethreshold
(required) (int) - Queue size (job count) threshold
TableSizeAlert
Alert is triggered if database table size exceeds threshold.
Configuration
connection
(required) (string) - Database connection nametable_name
(required) (string) - Table namethreshold
(required) (int) - Table size (row count) threshold