Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/Integration/AbstractErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sentry\Event;
use Sentry\EventHint;
use Sentry\ExceptionMechanism;
use Sentry\State\EventCapturer;
use Sentry\State\EventRecorder;
use Sentry\State\IsolationScope;

use function Sentry\withIsolationScope;
Expand All @@ -24,7 +24,7 @@ protected function captureException(\Throwable $exception): void
return $this->addExceptionMechanismToEvent($event);
});

EventCapturer::captureException($exception);
EventRecorder::captureException($exception, null, $scope);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/ExceptionToSentryIssueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Monolog\Logger;
use Monolog\LogRecord;
use Psr\Log\LogLevel;
use Sentry\State\EventCapturer;
use Sentry\State\EventRecorder;
use Sentry\State\IsolationScope;

use function Sentry\withIsolationScope;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function handle($record): bool
$scope->setExtra('monolog.extra', $monologExtraData);
}

EventCapturer::captureException($exception);
EventRecorder::captureException($exception, null, $scope);
});

return $this->bubble === false;
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/LogToSentryIssueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Psr\Log\LogLevel;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\State\EventCapturer;
use Sentry\State\EventRecorder;
use Sentry\State\IsolationScope;

use function Sentry\withIsolationScope;
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function doWrite($record): void
}
}

EventCapturer::captureEvent($event, $hint);
EventRecorder::captureEvent($event, $hint, $scope);
});
}

Expand Down
40 changes: 19 additions & 21 deletions src/State/EventCapturer.php → src/State/EventRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,54 @@
/**
* @internal
*/
final class EventCapturer
final class EventRecorder
{
private function __construct()
{
}

public static function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
public static function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null, ?IsolationScope $isolationScope = null): ?EventId
{
return self::capture(static function (ClientInterface $client, IsolationScope $captureScope) use ($message, $level, $hint): ?EventId {
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();

return self::captureWithScope(SentrySdk::getClient($isolationScope), $isolationScope, static function (ClientInterface $client, IsolationScope $captureScope) use ($message, $level, $hint): ?EventId {
return $client->captureMessage($message, $level, $captureScope, $hint);
});
}

public static function captureException(\Throwable $exception, ?EventHint $hint = null): ?EventId
public static function captureException(\Throwable $exception, ?EventHint $hint = null, ?IsolationScope $isolationScope = null): ?EventId
{
return self::capture(static function (ClientInterface $client, IsolationScope $captureScope) use ($exception, $hint): ?EventId {
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();

return self::captureWithScope(SentrySdk::getClient($isolationScope), $isolationScope, static function (ClientInterface $client, IsolationScope $captureScope) use ($exception, $hint): ?EventId {
return $client->captureException($exception, $captureScope, $hint);
});
}

public static function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
public static function captureEvent(Event $event, ?EventHint $hint = null, ?IsolationScope $isolationScope = null): ?EventId
{
return self::capture(static function (ClientInterface $client, IsolationScope $captureScope) use ($event, $hint): ?EventId {
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();

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

public static function captureLastError(?EventHint $hint = null): ?EventId
public static function captureLastError(?EventHint $hint = null, ?IsolationScope $isolationScope = null): ?EventId
{
return self::capture(static function (ClientInterface $client, IsolationScope $captureScope) use ($hint): ?EventId {
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();

return self::captureWithScope(SentrySdk::getClient($isolationScope), $isolationScope, static function (ClientInterface $client, IsolationScope $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
public static function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null, ?IsolationScope $isolationScope = null): ?string
{
$isolationScope = SentrySdk::getIsolationScope();
$isolationScope = $isolationScope ?? SentrySdk::getIsolationScope();
$client = SentrySdk::getClient($isolationScope);

if ($client instanceof NoOpClient) {
Expand All @@ -84,16 +92,6 @@ public static function captureCheckIn(string $slug, CheckInStatus $status, $dura
return $checkIn->getId();
}

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

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

/**
* @param callable(ClientInterface, IsolationScope): ?EventId $capture
*/
Expand Down
16 changes: 12 additions & 4 deletions src/Tracing/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Sentry\Options;
use Sentry\Profiling\Profiler;
use Sentry\SentrySdk;
use Sentry\State\EventRecorder;
use Sentry\State\IsolationScope;

/**
* This class stores all the information about a Transaction.
Expand All @@ -20,6 +22,11 @@ final class Transaction extends Span
*/
private $name;

/**
* @var IsolationScope
*/
private $scope;

/**
* @var Transaction The transaction
*/
Expand All @@ -42,11 +49,12 @@ final class Transaction extends Span
*
* @internal
*/
public function __construct(TransactionContext $context)
public function __construct(TransactionContext $context, ?IsolationScope $scope = null)
{
parent::__construct($context);

$this->name = $context->getName();
$this->scope = $scope ?? SentrySdk::getIsolationScope();
$this->metadata = $context->getMetadata();
$this->transaction = $this;
}
Expand Down Expand Up @@ -90,7 +98,7 @@ public function getDynamicSamplingContext(): DynamicSamplingContext
return $this->metadata->getDynamicSamplingContext();
}

$samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, SentrySdk::getClient());
$samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, SentrySdk::getClient($this->scope));
$this->getMetadata()->setDynamicSamplingContext($samplingContext);

return $samplingContext;
Expand All @@ -115,7 +123,7 @@ public function initSpanRecorder(int $maxSpans = 1000): self
public function initProfiler(?Options $options = null): Profiler
{
if ($this->profiler === null) {
$this->profiler = new Profiler($options ?? SentrySdk::getClient()->getOptions());
$this->profiler = new Profiler($options ?? SentrySdk::getClient($this->scope)->getOptions());
}

return $this->profiler;
Expand Down Expand Up @@ -180,6 +188,6 @@ public function finish(?float $endTimestamp = null): ?EventId
}
}

return \Sentry\captureEvent($event);
return EventRecorder::captureEvent($event, null, $this->scope);
}
}
12 changes: 6 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Sentry\Logs\Logs;
use Sentry\Metrics\TraceMetrics;
use Sentry\State\BreadcrumbRecorder;
use Sentry\State\EventCapturer;
use Sentry\State\EventRecorder;
use Sentry\State\GlobalScope;
use Sentry\State\IsolationScope;
use Sentry\State\Scope;
Expand Down Expand Up @@ -105,7 +105,7 @@ function getClient(): ClientInterface
*/
function captureMessage(string $message, ?Severity $level = null, ?EventHint $hint = null): ?EventId
{
return EventCapturer::captureMessage($message, $level, $hint);
return EventRecorder::captureMessage($message, $level, $hint);
}

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

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

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\State\EventCapturer;
use Sentry\State\EventRecorder;
use Sentry\State\IsolationScope;
use Sentry\Util\SentryUid;

final class EventCapturerTest extends TestCase
final class EventRecorderTest extends TestCase
{
public function testCaptureMessagePassesIsolationScopeAndStoresLastEventId(): void
{
Expand All @@ -36,7 +36,7 @@ public function testCaptureMessagePassesIsolationScopeAndStoresLastEventId(): vo
}), $hint)
->willReturn($eventId);

$this->assertSame($eventId, EventCapturer::captureMessage('foo', Severity::debug(), $hint));
$this->assertSame($eventId, EventRecorder::captureMessage('foo', Severity::debug(), $hint, $isolationScope));
$this->assertSame($eventId, $isolationScope->getLastEventId());
}

Expand All @@ -55,7 +55,7 @@ public function testCaptureExceptionPassesIsolationScopeAndStoresLastEventId():
}), $hint)
->willReturn($eventId);

$this->assertSame($eventId, EventCapturer::captureException($exception, $hint));
$this->assertSame($eventId, EventRecorder::captureException($exception, $hint, $isolationScope));
$this->assertSame($eventId, $isolationScope->getLastEventId());
}

Expand All @@ -73,7 +73,7 @@ public function testCaptureEventPassesIsolationScopeAndStoresLastEventId(): void
}))
->willReturn($event->getId());

