Skip to content

Sentry before_send callback not executed after running "php artisan optimize" #1069

@duykb2015

Description

@duykb2015

How do you use Sentry?

Sentry SaaS (sentry.io)

SDK version

Laravel SDK Version 4.19.0 PHP SDK Version 4.18.1

Laravel version

11.46.1

Steps to reproduce

  1. Install Laravel and Sentry
    composer require sentry/sentry-laravel

  2. Set a non-closure callback for before_send in config/sentry.php
    'before_send' => ['App\Helpers\Sentry', 'beforeCall']

  3. Create the callback class with strict type hints

<?php

namespace App\Helpers;

class Sentry
{
    /**
     * Create a new class instance.
     */
    public static function beforeCall(?\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event
    {
        dd(1);
        if ($hint === null) {
            return $event;
        }

        if (!isset($hint->exception) || !method_exists($hint->exception, 'getTrace')) {
            return $event;
        }

        $isJob = false;
        foreach ($hint->exception->getTrace() as $trace) {
            $isJob = isset($trace['class']) && str_contains($trace['class'], 'App\\Jobs\\');
            if ($isJob) {
                break;
            }
        }

        // If it's a job, sample ~10%
        if ($isJob && mt_rand(1, 100) > env('SENTRY_JOB_SAMPLE_RATE', 0.1) * 100) {
            return null;
        }

        return $event;
    }
}
Image
  1. Trigger any exception
    → Sentry receives the event correctly.
Image
  1. Run Laravel optimization
    php artisan optimize
Image
  1. Trigger the same exception again
    → Sentry does not send the event anymore.
    → The before_send callback is silently skipped.
    (bypasses the beforeCall callback at step 4 and shows the exception directly)
Image
  1. Clear optimization
    php artisan optimize:clear
    → Sentry starts working normally again.
Image Image

Expected result

The before_send callback should execute normally even when configuration is cached.
Running php artisan optimize should not disable or bypass the callback.

Actual result

After running php artisan optimize, the callback is not executed.
No events are sent to Sentry.
Clearing the optimization cache restores normal behavior.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions