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
7 changes: 4 additions & 3 deletions src/Logs/LogsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,19 @@ public function add(
}
}

public function flush(?ClientInterface $client = null): ?EventId
public function flush(?ClientInterface $client = null, ?Scope $isolationScope = null): ?EventId
{
$logs = $this->logs;

if ($logs === null || $logs->isEmpty()) {
return null;
}

$client = $client ?? SentrySdk::getCurrentHub()->getClient();
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();
$client = $client ?? SentrySdk::getClient($isolationScope);
$event = Event::createLogs()->setLogs($logs->drain());

return $client->captureEvent($event);
return $client->captureEvent($event, null, Scope::mergeScopes(SentrySdk::getGlobalScope(), $isolationScope));
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ public function add(
}
}

public function flush(?ClientInterface $client = null): ?EventId
public function flush(?ClientInterface $client = null, ?Scope $isolationScope = null): ?EventId
{
$metrics = $this->metrics;

if ($metrics === null || $metrics->isEmpty()) {
return null;
}

$client = $client ?? SentrySdk::getCurrentHub()->getClient();
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();
$client = $client ?? SentrySdk::getClient($isolationScope);
$event = Event::createMetrics()->setMetrics($metrics->drain());

return $client->captureEvent($event);
return $client->captureEvent($event, null, Scope::mergeScopes(SentrySdk::getGlobalScope(), $isolationScope));
}

/**
Expand Down
41 changes: 10 additions & 31 deletions src/SentrySdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\State\Hub;
use Sentry\State\HubAdapter;
use Sentry\State\HubInterface;
use Sentry\State\RuntimeContext;
use Sentry\State\RuntimeContextManager;
Expand All @@ -19,11 +19,6 @@
*/
final class SentrySdk
{
/**
* @var HubInterface|null The baseline hub
*/
private static $currentHub;

/**
* @var Scope|null The process-global scope
*/
Expand All @@ -42,29 +37,22 @@ private function __construct()
}

/**
* Initializes the SDK by creating a new hub instance each time this method
* gets called.
* Initializes the SDK by binding the client to the global scope and reset
* the current local runtime state.
*/
public static function init(?ClientInterface $client = null): HubInterface
{
$hubClient = $client ?? new NoOpClient();

if ($client !== null) {
self::getGlobalScope()->setClient($client);
}
self::$currentHub = new Hub($hubClient);
self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub);
self::$runtimeContextManager = new RuntimeContextManager();

return self::getCurrentHub();
}

/**
* Gets the current hub. If it's not initialized then creates a new instance
* and sets it as current hub.
*/
public static function getCurrentHub(): HubInterface
{
return self::getRuntimeContextManager()->getCurrentHub();
return HubAdapter::getInstance();
}

/**
Expand All @@ -78,11 +66,7 @@ public static function getCurrentHub(): HubInterface
*/
public static function setCurrentHub(HubInterface $hub): HubInterface
{
$wasSetOnActiveRuntimeContext = self::getRuntimeContextManager()->setCurrentHub($hub);

if (!$wasSetOnActiveRuntimeContext) {
self::$currentHub = $hub;
}
self::getGlobalScope()->setClient($hub->getClient());
Comment thread
Litarnus marked this conversation as resolved.

return $hub;
}
Expand All @@ -101,9 +85,9 @@ public static function getIsolationScope(): Scope
return self::getCurrentRuntimeContext()->getIsolationScope();
}

