Skip to content
Draft
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
11 changes: 11 additions & 0 deletions src/Attributes/AttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public function set(string $key, $value): self
return $this;
}

public function __clone()
{
$attributes = [];

foreach ($this->attributes as $key => $attribute) {
$attributes[$key] = clone $attribute;
}

$this->attributes = $attributes;
}

public function get(string $key): ?Attribute
{
return $this->attributes[$key] ?? null;
Expand Down
24 changes: 24 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ public function captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scop
return null;
}

/**
* {@inheritdoc}
*
* @param int|float|null $duration
*/
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string
{
$options = $this->getOptions();
$event = Event::createCheckIn();
$checkIn = new CheckIn(
$slug,
$status,
$checkInId,
$options->getRelease(),
$options->getEnvironment(),
$duration,
$monitorConfig
);
$event->setCheckIn($checkIn);
$this->captureEvent($event, null, SentrySdk::getMergedScope());

return $checkIn->getId();
}

/**
* {@inheritdoc}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function captureLastError(?Scope $scope = null, ?EventHint $hint = null):
*/
public function captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scope = null): ?EventId;

/**
* Captures a check-in.
*
* @param int|float|null $duration
*/
public function captureCheckIn(string $slug, CheckInStatus $status, $duration = null, ?MonitorConfig $monitorConfig = null, ?string $checkInId = null): ?string;

