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 captureCheckIn() #1573

Merged
merged 13 commits into from
Jul 31, 2023
35 changes: 35 additions & 0 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Sentry\State;

use Sentry\Breadcrumb;
use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\Severity;
use Sentry\Tracing\SamplingContext;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -169,6 +172,38 @@
return null;
}

/**
* @param string $slug Identifier of the Monitor
* @param CheckInStatus $status The status of the check-in
* @param int|float|null $duration The duration of the check-in
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
* @param string|null $checkInId A check-in ID from the previous check-in
*/
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
$client = $this->getClient();

if (null === $client) {
return null;

Check warning on line 187 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L187

Added line #L187 was not covered by tests
}

$options = $client->getOptions();
$event = Event::createCheckIn();
$checkIn = new CheckIn(
$slug,
$status,
$checkInId,
$options->getRelease(),
$options->getEnvironment(),
$duration,
$monitorConfig
);
$event->setCheckIn($checkIn);
$this->captureEvent($event);

return $checkIn->getId();
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 14 additions & 0 deletions src/State/HubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace Sentry\State;

use Sentry\Breadcrumb;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -135,6 +137,18 @@ public function captureLastError(?EventHint $hint = null): ?EventId
return SentrySdk::getCurrentHub()->captureLastError($hint);
}

/**
* @param string $slug Identifier of the Monitor
* @param CheckInStatus $status The status of the check-in
* @param int|float|null $duration The duration of the check-in
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
* @param string|null $checkInId A check-in ID from the previous check-in
*/
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 4 additions & 0 deletions src/State/HubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace Sentry\State;

use Sentry\Breadcrumb;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\Severity;
use Sentry\Tracing\SamplingContext;
use Sentry\Tracing\Span;
Expand All @@ -20,6 +22,8 @@
* This interface represent the class which is responsible for maintaining a
* stack of pairs of clients and scopes. It is the main entry point to talk
* with the Sentry client.
*
* @method string|null captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $upsertMonitorConfig = null, ?string $checkInId = null) Captures a check-in
*/
interface HubInterface
{
Expand Down
14 changes: 14 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ function captureLastError(?EventHint $hint = null): ?EventId
return SentrySdk::getCurrentHub()->captureLastError($hint);
}

/**
* Captures a check-in and sends it to Sentry.
*
* @param string $slug Identifier of the Monitor
* @param CheckInStatus $status The status of the check-in
* @param int|float|null $duration The duration of the check-in
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
* @param string|null $checkInId A check-in ID from the previous check-in
*/
function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
}

/**
* Records a new breadcrumb which will be attached to future events. They
* will be added to subsequent events to provide more context on user's
Expand Down
37 changes: 37 additions & 0 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Sentry\Breadcrumb;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\Severity;
Expand All @@ -25,7 +28,9 @@
use Sentry\Tracing\TraceId;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Util\SentryUid;
use function Sentry\addBreadcrumb;
use function Sentry\captureCheckIn;
use function Sentry\captureEvent;
use function Sentry\captureException;
use function Sentry\captureLastError;
Expand Down Expand Up @@ -185,6 +190,38 @@ public static function captureLastErrorDataProvider(): \Generator
];
}

public function testCaptureCheckIn()
{
$hub = new Hub();
$options = new Options([
'environment' => Event::DEFAULT_ENVIRONMENT,
'release' => '1.1.8',
]);
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn($options);

$hub->bindClient($client);
SentrySdk::setCurrentHub($hub);

$checkInId = SentryUid::generate();

$this->assertSame($checkInId, captureCheckIn(
'test-crontab',
CheckInStatus::ok(),
10,
new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
),
$checkInId
));
}

public function testAddBreadcrumb(): void
{
$breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting');
Expand Down
40 changes: 40 additions & 0 deletions tests/State/HubAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@

namespace Sentry\Tests\State;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\State\Hub;
use Sentry\State\HubAdapter;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Util\SentryUid;

final class HubAdapterTest extends TestCase
{
Expand Down Expand Up @@ -255,6 +262,39 @@ public static function captureLastErrorDataProvider(): \Generator
];
}

public function testCaptureCheckIn()
{
$hub = new Hub();

$options = new Options([
'environment' => Event::DEFAULT_ENVIRONMENT,
'release' => '1.1.8',
]);
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn($options);

$hub->bindClient($client);
SentrySdk::setCurrentHub($hub);

$checkInId = SentryUid::generate();

$this->assertSame($checkInId, HubAdapter::getInstance()->captureCheckIn(
'test-crontab',
CheckInStatus::ok(),
10,
new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
),
$checkInId
));
}

public function testAddBreadcrumb(): void
{
$breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_DEBUG, Breadcrumb::TYPE_ERROR, 'user');
Expand Down
36 changes: 36 additions & 0 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\MonitorSchedule;
use Sentry\Options;
use Sentry\Severity;
use Sentry\State\Hub;
use Sentry\State\Scope;
use Sentry\Tracing\PropagationContext;
use Sentry\Tracing\SamplingContext;
use Sentry\Tracing\TransactionContext;
use Sentry\Util\SentryUid;

final class HubTest extends TestCase
{
Expand Down Expand Up @@ -390,6 +394,38 @@ public static function captureLastErrorDataProvider(): \Generator
];
}

public function testCaptureCheckIn()
{
$hub = new Hub();

$options = new Options([
'environment' => Event::DEFAULT_ENVIRONMENT,
'release' => '1.1.8',
]);
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn($options);

$hub->bindClient($client);

$checkInId = SentryUid::generate();

$this->assertSame($checkInId, $hub->captureCheckIn(
'test-crontab',
CheckInStatus::ok(),
10,
new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
),
$checkInId
));
}

public function testCaptureEvent(): void
{
$hub = new Hub();
Expand Down