Skip to content

Commit

Permalink
Adjust wording and add Cron Monitor upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
will2877 committed Jul 25, 2023
1 parent 5ed136f commit 32c1f61
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 72 deletions.
15 changes: 0 additions & 15 deletions src/Cron/CronJobFactoryInterface.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Cron/CronJobInterface.php

This file was deleted.

24 changes: 8 additions & 16 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
use Sentry\Integration\RequestFetcherInterface;
use Sentry\Integration\RequestIntegration;
use Sentry\Options;
use Sentry\SentryBundle\Cron\CronJobFactory;
use Sentry\SentryBundle\Cron\CronJobFactoryInterface;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\ErrorListener;
use Sentry\SentryBundle\EventListener\MessengerListener;
use Sentry\SentryBundle\EventListener\TracingConsoleListener;
use Sentry\SentryBundle\EventListener\TracingRequestListener;
use Sentry\SentryBundle\EventListener\TracingSubRequestListener;
use Sentry\SentryBundle\Integration\IntegrationConfigurator;
use Sentry\SentryBundle\Monitor\MonitorFactory;
use Sentry\SentryBundle\Monitor\MonitorFactoryInterface;
use Sentry\SentryBundle\SentryBundle;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\ConnectionConfigurator;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware;
Expand All @@ -43,25 +43,17 @@

