Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preconfigutred Cron Services #719

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<array-key, mixed> $mergedConfig
*/
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void
Expand Down Expand Up @@ -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 = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An event should always have an environment set and defaults to production. See https://github.com/getsentry/sentry-php/blob/master/src/Event.php#L19

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])
;
}

/**
Expand Down
104 changes: 104 additions & 0 deletions src/Monitor/Monitor.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class seems to create a CheckIn, not a Monitor.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Monitor;

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

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

/**
* @var MonitorConfig
*/
private $monitorConfig;
/**
* @var string
*/
private $environment;
/**
* @var string
*/
private $slug;
/**
* @var string|null
*/
private $release;

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
{
$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;
}
}
36 changes: 36 additions & 0 deletions src/Monitor/MonitorFactory.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this is about a CheckIn.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Monitor;

use Sentry\MonitorConfig;
use Sentry\SentrySdk;

class MonitorFactory implements MonitorFactoryInterface
{
/**
* @var string
*/
private $environment;
/**
* @var string|null
*/
private $release;

/**
* @param string $environment the configured environment
*/
public function __construct(string $environment, string $release = null)
{
$this->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);
}
}
17 changes: 17 additions & 0 deletions src/Monitor/MonitorFactoryInterface.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same naming issuse

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same naming issuse

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;
}
58 changes: 58 additions & 0 deletions tests/Cron/CronJobFactoryTest.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You likely want to rename the class and file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\Cron;

use PHPUnit\Framework\TestCase;
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 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 = $interval->inProgress();
$this->assertEquals('test', $checkIn->getEnvironment());
$this->assertEquals('test-release', $checkIn->getRelease());
$this->assertEquals('test-interval', $checkIn->getMonitorSlug());
}
}