From d9a50eed663155bf33b126f46987d018cc76002e Mon Sep 17 00:00:00 2001 From: William Ihde Date: Tue, 9 May 2023 07:45:13 +0200 Subject: [PATCH 1/6] Add interfaces and service implenetations. --- src/Cron/CronJob.php | 86 ++++++++++++++++++++++++++++ src/Cron/CronJobFactory.php | 37 ++++++++++++ src/Cron/CronJobFactoryInterface.php | 15 +++++ src/Cron/CronJobInterface.php | 12 ++++ 4 files changed, 150 insertions(+) create mode 100644 src/Cron/CronJob.php create mode 100644 src/Cron/CronJobFactory.php create mode 100644 src/Cron/CronJobFactoryInterface.php create mode 100644 src/Cron/CronJobInterface.php diff --git a/src/Cron/CronJob.php b/src/Cron/CronJob.php new file mode 100644 index 00000000..fb217cce --- /dev/null +++ b/src/Cron/CronJob.php @@ -0,0 +1,86 @@ +environment = $environment; + $this->slug = $slug; + $this->hub = $hub; + $this->release = $release; + } + + 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 + ); + $event->setCheckIn($checkIn); + $this->hub->captureEvent($event); + + return $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 + ); + $event->setCheckIn($checkIn); + $this->hub->captureEvent($event); + + return $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 + ); + $event->setCheckIn($checkIn); + $this->hub->captureEvent($event); + + return $checkIn; + } +} diff --git a/src/Cron/CronJobFactory.php b/src/Cron/CronJobFactory.php new file mode 100644 index 00000000..bb6e2e30 --- /dev/null +++ b/src/Cron/CronJobFactory.php @@ -0,0 +1,37 @@ +environment = $environment; + $this->release = $release; + } + + /** + * {@inheritdoc} + */ + public function getCronJob(string $slug): CronJobInterface + { + $hub = SentrySdk::getCurrentHub(); + return new CronJob($hub, $slug, $this->environment, $this->release); + } +} diff --git a/src/Cron/CronJobFactoryInterface.php b/src/Cron/CronJobFactoryInterface.php new file mode 100644 index 00000000..1677e278 --- /dev/null +++ b/src/Cron/CronJobFactoryInterface.php @@ -0,0 +1,15 @@ + Date: Tue, 9 May 2023 08:00:24 +0200 Subject: [PATCH 2/6] Add test. --- tests/Cron/CronJobFactoryTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/Cron/CronJobFactoryTest.php diff --git a/tests/Cron/CronJobFactoryTest.php b/tests/Cron/CronJobFactoryTest.php new file mode 100644 index 00000000..3c9c3721 --- /dev/null +++ b/tests/Cron/CronJobFactoryTest.php @@ -0,0 +1,24 @@ +getCronjob('test-monitor'); + $this->assertInstanceOf(CronJobInterface::class, $cronjob); + + // Create a CheckIn + $checkIn = $cronjob->inProgress(); + $this->assertEquals('test', $checkIn->getEnvironment()); + $this->assertEquals('test-release', $checkIn->getRelease()); + $this->assertEquals('test-monitor', $checkIn->getMonitorSlug()); + } +} From da7d9615c5dc8ea74079810d9f8d886855a1800c Mon Sep 17 00:00:00 2001 From: William Ihde Date: Tue, 9 May 2023 12:34:52 +0200 Subject: [PATCH 3/6] Add missing return value to test. --- tests/Cron/CronJobFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Cron/CronJobFactoryTest.php b/tests/Cron/CronJobFactoryTest.php index 3c9c3721..c6ca0577 100644 --- a/tests/Cron/CronJobFactoryTest.php +++ b/tests/Cron/CronJobFactoryTest.php @@ -8,7 +8,7 @@ class CronJobFactoryTest extends TestCase { - public function testCronJob() + public function testCronJob(): void { // Setup test $factory = new CronJobFactory('test', 'test-release'); From 1c30d264858c1efddc39d6c78bf5b76ca1f4ca78 Mon Sep 17 00:00:00 2001 From: William Ihde Date: Tue, 9 May 2023 12:45:12 +0200 Subject: [PATCH 4/6] Register Service. --- src/DependencyInjection/SentryExtension.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 5e559a16..c5fd5974 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -13,6 +13,8 @@ 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; @@ -154,6 +156,23 @@ private function registerConfiguration(ContainerBuilder $container, array $confi ->setDefinition('sentry.client', new Definition(Client::class)) ->setPublic(false) ->setFactory([$clientBuilderDefinition, 'getClient']); + + // Setup Cronjob. + $environment = ''; + if (isset($options['environment'])) { + $environment = $options['environment']; + } + + $release = null; + if (isset($options['release'])) { + $release = $options['release']; + } + + $container + ->register(CronJobFactoryInterface::class, CronJobFactory::class) + ->setPublic(true) + ->setArgument(0, $environment) + ->setArgument(1, $release); } /** From 5ed136fba43f599448abfc19dfd7ba2173f3405e Mon Sep 17 00:00:00 2001 From: William Ihde Date: Tue, 9 May 2023 15:14:49 +0200 Subject: [PATCH 5/6] Make release optional. --- src/Cron/CronJobFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cron/CronJobFactory.php b/src/Cron/CronJobFactory.php index bb6e2e30..2541b557 100644 --- a/src/Cron/CronJobFactory.php +++ b/src/Cron/CronJobFactory.php @@ -13,14 +13,14 @@ class CronJobFactory implements CronJobFactoryInterface */ private $environment; /** - * @var string + * @var string|null */ private $release; /** * @param string $environment the configured environment */ - public function __construct(string $environment, string $release) + public function __construct(string $environment, ?string $release = null) { $this->environment = $environment; $this->release = $release; From 32c1f613dbb87cecb2ec63aad0b52fac052d5185 Mon Sep 17 00:00:00 2001 From: William Ihde Date: Tue, 25 Jul 2023 21:29:18 +0200 Subject: [PATCH 6/6] Adjust wording and add Cron Monitor upsert https://github.com/getsentry/sentry-php/pull/1511 --- src/Cron/CronJobFactoryInterface.php | 15 ------ src/Cron/CronJobInterface.php | 12 ----- src/DependencyInjection/SentryExtension.php | 24 ++++------ src/{Cron/CronJob.php => Monitor/Monitor.php} | 46 ++++++++++++------ .../MonitorFactory.php} | 15 +++--- src/Monitor/MonitorFactoryInterface.php | 17 +++++++ src/Monitor/MonitorInterface.php | 16 +++++++ tests/Cron/CronJobFactoryTest.php | 48 ++++++++++++++++--- 8 files changed, 121 insertions(+), 72 deletions(-) delete mode 100644 src/Cron/CronJobFactoryInterface.php delete mode 100644 src/Cron/CronJobInterface.php rename src/{Cron/CronJob.php => Monitor/Monitor.php} (64%) rename src/{Cron/CronJobFactory.php => Monitor/MonitorFactory.php} (51%) create mode 100644 src/Monitor/MonitorFactoryInterface.php create mode 100644 src/Monitor/MonitorInterface.php diff --git a/src/Cron/CronJobFactoryInterface.php b/src/Cron/CronJobFactoryInterface.php deleted file mode 100644 index 1677e278..00000000 --- a/src/Cron/CronJobFactoryInterface.php +++ /dev/null @@ -1,15 +0,0 @@ - $mergedConfig */ protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void @@ -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]) + ; } /** diff --git a/src/Cron/CronJob.php b/src/Monitor/Monitor.php similarity index 64% rename from src/Cron/CronJob.php rename to src/Monitor/Monitor.php index fb217cce..3e9ad970 100644 --- a/src/Cron/CronJob.php +++ b/src/Monitor/Monitor.php @@ -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 */ @@ -19,24 +29,26 @@ 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( @@ -44,7 +56,9 @@ public function inProgress(?CheckIn $previous = null): CheckIn CheckInStatus::inProgress(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); @@ -52,7 +66,7 @@ public function inProgress(?CheckIn $previous = null): CheckIn return $checkIn; } - public function error(?CheckIn $previous = null): CheckIn + public function error(CheckIn $previous = null): CheckIn { $event = Event::createCheckIn(); $checkIn = new CheckIn( @@ -60,7 +74,9 @@ public function error(?CheckIn $previous = null): CheckIn CheckInStatus::error(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); @@ -68,7 +84,7 @@ public function error(?CheckIn $previous = null): CheckIn return $checkIn; } - public function ok(?CheckIn $previous = null): CheckIn + public function ok(CheckIn $previous = null): CheckIn { $event = Event::createCheckIn(); $checkIn = new CheckIn( @@ -76,7 +92,9 @@ public function ok(?CheckIn $previous = null): CheckIn CheckInStatus::ok(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); diff --git a/src/Cron/CronJobFactory.php b/src/Monitor/MonitorFactory.php similarity index 51% rename from src/Cron/CronJobFactory.php rename to src/Monitor/MonitorFactory.php index 2541b557..1043ea7d 100644 --- a/src/Cron/CronJobFactory.php +++ b/src/Monitor/MonitorFactory.php @@ -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 @@ -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); } } diff --git a/src/Monitor/MonitorFactoryInterface.php b/src/Monitor/MonitorFactoryInterface.php new file mode 100644 index 00000000..d9a5f29d --- /dev/null +++ b/src/Monitor/MonitorFactoryInterface.php @@ -0,0 +1,17 @@ +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()); } }