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
23 changes: 23 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,26 @@
return null;
}

public function captureCheckIn(string $slug, MonitorConfig $upsertMonitorConfig, CheckInStatus $status, ?CheckIn $previous = null): CheckIn

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

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L175

Added line #L175 was not covered by tests
{
$options = $this->getClient()->getOptions();

Check failure on line 177 in src/State/Hub.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method getOptions() on Sentry\ClientInterface|null.

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

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L177

Added line #L177 was not covered by tests

$event = Event::createCheckIn();
$checkIn = new CheckIn(
$slug,
$status,
$previous?$previous->getId():null,
$options->getRelease(),
$options->getEnvironment(),
null,
$upsertMonitorConfig
);
$event->setCheckIn($checkIn);
$this->captureEvent($event);

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

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L179-L190

Added lines #L179 - L190 were not covered by tests

return $checkIn;

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

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L192

Added line #L192 was not covered by tests
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions src/State/HubAdapter.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\SentrySdk;
use Sentry\Severity;
use Sentry\Tracing\Span;
Expand Down Expand Up @@ -135,6 +138,14 @@ public function captureLastError(?EventHint $hint = null): ?EventId
return SentrySdk::getCurrentHub()->captureLastError($hint);
}

/**
* {@inheritdoc}
*/
public function captureCheckIn(string $slug, MonitorConfig $upsertMonitorConfig, CheckInStatus $status, ?CheckIn $previous = null): CheckIn
{
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $upsertMonitorConfig, $status, $previous);
}

/**
* {@inheritdoc}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/State/HubInterface.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 @@ -112,6 +115,18 @@
*/
public function captureLastError(/*?EventHint $hint = null*/): ?EventId;

/**
* Captures a CheckIn to the configured Monitor.
*
* @param string $slug Identifier of the Monitor
* @param MonitorConfig $upsertMonitorConfig Configuration of the Monitor
* @param CheckInStatus $status The status of the Monitor
* @param CheckIn|null $previos A CheckIn that may have preceded the current CheckIn
*
* @return CheckIn The created CheckIn
*/
public function captureCheckIn(string $slug, MonitorConfig $upsertMonitorConfig, CheckInStatus $status, ?CheckIn $previous = null): CheckIn;

Check failure on line 128 in src/State/HubInterface.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @param references unknown parameter: $previos
cleptric marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
59 changes: 59 additions & 0 deletions tests/State/HubAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

use PHPUnit\Framework\TestCase;
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\MonitorSchedule;
use Sentry\MonitorScheduleUnit;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\State\HubAdapter;
Expand Down Expand Up @@ -255,6 +260,60 @@ public static function captureLastErrorDataProvider(): \Generator
];
}

/**
* @dataProvider captureCheckInProvider
*/
public function testCaptureCheckIn(array $expectedFunctionCallArgs)
{
$checkIn = new CheckIn(
$expectedFunctionCallArgs[0],
$expectedFunctionCallArgs[2]
);

$hub = $this->createMock(HubInterface::class);
$hub->expects($this->once())
->method('captureCheckIn')
->with(...$expectedFunctionCallArgs)
->willReturn($checkIn);

SentrySdk::setCurrentHub($hub);

$this->assertSame($checkIn, HubAdapter::getInstance()->captureCheckIn(...$expectedFunctionCallArgs));

}

public static function captureCheckInProvider(): \Generator
{
yield [
[
'test-crontab',
new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
),
CheckInStatus::ok(),
],
];

yield [
[
'test-interval',
new MonitorConfig(
MonitorSchedule::interval(
5,
MonitorScheduleUnit::minute()
),
5,
30,
'UTC'
),
CheckInStatus::ok(),
],
];
}

public function testAddBreadcrumb(): void
{
$breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_DEBUG, Breadcrumb::TYPE_ERROR, 'user');
Expand Down
61 changes: 61 additions & 0 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
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\MonitorSchedule;
use Sentry\MonitorScheduleUnit;
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\PropagationContext;
use Sentry\Tracing\SamplingContext;
Expand Down Expand Up @@ -390,6 +398,59 @@ public static function captureLastErrorDataProvider(): \Generator
];
}

/**
* @dataProvider captureCheckInProvider
*/
public function testCaptureCheckIn(array $expectedFunctionCallArgs)
{
$checkIn = new CheckIn(
$expectedFunctionCallArgs[0],
$expectedFunctionCallArgs[2]
);

$hub = $this->createMock(HubInterface::class);
$hub->expects($this->once())
->method('captureCheckIn')
->with(...$expectedFunctionCallArgs)
->willReturn($checkIn);

SentrySdk::setCurrentHub($hub);

$this->assertSame($checkIn, HubAdapter::getInstance()->captureCheckIn(...$expectedFunctionCallArgs));

}

public static function captureCheckInProvider(): \Generator
{
yield [
[
'test-crontab',
new MonitorConfig(
MonitorSchedule::crontab('*/5 * * * *'),
5,
30,
'UTC'
),
CheckInStatus::ok(),
],
];

yield [
[
'test-interval',
new MonitorConfig(
MonitorSchedule::interval(
5,
MonitorScheduleUnit::minute()
),
5,
30,
'UTC'
),
CheckInStatus::ok(),
],
];
}
public function testCaptureEvent(): void
{
$hub = new Hub();
Expand Down
Loading