diff --git a/src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php b/src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php index 31e8165a..9830a23d 100644 --- a/src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php +++ b/src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php @@ -74,8 +74,8 @@ public function register(): void ?int $recoveryThreshold = null ) use ($startCheckIn, $finishCheckIn) { /** @var SchedulingEvent $this */ - if ($monitorSlug === null && $this->command === null) { - throw new RuntimeException('The command string is null, please set a slug manually for this scheduled command using the `sentryMonitor(\'your-monitor-slug\')` macro.'); + if ($monitorSlug === null && empty($this->command) && empty($this->description)) { + throw new RuntimeException('The command and description are not set, please set a slug manually for this scheduled command using the `sentryMonitor(\'your-monitor-slug\')` macro.'); } return $this @@ -266,7 +266,7 @@ private function createCheckIn(string $slug, CheckInStatus $status, ?string $id $options = SentrySdk::getCurrentHub()->getClient()->getOptions(); return new CheckIn( - $slug, + Str::limit($slug, 128, ''), $status, $id, $options->getRelease(), @@ -282,17 +282,28 @@ private function buildCacheKey(string $mutex, string $slug): string private function makeSlugForScheduled(SchedulingEvent $scheduled): string { - $generatedSlug = Str::slug( - str_replace( - // `:` is commonly used in the command name, so we replace it with `-` to avoid it being stripped out by the slug function - ':', - '-', - trim( - // The command string always starts with the PHP binary, so we remove it since it's not relevant to the slug - Str::after($scheduled->command, ConsoleApplication::phpBinary()) + if (empty($scheduled->command)) { + if (!empty($scheduled->description) && class_exists($scheduled->description)) { + $generatedSlug = Str::slug( + // We reverse the class name to have the class name at the start of the slug instead of at the end (and possibly cut off) + implode('_', array_reverse(explode('\\', $scheduled->description))) + ); + } else { + $generatedSlug = Str::slug($scheduled->description); + } + } else { + $generatedSlug = Str::slug( + str_replace( + // `:` is commonly used in the command name, so we replace it with `-` to avoid it being stripped out by the slug function + ':', + '-', + trim( + // The command string always starts with the PHP binary, so we remove it since it's not relevant to the slug + Str::after($scheduled->command, ConsoleApplication::phpBinary()) + ) ) - ) - ); + ); + } return "scheduled_{$generatedSlug}"; } diff --git a/test/Sentry/Features/ConsoleSchedulingIntegrationTest.php b/test/Sentry/Features/ConsoleSchedulingIntegrationTest.php index f0bc8c0a..1dddde6d 100644 --- a/test/Sentry/Features/ConsoleSchedulingIntegrationTest.php +++ b/test/Sentry/Features/ConsoleSchedulingIntegrationTest.php @@ -60,7 +60,7 @@ public function testScheduleMacroWithTimeZone(): void $this->assertEquals($expectedTimezone, $finishCheckInEvent->getCheckIn()->getMonitorConfig()->getTimezone()); } - public function testScheduleMacroAutomaticSlug(): void + public function testScheduleMacroAutomaticSlugForCommand(): void { /** @var Event $scheduledEvent */ $scheduledEvent = $this->getScheduler()->command('inspire')->sentryMonitor(); @@ -78,7 +78,26 @@ public function testScheduleMacroAutomaticSlug(): void $this->assertEquals('scheduled_artisan-inspire', $finishCheckInEvent->getCheckIn()->getMonitorSlug()); } - public function testScheduleMacroWithoutSlugOrCommandName(): void + public function testScheduleMacroAutomaticSlugForJob(): void + { + /** @var Event $scheduledEvent */ + $scheduledEvent = $this->getScheduler()->job(ScheduledQueuedJob::class)->sentryMonitor(); + + $scheduledEvent->run($this->app); + + // We expect a total of 2 events to be sent to Sentry: + // 1. The start check-in event + // 2. The finish check-in event + $this->assertSentryCheckInCount(2); + + $finishCheckInEvent = $this->getLastSentryEvent(); + + $this->assertNotNull($finishCheckInEvent->getCheckIn()); + // Scheduled is duplicated here because of the class name of the queued job, this is not a bug just unfortunate naming for the test class + $this->assertEquals('scheduled_scheduledqueuedjob-features-tests-laravel-sentry', $finishCheckInEvent->getCheckIn()->getMonitorSlug()); + } + + public function testScheduleMacroWithoutSlugCommandOrDescriptionOrName(): void { $this->expectException(RuntimeException::class);