Skip to content
Open
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
44 changes: 44 additions & 0 deletions src/State/BreadcrumbRecorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Sentry\State;

use Sentry\Breadcrumb;
use Sentry\ClientInterface;
use Sentry\NoOpClient;

/**
* @internal
*/
final class BreadcrumbRecorder
{
private function __construct()
{
}

/**
* Records the breadcrumb on the given scope if the client configuration allows it.
*/
public static function record(ClientInterface $client, Scope $scope, Breadcrumb $breadcrumb): bool
{
if ($client instanceof NoOpClient) {
return false;
}

$options = $client->getOptions();
$maxBreadcrumbs = $options->getMaxBreadcrumbs();

if ($maxBreadcrumbs <= 0) {
return false;
}

$breadcrumb = ($options->getBeforeBreadcrumbCallback())($breadcrumb);

if ($breadcrumb !== null) {
$scope->addBreadcrumb($breadcrumb, $maxBreadcrumbs);
}

return $breadcrumb !== null;
}
}
107 changes: 107 additions & 0 deletions src/State/EventCapturer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace Sentry\State;

use Sentry\CheckIn;
use Sentry\CheckInStatus;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\MonitorConfig;
use Sentry\NoOpClient;
use Sentry\SentrySdk;
use Sentry\Severity;

/**
* @internal
*/
final class EventCapturer
{
private function __construct()
{
}

public static function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
{
return self::capture(static function (ClientInterface $client, Scope $captureScope) use ($message, $level, $hint): ?EventId {
return $client->captureMessage($message, $level, $captureScope, $hint);
});
}

public static function captureException(\Throwable $exception, ?EventHint $hint = null): ?EventId
{
return self::capture(static function (ClientInterface $client, Scope $captureScope) use ($exception, $hint): ?EventId {
return $client->captureException($exception, $captureScope, $hint);
});
}

public static function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
{
return self::capture(static function (ClientInterface $client, Scope $captureScope) use ($event, $hint): ?EventId {
return $client->captureEvent($event, $hint, $captureScope);
});
}

public static function captureLastError(?EventHint $hint = null): ?EventId
{
return self::capture(static function (ClientInterface $client, Scope $captureScope) use ($hint): ?EventId {
return $client->captureLastError($captureScope, $hint);
});
}

/**
* @param int|float|null $duration
*/
public static function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
$isolationScope = SentrySdk::getIsolationScope();
$client = SentrySdk::getClient($isolationScope);

if ($client instanceof NoOpClient) {
return null;
}

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

self::captureWithScope($client, $isolationScope, static function (ClientInterface $client, Scope $captureScope) use ($event): ?EventId {
return $client->captureEvent($event, null, $captureScope);
});

return $checkIn->getId();
}

/**
* @param callable(ClientInterface, Scope): ?EventId $capture
*/
private static function capture(callable $capture): ?EventId
{
$isolationScope = SentrySdk::getIsolationScope();

return self::captureWithScope(SentrySdk::getClient($isolationScope), $isolationScope, $capture);
}

/**
* @param callable(ClientInterface, Scope): ?EventId $capture
*/
private static function captureWithScope(ClientInterface $client, Scope $isolationScope, callable $capture): ?EventId
{
$eventId = $capture($client, Scope::mergeScopes(SentrySdk::getGlobalScope(), $isolationScope));
$isolationScope->setLastEventId($eventId);

return $eventId;
}
}
63 changes: 7 additions & 56 deletions src/State/HubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,43 +97,31 @@ public function bindClient(ClientInterface $client): void
*/
public function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
{
$eventId = SentrySdk::getClient()->captureMessage($message, $level, SentrySdk::getIsolationScope(), $hint);
SentrySdk::getIsolationScope()->setLastEventId($eventId);

return $eventId;
return EventCapturer::captureMessage($message, $level, $hint);
}

/**
* {@inheritdoc}
*/
public function captureException(\Throwable $exception, ?EventHint $hint = null): ?EventId
{
$eventId = SentrySdk::getClient()->captureException($exception, SentrySdk::getIsolationScope(), $hint);
SentrySdk::getIsolationScope()->setLastEventId($eventId);

return $eventId;
return EventCapturer::captureException($exception, $hint);
}

/**
* {@inheritdoc}
*/
public function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
{
$eventId = SentrySdk::getClient()->captureEvent($event, $hint, SentrySdk::getIsolationScope());
SentrySdk::getIsolationScope()->setLastEventId($eventId);

return $eventId;
return EventCapturer::captureEvent($event, $hint);
}

