Skip to content

Commit

Permalink
feat: Cron Monitor upsert (#1511)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Bouma <alex@bouma.me>
  • Loading branch information
cleptric and stayallive authored Jun 13, 2023
1 parent 6b17289 commit d907d27
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/CheckIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class CheckIn
*/
private $duration;

/**
* @var \Sentry\MonitorConfig|null The monitor configuration
*/
private $monitorConfig;

/**
* @param int|float|null $duration The duration of the check-in in seconds
*/
Expand All @@ -47,7 +52,8 @@ public function __construct(
string $id = null,
?string $release = null,
?string $environment = null,
$duration = null
$duration = null,
?MonitorConfig $monitorConfig = null
) {
$this->setMonitorSlug($monitorSlug);
$this->setStatus($status);
Expand All @@ -56,6 +62,7 @@ public function __construct(
$this->setRelease($release ?? '');
$this->setEnvironment($environment ?? Event::DEFAULT_ENVIRONMENT);
$this->setDuration($duration);
$this->setMonitorConfig($monitorConfig);
}

public function getId(): string
Expand Down Expand Up @@ -123,4 +130,14 @@ public function setDuration($duration): void
{
$this->duration = $duration;
}

public function getMonitorConfig(): ?MonitorConfig
{
return $this->monitorConfig;
}

public function setMonitorConfig(?MonitorConfig $monitorConfig): void
{
$this->monitorConfig = $monitorConfig;
}
}
101 changes: 101 additions & 0 deletions src/MonitorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorConfig
{
/**
* @var MonitorSchedule The schedule of the monitor
*/
private $schedule;

/**
* @var int|null The check-in margin in seconds
*/
private $checkinMargin;

/**
* @var int|null The maximum runtime in seconds
*/
private $maxRuntime;

/**
* @var string|null The timezone
*/
private $timezone;

public function __construct(
MonitorSchedule $schedule,
?int $checkinMargin = null,
?int $maxRuntime = null,
?string $timezone = null
) {
$this->schedule = $schedule;
$this->checkinMargin = $checkinMargin;
$this->maxRuntime = $maxRuntime;
$this->timezone = $timezone;
}

public function getSchedule(): MonitorSchedule
{
return $this->schedule;
}

public function setSchedule(MonitorSchedule $schedule): self
{
$this->schedule = $schedule;

return $this;
}

public function getCheckinMargin(): ?int
{
return $this->checkinMargin;
}

public function setCheckinMargin(?int $checkinMargin): self
{
$this->checkinMargin = $checkinMargin;

return $this;
}

public function getMaxRuntime(): ?int
{
return $this->maxRuntime;
}

public function setMaxRuntime(?int $maxRuntime): self
{
$this->maxRuntime = $maxRuntime;

return $this;
}

public function getTimezone(): ?string
{
return $this->timezone;
}

public function setTimezone(?string $timezone): self
{
$this->timezone = $timezone;

return $this;
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'schedule' => $this->schedule->toArray(),
'checkin_margin' => $this->checkinMargin,
'max_runtime' => $this->maxRuntime,
'timezone' => $this->timezone,
];
}
}
106 changes: 106 additions & 0 deletions src/MonitorSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorSchedule
{
/**
* @var string The type of the schedule
*/
private $type;

/**
* @var string|int The value of the schedule
*/
private $value;

/**
* @var MonitorScheduleUnit|null The unit of the schedule
*/
private $unit;

public const TYPE_CRONTAB = 'crontab';

public const TYPE_INTERVAL = 'interval';

/**
* @param string $type The type of the schedule
* @param string|int $value The value of the schedule
* @param MonitorScheduleUnit|null $unit The unit of the schedule
*/
public function __construct(
string $type,
$value,
?MonitorScheduleUnit $unit = null
) {
$this->type = $type;
$this->value = $value;
$this->unit = $unit;
}

public static function crontab(string $value): self
{
return new self(self::TYPE_CRONTAB, $value);
}

public static function interval(int $value, MonitorScheduleUnit $unit): self
{
return new self(self::TYPE_INTERVAL, $value, $unit);
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): self
{
$this->type = $type;

return $this;
}

/**
* @return string|int
*/
public function getValue()
{
return $this->value;
}

/**
* @param string|int $value
*/
public function setValue($value): self
{
$this->value = $value;

return $this;
}

public function getUnit(): ?MonitorScheduleUnit
{
return $this->unit;
}

public function setUnit(?MonitorScheduleUnit $unit): self
{
$this->unit = $unit;

return $this;
}

/**
* @return array<string, string|int>
*/
public function toArray(): array
{
return [
'type' => $this->type,
'value' => $this->value,
'unit' => (string) $this->unit,
];
}
}
67 changes: 67 additions & 0 deletions src/MonitorScheduleUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Sentry;

final class MonitorScheduleUnit implements \Stringable
{
/**
* @var string The value of the enum instance
*/
private $value;

/**
* @var array<string, self> A list of cached enum instances
*/
private static $instances = [];

private function __construct(string $value)
{
$this->value = $value;
}

public static function minute(): self
{
return self::getInstance('minute');
}

public static function hour(): self
{
return self::getInstance('hour');
}

public static function day(): self
{
return self::getInstance('day');
}

public static function week(): self
{
return self::getInstance('week');
}

public static function month(): self
{
return self::getInstance('month');
}

public static function year(): self
{
return self::getInstance('year');
}

public function __toString(): string
{
return $this->value;
}

private static function getInstance(string $value): self
{
if (!isset(self::$instances[$value])) {
self::$instances[$value] = new self($value);
}

return self::$instances[$value];
}
}
4 changes: 4 additions & 0 deletions src/Serializer/PayloadSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private function serializeAsCheckInEvent(Event $event): string
'environment' => $checkIn->getEnvironment(),
];

if (null !== $checkIn->getMonitorConfig()) {
$result['monitor_config'] = $checkIn->getMonitorConfig()->toArray();
}

if (!empty($event->getContexts()['trace'])) {
$result['contexts']['trace'] = $event->getContexts()['trace'];
}
Expand Down
3 changes: 3 additions & 0 deletions tests/CheckInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHPUnit\Framework\TestCase;
use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Sentry\Util\SentryUid;

final class CheckInTest extends TestCase
Expand Down Expand Up @@ -54,6 +56,7 @@ public function gettersAndSettersDataProvider(): array
['getRelease', 'setRelease', '1.0.0'],
['getEnvironment', 'setEnvironment', 'dev'],
['getDuration', 'setDuration', 10],
['getMonitorConfig', 'setMonitorConfig', new MonitorConfig(MonitorSchedule::crontab('* * * * *'))],
];
}
}
Loading

0 comments on commit d907d27

Please sign in to comment.