PHP code example of webparking / laravel-sentry-throttling

1. Go to this page and download the library: Download webparking/laravel-sentry-throttling library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webparking / laravel-sentry-throttling example snippets


// config/sentry.php
use Webparking\SentryThrottling\SentryThrottling;

return [
    // ...existing config...
    'before_send' => function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
        $event = SentryThrottling::beforeSend($event, $hint);

        // Your own logic; $event is null when the throttle dropped the event.
        return $event;
    },
];

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



declare(strict_types=1);

namespace App\Sentry;

use Illuminate\Broadcasting\BroadcastException;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Lottery;
use Throwable;
use Webparking\SentryThrottling\Contracts\ThrottlesSentryReports;

final class SentryThrottler implements ThrottlesSentryReports
{
    public function throttleSentry(Throwable $exception): Limit|Lottery|null
    {
        return match (true) {
            // At most one report per minute, across all instances.
            $exception instanceof BroadcastException => Limit::perMinute(1),

            // One bucket per unique message, so distinct errors aren't collapsed.
            $exception instanceof \App\Exceptions\ApiException =>
                Limit::perHour(1)->by($exception->getMessage()),

            // Send roughly one in a hundred of a very noisy exception.
            $exception instanceof \App\Exceptions\NoisyException => Lottery::odds(1, 100),

            // Let everything else through.
            default => Limit::none(),
        };
    }
}

use App\Sentry\SentryThrottler;
use Webparking\SentryThrottling\Contracts\ThrottlesSentryReports;

public function register(): void
{
    $this->app->bind(ThrottlesSentryReports::class, SentryThrottler::class);
}

// One bucket for every instance of the exception:
Limit::perHour(1),

// One bucket per unique message / code:
Limit::perHour(1)->by($exception->getMessage()),
Limit::perMinute(5)->by($exception::class.':'.$exception->getCode()),