vandarpay/exception-monitoring

Vandar Exception Monitoring Laravel Package

1.1.4 2023-09-12 08:29 UTC

This package is auto-updated.

Last update: 2024-12-15 16:52:15 UTC


README

We need to define keys for each important section of the service.

When an exception occurs in a section, we need to add related key to redis and set a specific ttl to automatically remove the keys.

Also, need an api with dynamic parameter for keys and return related response. api return 500 if the key exists in redis and 200 otherwise.

we must prepare some functionality that get key of important section and ttl from services and this package must provide a dynamic route with dynamic parameter by the given key.

Requirement

  • Redis
  • PHP redis extension

Installation

composer

composer require vandarpay/exception-monitoring

you can publish config file:

#config
php artisan vendor:publish --provider="VandarPay\ExceptionMonitoring\Providers\ExceptionMonitoringServiceProvider" --tag="exception-monitoring-config"

Usage

Set key on redis

Here's a quick example:

use VandarPay\ExceptionMonitoring\Facades\ExceptionMonitoring;
    
    //...
    try{
        //...
        Mandate::store($data); // <--- this function will throw Exception
        //...
    } catch (Exception $exception){
        //...
        ExceptionMonitoring::set('mandate-store'); // <--- here we add important section key to redis
        //...
    }
    //...

the default ttl for each key is 300s. you can change that in config/exception-monitoring.php. or you can pass ttl as second argument in set function:

ExceptionMonitoring::set('mandate-store',60); // this key will exist until 60 second.

Calling Api for getin Status of mapped sections

there is a route with this pattern /api/exception-monitoring/{key}.

for security and privacy you must send X-TOKEN header with value set in config with token key name. make sure in config bellow key is set:

	'token' => env('EXCEPTION_MONITORING_TOKEN')

simply you can set EXCEPTION_MONITORING_TOKEN in .env file.

EXCEPTION_MONITORING_TOKEN="your_token"

for example if you call https://your-domain.com/api/exception-monitoring/mandate-store and,

if key exists in redis you will get bellow response:

//status code = 500
"NOK"

or if key not exists in redis you will get bellow response:

//status code = 200
"OK"

and if key X-TOKEN header not exists or invalid you will get bellow response:

//status code = 401
"UNAUTHENTICATED"

Remove key from redis manually

you can remove key from redis manually like bellow:

use VandarPay\ExceptionMonitoring\Facades\ExceptionMonitoring;

    ExceptionMonitoring::remove('mandate-store');

License

The MIT License (MIT). Please see License File for more information.