Skip to content

Commit 9d7a34b

Browse files
authored
Try harder to generate a slug for a cron monitor (#977)
1 parent 51225a7 commit 9d7a34b

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/Sentry/Laravel/Features/ConsoleSchedulingIntegration.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public function register(): void
7474
?int $recoveryThreshold = null
7575
) use ($startCheckIn, $finishCheckIn) {
7676
/** @var SchedulingEvent $this */
77-
if ($monitorSlug === null && $this->command === null) {
78-
throw new RuntimeException('The command string is null, please set a slug manually for this scheduled command using the `sentryMonitor(\'your-monitor-slug\')` macro.');
77+
if ($monitorSlug === null && empty($this->command) && empty($this->description)) {
78+
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.');
7979
}
8080

8181
return $this
@@ -266,7 +266,7 @@ private function createCheckIn(string $slug, CheckInStatus $status, ?string $id
266266
$options = SentrySdk::getCurrentHub()->getClient()->getOptions();
267267

268268
return new CheckIn(
269-
$slug,
269+
Str::limit($slug, 128, ''),
270270
$status,
271271
$id,
272272
$options->getRelease(),
@@ -282,17 +282,28 @@ private function buildCacheKey(string $mutex, string $slug): string
282282

283283
private function makeSlugForScheduled(SchedulingEvent $scheduled): string
284284
{
285-
$generatedSlug = Str::slug(
286-
str_replace(
287-
// `:` is commonly used in the command name, so we replace it with `-` to avoid it being stripped out by the slug function
288-
':',
289-
'-',
290-
trim(
291-
// The command string always starts with the PHP binary, so we remove it since it's not relevant to the slug
292-
Str::after($scheduled->command, ConsoleApplication::phpBinary())
285+
if (empty($scheduled->command)) {
286+
if (!empty($scheduled->description) && class_exists($scheduled->description)) {
287+
$generatedSlug = Str::slug(
288+
// 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)
289+
implode('_', array_reverse(explode('\\', $scheduled->description)))
290+
);
291+
} else {
292+
$generatedSlug = Str::slug($scheduled->description);
293+
}
294+
} else {
295+
$generatedSlug = Str::slug(
296+
str_replace(
297+
// `:` is commonly used in the command name, so we replace it with `-` to avoid it being stripped out by the slug function
298+
':',
299+
'-',
300+
trim(
301+
// The command string always starts with the PHP binary, so we remove it since it's not relevant to the slug
302+
Str::after($scheduled->command, ConsoleApplication::phpBinary())
303+
)
293304
)
294-
)
295-
);
305+
);
306+
}
296307

297308
return "scheduled_{$generatedSlug}";
298309
}

test/Sentry/Features/ConsoleSchedulingIntegrationTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testScheduleMacroWithTimeZone(): void
6060
$this->assertEquals($expectedTimezone, $finishCheckInEvent->getCheckIn()->getMonitorConfig()->getTimezone());
6161
}
6262

63-
public function testScheduleMacroAutomaticSlug(): void
63+
public function testScheduleMacroAutomaticSlugForCommand(): void
6464
{
6565
/** @var Event $scheduledEvent */
6666
$scheduledEvent = $this->getScheduler()->command('inspire')->sentryMonitor();
@@ -78,7 +78,26 @@ public function testScheduleMacroAutomaticSlug(): void
7878
$this->assertEquals('scheduled_artisan-inspire', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
7979
}
8080

81-
public function testScheduleMacroWithoutSlugOrCommandName(): void
81+
public function testScheduleMacroAutomaticSlugForJob(): void
82+
{
83+
/** @var Event $scheduledEvent */
84+
$scheduledEvent = $this->getScheduler()->job(ScheduledQueuedJob::class)->sentryMonitor();
85+
86+
$scheduledEvent->run($this->app);
87+
88+
// We expect a total of 2 events to be sent to Sentry:
89+
// 1. The start check-in event
90+
// 2. The finish check-in event
91+
$this->assertSentryCheckInCount(2);
92+
93+
$finishCheckInEvent = $this->getLastSentryEvent();
94+
95+
$this->assertNotNull($finishCheckInEvent->getCheckIn());
96+
// 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
97+
$this->assertEquals('scheduled_scheduledqueuedjob-features-tests-laravel-sentry', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
98+
}
99+
100+
public function testScheduleMacroWithoutSlugCommandOrDescriptionOrName(): void
82101
{
83102
$this->expectException(RuntimeException::class);
84103

0 commit comments

Comments
 (0)