/**
* {@inheritdoc}
*/
public function captureLastError(?EventHint $hint = null): ?EventId
{
$eventId = SentrySdk::getClient()->captureLastError(SentrySdk::getIsolationScope(), $hint);
SentrySdk::getIsolationScope()->setLastEventId($eventId);

return $eventId;
return EventCapturer::captureLastError($hint);
}

/**
Expand All @@ -143,54 +131,17 @@ public function captureLastError(?EventHint $hint = null): ?EventId
*/
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
$client = SentrySdk::getClient();

if ($client instanceof NoOpClient) {
return null;
}

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

return $checkIn->getId();
return EventCapturer::captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
}

/**
* {@inheritdoc}
*/
public function addBreadcrumb(Breadcrumb $breadcrumb): bool
{
$client = SentrySdk::getClient();

if ($client instanceof NoOpClient) {
return false;
}

$options = $client->getOptions();
$maxBreadcrumbs = $options->getMaxBreadcrumbs();

if ($maxBreadcrumbs <= 0) {
return false;
}

$breadcrumb = ($options->getBeforeBreadcrumbCallback())($breadcrumb);

if ($breadcrumb !== null) {
SentrySdk::getIsolationScope()->addBreadcrumb($breadcrumb, $maxBreadcrumbs);
}
$scope = SentrySdk::getIsolationScope();

return $breadcrumb !== null;
return BreadcrumbRecorder::record(SentrySdk::getClient($scope), $scope, $breadcrumb);
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Sentry\Integration\OTLPIntegration;
use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\State\BreadcrumbRecorder;
use Sentry\State\EventCapturer;
use Sentry\State\Scope;
use Sentry\Tracing\PropagationContext;
use Sentry\Tracing\SpanContext;
Expand Down Expand Up @@ -101,7 +103,7 @@ function getClient(): ClientInterface
*/
function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
{
return SentrySdk::getCurrentHub()->captureMessage($message, $level, $hint);
return EventCapturer::captureMessage($message, $level, $hint);
}

/**
Expand All @@ -112,7 +114,7 @@ function captureMessage(string $message, ?Severity $level = null, ?EventHint $hi
*/
function captureException(\Throwable $exception, ?EventHint $hint = null): ?EventId
{
return SentrySdk::getCurrentHub()->captureException($exception, $hint);
return EventCapturer::captureException($exception, $hint);
}

/**
Expand All @@ -123,7 +125,7 @@ function captureException(\Throwable $exception, ?EventHint $hint = null): ?Even
*/
function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
{
return SentrySdk::getCurrentHub()->captureEvent($event, $hint);
return EventCapturer::captureEvent($event, $hint);
}

/**
Expand All @@ -133,7 +135,7 @@ function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
*/
function captureLastError(?EventHint $hint = null): ?EventId
{
return SentrySdk::getCurrentHub()->captureLastError($hint);
return EventCapturer::captureLastError($hint);
}

/**
Expand All @@ -147,7 +149,7 @@ function captureLastError(?EventHint $hint = null): ?EventId
*/
function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
return SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
return EventCapturer::captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
}

/**
Expand All @@ -161,7 +163,7 @@ function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?
*/
function withMonitor(string $slug, callable $callback, ?MonitorConfig $monitorConfig = null)
{
$checkInId = SentrySdk::getCurrentHub()->captureCheckIn($slug, CheckInStatus::inProgress(), null, $monitorConfig);
$checkInId = captureCheckIn($slug, CheckInStatus::inProgress(), null, $monitorConfig);

$status = CheckInStatus::ok();
$duration = 0;
Expand All @@ -177,7 +179,7 @@ function withMonitor(string $slug, callable $callback, ?MonitorConfig $monitorCo

throw $e;
} finally {
SentrySdk::getCurrentHub()->captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
captureCheckIn($slug, $status, $duration, $monitorConfig, $checkInId);
}
}

Expand All @@ -195,11 +197,12 @@ function withMonitor(string $slug, callable $callback, ?MonitorConfig $monitorCo
*/
function addBreadcrumb($category, ?string $message = null, array $metadata = [], string $level = Breadcrumb::LEVEL_INFO, string $type = Breadcrumb::TYPE_DEFAULT, ?float $timestamp = null): void
{
SentrySdk::getCurrentHub()->addBreadcrumb(
$category instanceof Breadcrumb
? $category
: new Breadcrumb($level, $type, $category, $message, $metadata, $timestamp)
);
$scope = SentrySdk::getIsolationScope();
$breadcrumb = $category instanceof Breadcrumb
? $category
: new Breadcrumb($level, $type, $category, $message, $metadata, $timestamp);

BreadcrumbRecorder::record(SentrySdk::getClient($scope), $scope, $breadcrumb);
}

/**
Expand Down
Loading