Skip to content

Commit

Permalink
Add a switch configuration option for Cron monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
summerKK committed Jan 23, 2024
1 parent a2b3932 commit 1836f0d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@
'default_integrations' => env('SENTRY_TRACE_DEFAULT_INTEGRATIONS_ENABLED', true),
],

// Crons monitoring specific configuration
'crons' => [
// Enable monitoring of scheduled tasks (crons)
'monitor_enabled' => env('SENTRY_CRONS_MONITOR_ENABLED', true),
],
];
4 changes: 2 additions & 2 deletions src/Sentry/Laravel/Features/ConsoleIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public function register(): void
{
$this->onBootInactive();
}

public function isApplicable(): bool
{
return $this->container()->make(Application::class)->runningInConsole();
return $this->container()->make(Application::class)->runningInConsole() && $this->isCronsFeatureEnabled('monitor_enabled');
}

public function onBoot(Cache $cache): void
Expand Down
19 changes: 19 additions & 0 deletions src/Sentry/Laravel/Features/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ abstract class Feature
*/
private $isBreadcrumbFeatureEnabled = [];

/**
* In-memory cache for the crons feature flag.
*
* @var array<string, bool>
*/
private $isCronsFeatureEnabled = [];

/**
* @param Container $container The Laravel application container.
*/
Expand Down Expand Up @@ -145,6 +152,18 @@ protected function isBreadcrumbFeatureEnabled(string $feature, bool $default = t
return $this->isBreadcrumbFeatureEnabled[$feature];
}

/**
* Indicates if the given feature is enabled for Crons.
*/
protected function isCronsFeatureEnabled(string $feature, bool $default = true): bool
{
if (!array_key_exists($feature, $this->isCronsFeatureEnabled)) {
$this->isCronsFeatureEnabled[$feature] = $this->isFeatureEnabled('crons', $feature, $default);
}

return $this->isCronsFeatureEnabled[$feature];
}

/**
* Helper to test if a certain feature is enabled in the user config.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ServiceProvider extends BaseServiceProvider
// We do not want these settings to hit the PHP SDK because they are Laravel specific and the PHP SDK will throw errors
'tracing',
'breadcrumbs',
'crons',
// We resolve the integrations through the container later, so we initially do not pass it to the SDK yet
'integrations',
// This is kept for backwards compatibility and can be dropped in a future breaking release
Expand Down
40 changes: 40 additions & 0 deletions test/Sentry/Features/ConsoleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@

class ConsoleIntegrationTest extends TestCase
{
public function testScheduleMonitorEnabled(): void
{
$this->resetApplicationWithConfig([
'sentry.crons.monitor_enabled' => true,
]);

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

$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 testScheduleMonitorDisabled(): void
{
$this->resetApplicationWithConfig([
'sentry.crons.monitor_enabled' => false,
]);

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

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

$this->assertSentryCheckInCount(0);
}

public function testScheduleMacro(): void
{
/** @var Event $scheduledEvent */
Expand Down

0 comments on commit 1836f0d

Please sign in to comment.