/**
* Returns the integration instance if it is installed on the client.
*
Expand Down
13 changes: 6 additions & 7 deletions src/Integration/AbstractErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

use Sentry\Event;
use Sentry\ExceptionMechanism;
use Sentry\State\HubInterface;
use Sentry\SentrySdk;
use Sentry\State\Scope;

abstract class AbstractErrorListenerIntegration implements IntegrationInterface
{
/**
* Captures the exception using the given hub instance.
* Captures the exception using a forked scope.
*
* @param HubInterface $hub The hub instance
* @param \Throwable $exception The exception instance
* @param \Throwable $exception The exception instance
*/
protected function captureException(HubInterface $hub, \Throwable $exception): void
protected function captureException(\Throwable $exception): void
{
$hub->withScope(function (Scope $scope) use ($hub, $exception): void {
SentrySdk::withScope(function (Scope $scope) use ($exception): void {
$scope->addEventProcessor(\Closure::fromCallable([$this, 'addExceptionMechanismToEvent']));

$hub->captureException($exception);
\Sentry\captureException($exception);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Integration/EnvironmentIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class EnvironmentIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

if ($integration !== null) {
$event->setRuntimeContext($integration->updateRuntimeContext($event->getRuntimeContext()));
Expand Down
8 changes: 3 additions & 5 deletions src/Integration/ErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ public function setupOnce(): void
ErrorHandler::registerOnceErrorHandler($this->options)
->addErrorHandlerListener(
static function (\ErrorException $exception): void {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

if ($integration === null) {
return;
}

$client = $currentHub->getClient();

if ($exception instanceof SilencedErrorException && !$client->getOptions()->shouldCaptureSilencedErrors()) {
return;
}
Expand All @@ -50,7 +48,7 @@ static function (\ErrorException $exception): void {
return;
}

$integration->captureException($currentHub, $exception);
$integration->captureException($exception);
}
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Integration/ExceptionListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public function setupOnce(): void
{
$errorHandler = ErrorHandler::registerOnceExceptionHandler();
$errorHandler->addExceptionHandlerListener(static function (\Throwable $exception): void {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

// The client bound to the current hub, if any, could not have this
// integration enabled. If this is the case, bail out
if ($integration === null) {
return;
}

$integration->captureException($currentHub, $exception);
$integration->captureException($exception);
});
}
}
8 changes: 3 additions & 5 deletions src/Integration/FatalErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ public function setupOnce(): void
{
$errorHandler = ErrorHandler::registerOnceFatalErrorHandler();
$errorHandler->addFatalErrorHandlerListener(static function (FatalErrorException $exception): void {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

if ($integration === null) {
return;
}

$client = $currentHub->getClient();

if (!($client->getOptions()->getErrorTypes() & $exception->getSeverity())) {
return;
}

$integration->captureException($currentHub, $exception);
$integration->captureException($exception);
});
}
}
2 changes: 1 addition & 1 deletion src/Integration/FrameContextifierIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(?LoggerInterface $logger = null)
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$client = SentrySdk::getCurrentHub()->getClient();
$client = SentrySdk::getClient();

$maxContextLines = $client->getOptions()->getContextLines();
$integration = $client->getIntegration(self::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/ModulesIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ModulesIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$integration = SentrySdk::getClient()->getIntegration(self::class);

// The integration could be bound to a client that is not the one
// attached to the current hub. If this is the case, bail out
Expand Down
6 changes: 2 additions & 4 deletions src/Integration/RequestIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ public function __construct(?RequestFetcherInterface $requestFetcher = null, arr
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(function (Event $event): Event {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

if ($integration === null) {
return $event;
}

$client = $currentHub->getClient();

$this->processEvent($event, $client->getOptions());

return $event;
Expand Down
3 changes: 2 additions & 1 deletion src/Integration/TransactionIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ final class TransactionIntegration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event, EventHint $hint): Event {
$integration = SentrySdk::getCurrentHub()->getIntegration(self::class);
$client = SentrySdk::getClient();
$integration = $client->getIntegration(self::class);

// The client bound to the current hub, if any, could not have this
// integration enabled. If this is the case, bail out
Expand Down
57 changes: 29 additions & 28 deletions src/Logs/LogsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Sentry\Event;
use Sentry\EventId;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Util\Arr;
use Sentry\Util\Str;
Expand Down Expand Up @@ -37,8 +36,7 @@ public function add(
): void {
$timestamp = microtime(true);

$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();
$client = SentrySdk::getClient();

$options = $client->getOptions();
$sdkLogger = $options->getLogger();
Expand Down Expand Up @@ -67,31 +65,32 @@ public function add(
$formattedMessage = $message;
}

$log = (new Log($timestamp, $this->getTraceId($hub), $level, $formattedMessage))
$log = (new Log($timestamp, $this->getTraceId(), $level, $formattedMessage))
->setAttribute('sentry.release', $options->getRelease())
->setAttribute('sentry.environment', $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT)
->setAttribute('sentry.server.address', $options->getServerName())
->setAttribute('sentry.trace.parent_span_id', $hub->getSpan() ? $hub->getSpan()->getSpanId() : null);
->setAttribute('sentry.trace.parent_span_id', SentrySdk::getCurrentScope()->getSpan() ? SentrySdk::getCurrentScope()->getSpan()->getSpanId() : null);

if ($client instanceof Client) {
$log->setAttribute('sentry.sdk.name', $client->getSdkIdentifier());
$log->setAttribute('sentry.sdk.version', $client->getSdkVersion());
}

$hub->configureScope(function (Scope $scope) use ($log) {
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$log->setAttribute('user.id', $user->getId());
}
if ($user->getEmail() !== null) {
$log->setAttribute('user.email', $user->getEmail());
}
if ($user->getUsername() !== null) {
$log->setAttribute('user.name', $user->getUsername());
}
$scope = SentrySdk::getMergedScope();
$this->applyScopeAttributes($log, $scope);

$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$log->setAttribute('user.id', $user->getId());
}
if ($user->getEmail() !== null) {
$log->setAttribute('user.email', $user->getEmail());
}
if ($user->getUsername() !== null) {
$log->setAttribute('user.name', $user->getUsername());
}
});
}

if (\count($values)) {
$log->setAttribute('sentry.message.template', $message);
Expand Down Expand Up @@ -155,12 +154,11 @@ public function flush(): ?EventId
return null;
}

$hub = SentrySdk::getCurrentHub();
$event = Event::createLogs()->setLogs($this->logs);

$this->logs = [];

return $hub->captureEvent($event);
return SentrySdk::getClient()->captureEvent($event, null, SentrySdk::getMergedScope());
}

/**
Expand All @@ -171,20 +169,23 @@ public function all(): array
return $this->logs;
}

private function getTraceId(HubInterface $hub): string
private function getTraceId(): string
{
$span = $hub->getSpan();
$span = SentrySdk::getCurrentScope()->getSpan();

if ($span !== null) {
return (string) $span->getTraceId();
}

$traceId = '';

$hub->configureScope(function (Scope $scope) use (&$traceId) {
$traceId = (string) $scope->getPropagationContext()->getTraceId();
});
return (string) SentrySdk::getIsolationScope()->getPropagationContext()->getTraceId();
}

return $traceId;
private function applyScopeAttributes(Log $log, Scope $scope): void
{
foreach ($scope->getAttributes()->all() as $key => $attribute) {
if ($log->attributes()->get($key) === null) {
$log->setAttribute($key, $attribute);
}
}
}
}
Loading
Loading