van-darpay/exception-monitoring

Vandar Exception Monitoring Laravel 包

1.1.4 2023-09-12 08:29 UTC

This package is auto-updated.

Last update: 2024-09-15 16:15:42 UTC


README

我们需要为服务的每个重要部分定义键。

当某个部分发生异常时,我们需要将相关键添加到 Redis 中,并设置特定的 ttl 以自动删除键。

还需要一个具有动态参数的 API,用于键和返回相关响应。如果键存在于 Redis 中,API 返回 500,否则返回 200。

我们必须准备一些功能,从服务中获取重要部分的键和 ttl,并且此包必须提供由给定键提供的动态路由。

要求

  • Redis
  • PHP redis 扩展

安装

composer

composer require vandarpay/exception-monitoring

你可以发布配置文件

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

使用方法

在 Redis 中设置键

这里有一个快速示例

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
        //...
    }
    //...

每个键的默认 ttl 为 300 秒。你可以在 config/exception-monitoring.php 中更改它。或者,你可以在 set 函数中将 ttl 作为第二个参数传递

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

调用 API 以获取映射部分的 getin 状态

存在一个具有此模式的路由 /api/exception-monitoring/{key}

出于安全和隐私考虑,你必须发送带有在配置中设置的值的 X-TOKEN 标头。请确保在以下配置中设置了密钥

	'token' => env('EXCEPTION_MONITORING_TOKEN')

简单地,你可以在 .env 文件中设置 EXCEPTION_MONITORING_TOKEN

EXCEPTION_MONITORING_TOKEN="your_token"

例如,如果你调用 https://your-domain.com/api/exception-monitoring/mandate-store

如果键存在于 Redis 中,你将获得以下响应

//status code = 500
"NOK"

或者,如果键不存在于 Redis 中,你将获得以下响应

//status code = 200
"OK"

如果 X-TOKEN 标头不存在或无效,你将获得以下响应

//status code = 401
"UNAUTHENTICATED"

手动从 Redis 中删除键

你可以手动从 Redis 中删除键,如下所示

use VandarPay\ExceptionMonitoring\Facades\ExceptionMonitoring;

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

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件