Skip to content

Commit 153a988

Browse files
will2877cleptric
andauthored
Add captureCheckIn() (#1573)
Co-authored-by: Michi Hoffmann <[email protected]>
1 parent 7f4e129 commit 153a988

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed

src/State/Hub.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
namespace Sentry\State;
66

77
use Sentry\Breadcrumb;
8+
use Sentry\CheckIn;
9+
use Sentry\CheckInStatus;
810
use Sentry\ClientInterface;
911
use Sentry\Event;
1012
use Sentry\EventHint;
1113
use Sentry\EventId;
1214
use Sentry\Integration\IntegrationInterface;
15+
use Sentry\MonitorConfig;
1316
use Sentry\Severity;
1417
use Sentry\Tracing\SamplingContext;
1518
use Sentry\Tracing\Span;
@@ -169,6 +172,38 @@ public function captureLastError(?EventHint $hint = null): ?EventId
169172
return null;
170173
}
171174

175+
/**
176+
* @param string $slug Identifier of the Monitor
177+
* @param CheckInStatus $status The status of the check-in
178+
* @param int|float|null $duration The duration of the check-in
179+
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
180+
* @param string|null $checkInId A check-in ID from the previous check-in
181+
*/
182+
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
183+
{
184+
$client = $this->getClient();
185+
186+
if (null === $client) {
187+
return null;
188+
}
189+
190+
$options = $client->getOptions();
191+
$event = Event::createCheckIn();
192+
$checkIn = new CheckIn(
193+
$slug,
194+
$status,
195+
$checkInId,
196+
$options->getRelease(),
197+
$options->getEnvironment(),
198+
$duration,
199+
$monitorConfig
200+
);
201+
$event->setCheckIn($checkIn);
202+
$this->captureEvent($event);
203+
204+
return $checkIn->getId();
205+
}
206+
172207
/**
173208
* {@inheritdoc}
174209
*/

src/State/HubAdapter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace Sentry\State;
66

77
use Sentry\Breadcrumb;
8+
use Sentry\CheckInStatus;
89
use Sentry\ClientInterface;
910
use Sentry\Event;
1011
use Sentry\EventHint;
1112
use Sentry\EventId;
1213
use Sentry\Integration\IntegrationInterface;
14+
use Sentry\MonitorConfig;
1315
use Sentry\SentrySdk;
1416
use Sentry\Severity;
1517
use Sentry\Tracing\Span;
@@ -135,6 +137,18 @@ public function captureLastError(?EventHint $hint = null): ?EventId
135137
return SentrySdk::getCurrentHub()->captureLastError($hint);
136138
}
137139

140+
/**
141+
* @param string $slug Identifier of the Monitor
142+
* @param CheckInStatus $status The status of the check-in
143+
* @param int|float|null $duration The duration of the check-in
144+
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
145+
* @param string|null $checkInId A check-in ID from the previous check-in
146+
*/
147+
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
148+
{
149+
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
150+
}
151+
138152
/**
139153
* {@inheritdoc}
140154
*/

src/State/HubInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
namespace Sentry\State;
66

77
use Sentry\Breadcrumb;
8+
use Sentry\CheckInStatus;
89
use Sentry\ClientInterface;
910
use Sentry\Event;
1011
use Sentry\EventHint;
1112
use Sentry\EventId;
1213
use Sentry\Integration\IntegrationInterface;
14+
use Sentry\MonitorConfig;
1315
use Sentry\Severity;
1416
use Sentry\Tracing\SamplingContext;
1517
use Sentry\Tracing\Span;
@@ -20,6 +22,8 @@
2022
* This interface represent the class which is responsible for maintaining a
2123
* stack of pairs of clients and scopes. It is the main entry point to talk
2224
* with the Sentry client.
25+
*
26+
* @method string|null captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $upsertMonitorConfig = null, ?string $checkInId = null) Captures a check-in
2327
*/
2428
interface HubInterface
2529
{

src/functions.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ function captureLastError(?EventHint $hint = null): ?EventId
6666
return SentrySdk::getCurrentHub()->captureLastError($hint);
6767
}
6868

