承接 esign/laravel-sentry-throttling 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

esign/laravel-sentry-throttling

最新稳定版本:1.0.1

Composer 安装命令:

composer require esign/laravel-sentry-throttling

包简介

Throttle exceptions being sent to Sentry

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

Laravel includes a built-in mechanism for throttling exceptions, but it doesn’t support defining throttling rules for individual reportables. This package adds the ability to throttle exceptions specifically before they are sent to Sentry. It's especially useful if you want to continue logging all exceptions locally while avoiding repeated reports of the same exception being sent to Sentry. While Sentry does offer its own sampling via the sample_rate setting, that approach is percentage-based and less granular than exception-specific throttling.

Installation

You can install the package via composer:

composer require esign/laravel-sentry-throttling

You must configure the before_send option in your config/sentry.php file so that Sentry uses the throttling logic. Add the following to your Sentry config:

// config/sentry.php
return [
    // ...existing config...
    'before_send' => [\Esign\SentryThrottling\SentryThrottling::class, 'beforeSend'],
];

Usage

Implementing throttling

The recommended way to use this package is to implement the ThrottlesSentryReports interface on your application's default exception handler (typically App\Exceptions\Handler):

use App\Exceptions\ApiMonitoringException;
use Illuminate\Broadcasting\BroadcastException;
use Esign\SentryThrottling\Contracts\ThrottlesSentryReports;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Lottery;
use Throwable;

class Handler extends ExceptionHandler implements ThrottlesSentryReports
{
    public function throttleSentry(Throwable $exception): Lottery | Limit | null
    {
        return match (true) {
            $exception instanceof BroadcastException => Limit::perMinute(300),
            $exception instanceof ApiMonitoringException => Lottery::odds(1, 1000),
            default => Limit::none(),
        };
    }
}

Whilst we recommend implementing the ThrottlesSentryReports interface on your exception handler, you can implement it on any class you like.

Binding the interface

You must bind your implementation of ThrottlesSentryReports in the Laravel container so the package can resolve it. This is typically done in a service provider, such as App\Providers\AppServiceProvider:

use Esign\SentryThrottling\Contracts\ThrottlesSentryReports;
use App\Exceptions\Handler;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(ThrottlesSentryReports::class, Handler::class);
    }
}

Testing

composer test

License

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

统计信息

  • 总下载量: 1.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-14