diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 5e559a16..15185bdc 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -20,6 +20,8 @@ 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; @@ -41,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 $mergedConfig */ protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void @@ -154,6 +148,23 @@ private function registerConfiguration(ContainerBuilder $container, array $confi ->setDefinition('sentry.client', new Definition(Client::class)) ->setPublic(false) ->setFactory([$clientBuilderDefinition, 'getClient']); + + // Setup Monitors. + $environment = ''; + if (\is_array($options) && isset($options['environment'])) { + $environment = $options['environment']; + } + + $release = null; + if (\is_array($options) && isset($options['release'])) { + $release = $options['release']; + } + + $container + ->register(MonitorFactoryInterface::class, MonitorFactory::class) + ->setPublic(true) + ->setArguments([$environment, $release]) + ; } /** diff --git a/src/Monitor/Monitor.php b/src/Monitor/Monitor.php new file mode 100644 index 00000000..3e9ad970 --- /dev/null +++ b/src/Monitor/Monitor.php @@ -0,0 +1,104 @@ +monitorConfig = $monitorConfig; + $this->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, + null, + $this->monitorConfig + ); + $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, + null, + $this->monitorConfig + ); + $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, + null, + $this->monitorConfig + ); + $event->setCheckIn($checkIn); + $this->hub->captureEvent($event); + + return $checkIn; + } +} diff --git a/src/Monitor/MonitorFactory.php b/src/Monitor/MonitorFactory.php new file mode 100644 index 00000000..1043ea7d --- /dev/null +++ b/src/Monitor/MonitorFactory.php @@ -0,0 +1,36 @@ +environment = $environment; + $this->release = $release; + } + + public function getMonitor(string $slug, MonitorConfig $monitorConfig): MonitorInterface + { + $hub = SentrySdk::getCurrentHub(); + + 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 @@ +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 = $interval->inProgress(); + $this->assertEquals('test', $checkIn->getEnvironment()); + $this->assertEquals('test-release', $checkIn->getRelease()); + $this->assertEquals('test-interval', $checkIn->getMonitorSlug()); + } +}