69+
/**
70+
* Captures a check-in and sends it to Sentry.
71+
*
72+
* @param string $slug Identifier of the Monitor
73+
* @param CheckInStatus $status The status of the check-in
74+
* @param int|float|null $duration The duration of the check-in
75+
* @param MonitorConfig|null $monitorConfig Configuration of the Monitor
76+
* @param string|null $checkInId A check-in ID from the previous check-in
77+
*/
78+
function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
79+
{
80+
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
81+
}
82+
6983
/**
7084
* Records a new breadcrumb which will be attached to future events. They
7185
* will be added to subsequent events to provide more context on user's

tests/FunctionsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use PHPUnit\Framework\TestCase;
99
use RuntimeException;
1010
use Sentry\Breadcrumb;
11+
use Sentry\CheckInStatus;
1112
use Sentry\ClientInterface;
1213
use Sentry\Event;
1314
use Sentry\EventHint;
1415
use Sentry\EventId;
16+
use Sentry\MonitorConfig;
17+
use Sentry\MonitorSchedule;
1518
use Sentry\Options;
1619
use Sentry\SentrySdk;
1720
use Sentry\Severity;
@@ -25,7 +28,9 @@
2528
use Sentry\Tracing\TraceId;
2629
use Sentry\Tracing\Transaction;
2730
use Sentry\Tracing\TransactionContext;
31+
use Sentry\Util\SentryUid;
2832
use function Sentry\addBreadcrumb;
33+
use function Sentry\captureCheckIn;
2934
use function Sentry\captureEvent;
3035
use function Sentry\captureException;
3136
use function Sentry\captureLastError;
@@ -185,6 +190,38 @@ public static function captureLastErrorDataProvider(): \Generator
185190
];
186191
}
187192

193+
public function testCaptureCheckIn()
194+
{
195+
$hub = new Hub();
196+
$options = new Options([
197+
'environment' => Event::DEFAULT_ENVIRONMENT,
198+
'release' => '1.1.8',
199+
]);
200+
/** @var ClientInterface&MockObject $client */
201+
$client = $this->createMock(ClientInterface::class);
202+
$client->expects($this->once())
203+
->method('getOptions')
204+
->willReturn($options);
205+
206+
$hub->bindClient($client);
207+
SentrySdk::setCurrentHub($hub);
208+
209+
$checkInId = SentryUid::generate();
210+
211+
$this->assertSame($checkInId, captureCheckIn(
212+
'test-crontab',
213+
CheckInStatus::ok(),
214+
10,
215+
new MonitorConfig(
216+
MonitorSchedule::crontab('*/5 * * * *'),
217+
5,
218+
30,
219+
'UTC'
220+
),
221+
$checkInId
222+
));
223+
}
224+
188225
public function testAddBreadcrumb(): void
189226
{
190227
$breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting');

tests/State/HubAdapterTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44

55
namespace Sentry\Tests\State;
66

7+
use PHPUnit\Framework\MockObject\MockObject;
78
use PHPUnit\Framework\TestCase;
89
use Sentry\Breadcrumb;
10+
use Sentry\CheckInStatus;
911
use Sentry\ClientInterface;
1012
use Sentry\Event;
1113
use Sentry\EventHint;
1214
use Sentry\EventId;
1315
use Sentry\Integration\IntegrationInterface;
16+
use Sentry\MonitorConfig;
17+
use Sentry\MonitorSchedule;
18+
use Sentry\Options;
1419
use Sentry\SentrySdk;
1520
use Sentry\Severity;
21+
use Sentry\State\Hub;
1622
use Sentry\State\HubAdapter;
1723
use Sentry\State\HubInterface;
1824
use Sentry\State\Scope;
1925
use Sentry\Tracing\Span;
2026
use Sentry\Tracing\Transaction;
2127
use Sentry\Tracing\TransactionContext;
28+
use Sentry\Util\SentryUid;
2229

2330
final class HubAdapterTest extends TestCase
2431
{
@@ -255,6 +262,39 @@ public static function captureLastErrorDataProvider(): \Generator
255262
];
256263
}
257264

265+
public function testCaptureCheckIn()
266+
{
267+
$hub = new Hub();
268+
269+
$options = new Options([
270+
'environment' => Event::DEFAULT_ENVIRONMENT,
271+
'release' => '1.1.8',
272+
]);
273+
/** @var ClientInterface&MockObject $client */
274+
$client = $this->createMock(ClientInterface::class);
275+
$client->expects($this->once())
276+
->method('getOptions')
277+
->willReturn($options);
278+
279+
$hub->bindClient($client);
280+
SentrySdk::setCurrentHub($hub);
281+
282+
$checkInId = SentryUid::generate();
283+
284+
$this->assertSame($checkInId, HubAdapter::getInstance()->captureCheckIn(
285+
'test-crontab',
286+
CheckInStatus::ok(),
287+
10,
288+
new MonitorConfig(
289+
MonitorSchedule::crontab('*/5 * * * *'),
290+
5,
291+
30,
292+
'UTC'
293+
),
294+
$checkInId
295+
));
296+
}
297+
258298
public function testAddBreadcrumb(): void
259299
{
260300
$breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_DEBUG, Breadcrumb::TYPE_ERROR, 'user');

tests/State/HubTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
use PHPUnit\Framework\MockObject\MockObject;
88
use PHPUnit\Framework\TestCase;
99
use Sentry\Breadcrumb;
10+
use Sentry\CheckInStatus;
1011
use Sentry\ClientInterface;
1112
use Sentry\Event;
1213
use Sentry\EventHint;
1314
use Sentry\EventId;
1415
use Sentry\Integration\IntegrationInterface;
16+
use Sentry\MonitorConfig;
17+
use Sentry\MonitorSchedule;
1518
use Sentry\Options;
1619
use Sentry\Severity;
1720
use Sentry\State\Hub;
1821
use Sentry\State\Scope;
1922
use Sentry\Tracing\PropagationContext;
2023
use Sentry\Tracing\SamplingContext;
2124
use Sentry\Tracing\TransactionContext;
25+
use Sentry\Util\SentryUid;
2226

2327
final class HubTest extends TestCase
2428
{
@@ -390,6 +394,38 @@ public static function captureLastErrorDataProvider(): \Generator
390394
];
391395
}
392396

397+
public function testCaptureCheckIn()
398+
{
399+
$hub = new Hub();
400+
401+
$options = new Options([
402+
'environment' => Event::DEFAULT_ENVIRONMENT,
403+
'release' => '1.1.8',
404+
]);
405+
/** @var ClientInterface&MockObject $client */
406+
$client = $this->createMock(ClientInterface::class);
407+
$client->expects($this->once())
408+
->method('getOptions')
409+
->willReturn($options);
410+
411+
$hub->bindClient($client);
412+
413+
$checkInId = SentryUid::generate();
414+
415+
$this->assertSame($checkInId, $hub->captureCheckIn(
416+
'test-crontab',
417+
CheckInStatus::ok(),
418+
10,
419+
new MonitorConfig(
420+
MonitorSchedule::crontab('*/5 * * * *'),
421+
5,
422+
30,
423+
'UTC'
424+
),
425+
$checkInId
426+
));
427+
}
428+
393429
public function testCaptureEvent(): void
394430
{
395431
$hub = new Hub();

0 commit comments

Comments
 (0)