getsentry / sentry-laravel

The official Laravel SDK for Sentry (sentry.io)
https://sentry.io
MIT License
1.25k stars 189 forks source link

Cron monitors not working #805

Closed gregory-claeyssens closed 10 months ago

gregory-claeyssens commented 11 months ago

How do you use Sentry?

Sentry SaaS (sentry.io)

SDK version

4.0.0

Steps to reproduce

Following the documentation to the letter by adding ->sentryMonitor() Example:

$schedule->command('cache:prune-stale-tags')->hourly()->sentryMonitor();

Expected result

It simply work

Actual result

An error is logged in Sentry Method Illuminate\Console\Scheduling\Event::sentryMonitor does not exist.

cleptric commented 11 months ago

We need a bit more info, please, like the Laravel version. Is anything custom in terms of SDK setup? Anything else you can think of?

rzv-me commented 10 months ago

I got the same error.

We are using Laravel Vapor. The problem, from what I understand from the code, is that Laravel Vapor instantiates the console kernel $app->make(ConsoleKernelContract::class)->call('config:cache'); in vendor/laravel/vapor-core/stubs/fpmRuntime.php, but the isApplicable() function on ConsoleIntegration, returns true only when running in console.

My solution right now is to add

    protected function addSentryMonitorMacro()
    {
        if(!SchedulingEvent::hasMacro('sentryMonitor')) {
            SchedulingEvent::macro('sentryMonitor', function (
                ?string $monitorSlug = null,
                ?int $checkInMargin = null,
                ?int $maxRuntime = null,
                bool $updateMonitorConfig = true
            ) {
                return $this;
            });
        }
    }

to the Console Kernel and to run it at the beginning of the schedule() method: $this->addSentryMonitorMacro();

I think the same issue would happen if you try to run any artisan command, while not running in the console


later edit:

The solution that fixed this and that can be implemented easily in sentry laravel, is to always load the empty macro. This doesn't affect the functionality, as the macros are overwritten later. This is the updated constructor for ConsoleIntegration

    public function __construct(Container $container)
    {
        parent::__construct($container);
        $this->onBootInactive();
    }
cleptric commented 10 months ago

@stayallive can you take a look please?

stayallive commented 10 months ago

Interesting, I'll take a look this seems like something we can fix. At least the Vapor case, unsure if the OP has the same issue but we might solve it anyway. Thanks for the detailed report!

rzv-me commented 10 months ago

I submitted a PR for this, the better solution I think is to do it in the register() method of ConsoleIntegration.

As I wrote in the PR, you can replicate this easily by doing Artisan::call('inspire'); in a controller.

cleptric commented 10 months ago

Fixed in 4.1.1