$this->assertSame($event->getId(), EventCapturer::captureEvent($event, $hint));
$this->assertSame($event->getId(), EventRecorder::captureEvent($event, $hint, $isolationScope));
$this->assertSame($event->getId(), $isolationScope->getLastEventId());
}

Expand All @@ -91,7 +91,7 @@ public function testCaptureLastErrorPassesIsolationScopeAndStoresLastEventId():
}), $hint)
->willReturn($eventId);

$this->assertSame($eventId, EventCapturer::captureLastError($hint));
$this->assertSame($eventId, EventRecorder::captureLastError($hint, $isolationScope));
$this->assertSame($eventId, $isolationScope->getLastEventId());
}

Expand All @@ -109,7 +109,7 @@ public function testCaptureEventClearsLastEventIdWhenClientReturnsNull(): void
}))
->willReturn(null);

$this->assertNull(EventCapturer::captureEvent($event));
$this->assertNull(EventRecorder::captureEvent($event));
$this->assertNull($isolationScope->getLastEventId());
}

Expand Down Expand Up @@ -150,12 +150,13 @@ public function testCaptureCheckInCreatesEventAndStoresLastEventId(): void
}))
->willReturn($eventId);

$this->assertSame($checkInId, EventCapturer::captureCheckIn(
$this->assertSame($checkInId, EventRecorder::captureCheckIn(
'test-crontab',
CheckInStatus::ok(),
10,
$monitorConfig,
$checkInId
$checkInId,
$isolationScope
));
$this->assertSame($eventId, $isolationScope->getLastEventId());
}
Expand All @@ -166,7 +167,7 @@ public function testCaptureCheckInReturnsNullForNoOpClient(): void
$eventId = EventId::generate();
SentrySdk::getIsolationScope()->setLastEventId($eventId);

$this->assertNull(EventCapturer::captureCheckIn('test-crontab', CheckInStatus::ok()));
$this->assertNull(EventRecorder::captureCheckIn('test-crontab', CheckInStatus::ok()));
$this->assertSame($eventId, SentrySdk::getIsolationScope()->getLastEventId());
}

Expand Down
8 changes: 6 additions & 2 deletions tests/Tracing/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function testFinish(): void

SentrySdk::init($client);

$transaction = new Transaction($transactionContext);
$scope = SentrySdk::getIsolationScope();
$transaction = new Transaction($transactionContext, $scope);
SentrySdk::getCurrentRuntimeContext()->setIsolationScope(new IsolationScope());
$transaction->initSpanRecorder();

$span1 = $transaction->startChild(new SpanContext());
Expand All @@ -59,7 +61,7 @@ public function testFinish(): void
$this->assertSame([$span1, $span2], $eventArg->getSpans());

return true;
}), null, $this->isInstanceOf(IsolationScope::class))
}), null, $scope)
->willReturnCallback(static function (Event $eventArg) use (&$expectedEventId): EventId {
$expectedEventId = $eventArg->getId();

Expand All @@ -72,6 +74,8 @@ public function testFinish(): void
$eventId = $transaction->finish();

$this->assertSame($expectedEventId, $eventId);
$this->assertSame($expectedEventId, $scope->getLastEventId());
$this->assertNull(SentrySdk::getIsolationScope()->getLastEventId());
}

public function testFinishDoesNothingIfSampledFlagIsNotTrue(): void
Expand Down