public static function getClient(): ClientInterface
public static function getClient(?Scope $isolationScope = null): ClientInterface
{
$client = self::getIsolationScope()->getClient();
$client = ($isolationScope ?? self::getIsolationScope())->getClient();

if (!$client instanceof NoOpClient) {
return $client;
Expand Down Expand Up @@ -181,18 +165,13 @@ public static function flush(): void
Logs::getInstance()->flush();
TraceMetrics::getInstance()->flush();

$client = self::getCurrentHub()->getClient();
$client->flush();
self::getClient()->flush();
}

private static function getRuntimeContextManager(): RuntimeContextManager
{
if (self::$currentHub === null) {
self::$currentHub = new Hub(new NoOpClient());
}

if (self::$runtimeContextManager === null) {
self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub);
self::$runtimeContextManager = new RuntimeContextManager();
}

return self::$runtimeContextManager;
Expand Down
111 changes: 78 additions & 33 deletions src/State/HubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
use Sentry\EventId;
use Sentry\Integration\IntegrationInterface;
use Sentry\MonitorConfig;
use Sentry\NoOpClient;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSampler;

/**
* An implementation of {@see HubInterface} that uses {@see SentrySdk} internally
Expand Down Expand Up @@ -55,87 +57,83 @@ public static function getInstance(): self
*/
public function getClient(): ClientInterface
{
return SentrySdk::getCurrentHub()->getClient();
return SentrySdk::getClient();
}

/**
* {@inheritdoc}
*/
public function getLastEventId(): ?EventId
{
return SentrySdk::getCurrentHub()->getLastEventId();
}

/**
* {@inheritdoc}
*/
public function pushScope(): Scope
{
return SentrySdk::getCurrentHub()->pushScope();
}

/**
* {@inheritdoc}
*/
public function popScope(): bool
{
return SentrySdk::getCurrentHub()->popScope();
return SentrySdk::getIsolationScope()->getLastEventId();
}

/**
* {@inheritdoc}
*/
public function withScope(callable $callback)
{
return SentrySdk::getCurrentHub()->withScope($callback);
return \Sentry\withIsolationScope($callback);
}

/**
* {@inheritdoc}
*/
public function configureScope(callable $callback): void
{
SentrySdk::getCurrentHub()->configureScope($callback);
$callback(SentrySdk::getIsolationScope());
}

/**
* {@inheritdoc}
*/
public function bindClient(ClientInterface $client): void
{
SentrySdk::getCurrentHub()->bindClient($client);
SentrySdk::getGlobalScope()->setClient($client);
}

/**
* {@inheritdoc}
*/
public function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
{
return SentrySdk::getCurrentHub()->captureMessage($message, $level, $hint);
$eventId = SentrySdk::getClient()->captureMessage($message, $level, SentrySdk::getIsolationScope(), $hint);
SentrySdk::getIsolationScope()->setLastEventId($eventId);

return $eventId;
}

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

return $eventId;
}

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

return $eventId;
}

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

return $eventId;
Comment thread
Litarnus marked this conversation as resolved.
}

/**
Expand All @@ -145,63 +143,110 @@ public function captureLastError(?EventHint $hint = null): ?EventId
*/
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);
$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();
}

/**
* {@inheritdoc}
*/
public function addBreadcrumb(Breadcrumb $breadcrumb): bool
{
return SentrySdk::getCurrentHub()->addBreadcrumb($breadcrumb);
$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);
}

return $breadcrumb !== null;
}

/**
* {@inheritDoc}
*/
public function addAttachment(Attachment $attachment): bool
{
return SentrySdk::getCurrentHub()->addAttachment($attachment);
if (SentrySdk::getClient() instanceof NoOpClient) {
return false;
}

SentrySdk::getIsolationScope()->addAttachment($attachment);

return true;
}

/**
* {@inheritdoc}
*/
public function getIntegration(string $className): ?IntegrationInterface
{
return SentrySdk::getCurrentHub()->getIntegration($className);
return SentrySdk::getClient()->getIntegration($className);
}

/**
* {@inheritdoc}
*/
public function startTransaction(TransactionContext $context, array $customSamplingContext = []): Transaction
{
return SentrySdk::getCurrentHub()->startTransaction($context, $customSamplingContext);
return TransactionSampler::startTransaction(SentrySdk::getClient()->getOptions(), $context, $customSamplingContext);
}

/**
* {@inheritdoc}
*/
public function getTransaction(): ?Transaction
{
return SentrySdk::getCurrentHub()->getTransaction();
return SentrySdk::getIsolationScope()->getTransaction();
}

/**
* {@inheritdoc}
*/
public function getSpan(): ?Span
{
return SentrySdk::getCurrentHub()->getSpan();
return SentrySdk::getIsolationScope()->getSpan();
}

/**
* {@inheritdoc}
*/
public function setSpan(?Span $span): HubInterface
{
return SentrySdk::getCurrentHub()->setSpan($span);
SentrySdk::getIsolationScope()->setSpan($span);

return $this;
}

/**
Expand Down
Loading
Loading