Skip to content

Commit

Permalink
feat: Added sentryMonitor macro support for specifying environments t…
Browse files Browse the repository at this point in the history
…o run in
  • Loading branch information
Braunson committed Dec 8, 2023
1 parent c7d0b3b commit 71b1b2d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ composer phpstan
The code is automatically formatted through [php-cs-fixer](https://cs.symfony.com).

```bash
composer phpcs
composer cs-fix
```

**Note**: there is a dry run option `composer cs-check`

## Releasing a new version

(only relevant for Sentry employees)
Expand Down
12 changes: 9 additions & 3 deletions src/Sentry/Laravel/Features/ConsoleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class ConsoleIntegration extends Feature
{
/**
* @var array<string, CheckIn> The list of checkins that are currently in progress.
* @var array<string, CheckIn> The list of check-ins that are currently in progress.
*/
private $checkInStore = [];

Expand Down Expand Up @@ -48,8 +48,13 @@ public function onBoot(Cache $cache): void
?string $monitorSlug = null,
?int $checkInMargin = null,
?int $maxRuntime = null,
bool $updateMonitorConfig = true
bool $updateMonitorConfig = true,
array $runOnEnvironments = []
) use ($startCheckIn, $finishCheckIn) {
if (! empty($runOnEnvironments) && ! in_array(config('app.env'), $runOnEnvironments)) {
return $this;
}

/** @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.');
Expand Down Expand Up @@ -78,7 +83,8 @@ public function onBootInactive(): void
?string $monitorSlug = null,
?int $checkInMargin = null,
?int $maxRuntime = null,
bool $updateMonitorConfig = true
bool $updateMonitorConfig = true,
array $runOnEnvironments = []
) {
return $this;
});
Expand Down
63 changes: 63 additions & 0 deletions test/Sentry/Features/ConsoleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,69 @@ public function testScheduleMacroWithoutSlugOrCommandName(): void
$this->getScheduler()->call(function () {})->sentryMonitor();
}

public function testScheduledMacroWithSpecifiedEnvironmentsMatching(): void
{
$this->app['config']->set('app.env', 'production');

/** @var Event $scheduledEvent */
$scheduledEvent = $this->getScheduler()
->call(function () {})
->sentryMonitor('test-monitor', null, null, true, ['production']);

$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());
$this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
}

public function testScheduledMacroWithSpecifiedEnvironmentsNotMatching(): void
{
$this->app['config']->set('app.env', 'production');

/** @var Event $scheduledEvent */
$scheduledEvent = $this->getScheduler()
->call(function () {})
->sentryMonitor('test-monitor', null, null, true, ['staging']);

$scheduledEvent->run($this->app);

// We expect a total of 0 events to be sent to Sentry
$this->assertSentryCheckInCount(0);

$finishCheckInEvent = $this->getLastSentryEvent();

$this->assertNull($finishCheckInEvent);
}

public function testScheduledMacroWithSpecifiedEnvironmentsEmpty(): void
{
$this->app['config']->set('app.env', 'production');

/** @var Event $scheduledEvent */
$scheduledEvent = $this->getScheduler()
->call(function () {})
->sentryMonitor('test-monitor', null, null, true, []);

$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());
$this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
}

/** @define-env envWithoutDsnSet */
public function testScheduleMacroWithoutDsnSet(): void
{
Expand Down

0 comments on commit 71b1b2d

Please sign in to comment.