final class SentryExtension extends ConfigurableExtension
{
/**
* {@inheritdoc}
*/
public function getXsdValidationBasePath(): string
{
return __DIR__ . '/../Resources/config/schema';
}

/**
* {@inheritdoc}
*/
public function getNamespace(): string
{
return 'https://sentry.io/schema/dic/sentry-symfony';
}

/**
* {@inheritdoc}
*
* @param array<array-key, mixed> $mergedConfig
*/
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
Expand Down Expand Up @@ -157,22 +149,22 @@ private function registerConfiguration(ContainerBuilder $container, array $confi
->setPublic(false)
->setFactory([$clientBuilderDefinition, 'getClient']);

// Setup Cronjob.
// Setup Monitors.
$environment = '';
if (isset($options['environment'])) {
if (\is_array($options) && isset($options['environment'])) {
$environment = $options['environment'];
}

$release = null;
if (isset($options['release'])) {
if (\is_array($options) && isset($options['release'])) {
$release = $options['release'];
}

$container
->register(CronJobFactoryInterface::class, CronJobFactory::class)
->register(MonitorFactoryInterface::class, MonitorFactory::class)
->setPublic(true)
->setArgument(0, $environment)
->setArgument(1, $release);
->setArguments([$environment, $release])
;
}

/**
Expand Down
46 changes: 32 additions & 14 deletions src/Cron/CronJob.php → src/Monitor/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

declare(strict_types=1);

namespace Sentry\SentryBundle\Cron;
namespace Sentry\SentryBundle\Monitor;

use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Sentry\Event;
use Sentry\MonitorConfig;
use Sentry\State\HubInterface;

class CronJob implements CronJobInterface
class Monitor implements MonitorInterface
{
/**
* @var HubInterface
*/
private $hub;

/**
* @var MonitorConfig
*/
private $monitorConfig;
/**
* @var string
*/
Expand All @@ -19,64 +29,72 @@ class CronJob implements CronJobInterface
* @var string
*/
private $slug;
/**
* @var HubInterface
*/
private $hub;
/**
* @var string|null
*/
private $release;

public function __construct(HubInterface $hub, string $slug, string $environment, ?string $release = null)
{
public function __construct(
HubInterface $hub,
MonitorConfig $monitorConfig,
string $slug,
string $environment,
string $release = null
) {
$this->monitorConfig = $monitorConfig;
$this->environment = $environment;
$this->slug = $slug;
$this->hub = $hub;
$this->release = $release;
}

public function inProgress(?CheckIn $previous = null): CheckIn
public function inProgress(CheckIn $previous = null): CheckIn
{
$event = Event::createCheckIn();
$checkIn = new CheckIn(
$this->slug,
CheckInStatus::inProgress(),
$previous ? $previous->getId() : null,
$this->release,
$this->environment
$this->environment,
null,
$this->monitorConfig
);
$event->setCheckIn($checkIn);
$this->hub->captureEvent($event);

return $checkIn;
}

public function error(?CheckIn $previous = null): CheckIn
public function error(CheckIn $previous = null): CheckIn
{
$event = Event::createCheckIn();
$checkIn = new CheckIn(
$this->slug,
CheckInStatus::error(),
$previous ? $previous->getId() : null,
$this->release,
$this->environment
$this->environment,
null,
$this->monitorConfig
);
$event->setCheckIn($checkIn);
$this->hub->captureEvent($event);

return $checkIn;
}

public function ok(?CheckIn $previous = null): CheckIn
public function ok(CheckIn $previous = null): CheckIn
{
$event = Event::createCheckIn();
$checkIn = new CheckIn(
$this->slug,
CheckInStatus::ok(),
$previous ? $previous->getId() : null,
$this->release,
$this->environment
$this->environment,
null,
$this->monitorConfig
);
$event->setCheckIn($checkIn);
$this->hub->captureEvent($event);
Expand Down
15 changes: 7 additions & 8 deletions src/Cron/CronJobFactory.php → src/Monitor/MonitorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

declare(strict_types=1);

namespace Sentry\SentryBundle\Cron;
namespace Sentry\SentryBundle\Monitor;

use Sentry\MonitorConfig;
use Sentry\SentrySdk;

class CronJobFactory implements CronJobFactoryInterface
class MonitorFactory implements MonitorFactoryInterface
{
/**
* @var string
Expand All @@ -20,18 +21,16 @@ class CronJobFactory implements CronJobFactoryInterface
/**
* @param string $environment the configured environment
*/
public function __construct(string $environment, ?string $release = null)
public function __construct(string $environment, string $release = null)
{
$this->environment = $environment;
$this->release = $release;
}

/**
* {@inheritdoc}
*/
public function getCronJob(string $slug): CronJobInterface
public function getMonitor(string $slug, MonitorConfig $monitorConfig): MonitorInterface
{
$hub = SentrySdk::getCurrentHub();
return new CronJob($hub, $slug, $this->environment, $this->release);

return new Monitor($hub, $monitorConfig, $slug, $this->environment, $this->release);
}
}
17 changes: 17 additions & 0 deletions src/Monitor/MonitorFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Monitor;

use Sentry\MonitorConfig;

interface MonitorFactoryInterface
{
/**
* @param MonitorConfig $monitorConfig the monitor configuration
*
* @return MonitorInterface the monitor
*/
public function getMonitor(string $slug, MonitorConfig $monitorConfig): MonitorInterface;
}
16 changes: 16 additions & 0 deletions src/Monitor/MonitorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Monitor;

use Sentry\CheckIn;

interface MonitorInterface
{
public function inProgress(CheckIn $previous = null): CheckIn;

public function error(CheckIn $previous = null): CheckIn;

public function ok(CheckIn $previous = null): CheckIn;
}
48 changes: 41 additions & 7 deletions tests/Cron/CronJobFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\Cron;

use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Cron\CronJobFactory;
use Sentry\SentryBundle\Cron\CronJobInterface;
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Sentry\MonitorScheduleUnit;
use Sentry\SentryBundle\Monitor\MonitorFactory;
use Sentry\SentryBundle\Monitor\MonitorInterface;

class CronJobFactoryTest extends TestCase
{
public function testCronJob(): void
{
// Setup test
$factory = new CronJobFactory('test', 'test-release');
$cronjob = $factory->getCronjob('test-monitor');
$this->assertInstanceOf(CronJobInterface::class, $cronjob);
$factory = new MonitorFactory('test', 'test-release');
$monitorConfig = new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
);
$cronJob = $factory->getMonitor('test-cronjob', $monitorConfig);
$this->assertInstanceOf(MonitorInterface::class, $cronJob);

// Create a CheckIn
$checkIn = $cronJob->inProgress();
$this->assertEquals('test', $checkIn->getEnvironment());
$this->assertEquals('test-release', $checkIn->getRelease());
$this->assertEquals('test-cronjob', $checkIn->getMonitorSlug());
}

public function testInterval(): void
{
// Setup test
$factory = new MonitorFactory('test', 'test-release');
$monitorConfig = new MonitorConfig(
MonitorSchedule::interval(
30,
MonitorScheduleUnit::minute()
),
5,
30,
'UTC'
);
$interval = $factory->getMonitor('test-interval', $monitorConfig);
$this->assertInstanceOf(MonitorInterface::class, $interval);

// Create a CheckIn
$checkIn = $cronjob->inProgress();
$checkIn = $interval->inProgress();
$this->assertEquals('test', $checkIn->getEnvironment());
$this->assertEquals('test-release', $checkIn->getRelease());
$this->assertEquals('test-monitor', $checkIn->getMonitorSlug());
$this->assertEquals('test-interval', $checkIn->getMonitorSlug());
}
}

0 comments on commit 32c1f61

Please sign in to comment.