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
22 changes: 10 additions & 12 deletions src/ClientReport/ClientReportAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sentry\ClientReport;

use Sentry\Event;
use Sentry\State\HubAdapter;
use Sentry\SentrySdk;
use Sentry\Transport\DataCategory;

class ClientReportAggregator
Expand Down Expand Up @@ -35,15 +35,13 @@ public function add(DataCategory $category, Reason $reason, int $quantity): void
$category = $category->getValue();
$reason = $reason->getValue();
if ($quantity <= 0) {
$client = HubAdapter::getInstance()->getClient();
if ($client !== null) {
$logger = $client->getOptions()->getLoggerOrNullLogger();
$logger->debug('Dropping Client report with category={category} and reason={reason} because quantity is zero or negative ({quantity})', [
'category' => $category,
'reason' => $reason,
'quantity' => $quantity,
]);
}
$client = SentrySdk::getClient();
$logger = $client->getOptions()->getLoggerOrNullLogger();
$logger->debug('Dropping Client report with category={category} and reason={reason} because quantity is zero or negative ({quantity})', [
'category' => $category,
'reason' => $reason,
'quantity' => $quantity,
]);

return;
}
Expand All @@ -64,11 +62,11 @@ public function flush(): void
$event = Event::createClientReport();
$event->setClientReports($reports);

$client = HubAdapter::getInstance()->getClient();
$client = SentrySdk::getClient();

// Reset the client reports only if we successfully sent an event. If it fails it
// can be sent on the next flush, or it gets discarded anyway.
if ($client !== null && $client->captureEvent($event) !== null) {
if ($client->captureEvent($event) !== null) {
$this->reports = [];
}
}
Expand Down
15 changes: 7 additions & 8 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\State\EventCapturer;
use Sentry\State\Scope;

use function Sentry\withIsolationScope;

abstract class AbstractErrorListenerIntegration implements IntegrationInterface
{
/**
* Captures the exception using the given hub instance.
*
* @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 {
withIsolationScope(function (Scope $scope) use ($exception): void {
$scope->addEventProcessor(\Closure::fromCallable([$this, 'addExceptionMechanismToEvent']));

$hub->captureException($exception);
EventCapturer::captureException($exception);
});
}

Expand Down
10 changes: 4 additions & 6 deletions src/Integration/ErrorListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,22 @@ 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;
}

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

$integration->captureException($currentHub, $exception);
$integration->captureException($exception);
}
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Integration/ExceptionListenerIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ 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);
});
}
}
10 changes: 4 additions & 6 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())) {
if (($client->getOptions()->getErrorTypes() & $exception->getSeverity()) === 0) {
return;
}

$integration->captureException($currentHub, $exception);
$integration->captureException($exception);
});
}
}
24 changes: 9 additions & 15 deletions src/Monolog/BreadcrumbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Psr\Log\LogLevel;
use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\State\HubInterface;
use Sentry\SentrySdk;
use Sentry\State\BreadcrumbRecorder;
use Sentry\State\Scope;

/**
Expand All @@ -21,23 +22,15 @@
final class BreadcrumbHandler extends AbstractProcessingHandler
{
/**
* @var HubInterface
*/
private $hub;

/**
* @param HubInterface $hub The hub to which errors are reported
* @param int|string $level The minimum logging level at which this
* handler will be triggered
* @param bool $bubble Whether the messages that are handled can
* bubble up the stack or not
* @param int|string $level The minimum logging level at which this
* handler will be triggered
* @param bool $bubble Whether the messages that are handled can
* bubble up the stack or not
*
* @phpstan-param int|string|Level|LogLevel::* $level
*/
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true)
public function __construct($level = Logger::DEBUG, bool $bubble = true)
{
$this->hub = $hub;

parent::__construct($level, $bubble);
}

Expand Down Expand Up @@ -66,7 +59,8 @@ protected function write($record): void
$timestamp
);

$this->hub->addBreadcrumb($breadcrumb);
$scope = SentrySdk::getIsolationScope();
BreadcrumbRecorder::record(SentrySdk::getClient($scope), $scope, $breadcrumb);
}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/Monolog/ExceptionToSentryIssueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@
use Monolog\Logger;
use Monolog\LogRecord;
use Psr\Log\LogLevel;
use Sentry\State\HubInterface;
use Sentry\State\EventCapturer;
use Sentry\State\Scope;

use function Sentry\withIsolationScope;

/**
* This Monolog handler will collect monolog events and send them to sentry.
*/
class ExceptionToSentryIssueHandler extends AbstractHandler
{
/**
* @var HubInterface
*/
private $hub;

/**
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true)
public function __construct($level = Logger::DEBUG, bool $bubble = true)
{
$this->hub = $hub;

parent::__construct($level, $bubble);
}

Expand All @@ -44,7 +39,7 @@ public function handle($record): bool
return false;
}

$this->hub->withScope(function (Scope $scope) use ($record, $exception): void {
withIsolationScope(function (Scope $scope) use ($record, $exception): void {
$scope->setExtra('monolog.channel', $record['channel']);
$scope->setExtra('monolog.level', $record['level_name']);
$scope->setExtra('monolog.message', $record['message']);
Expand All @@ -61,7 +56,7 @@ public function handle($record): bool
$scope->setExtra('monolog.extra', $monologExtraData);
}

$this->hub->captureException($exception);
EventCapturer::captureException($exception);
});

return $this->bubble === false;
Expand Down
16 changes: 6 additions & 10 deletions src/Monolog/LogToSentryIssueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use Psr\Log\LogLevel;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\State\HubInterface;
use Sentry\State\EventCapturer;
use Sentry\State\Scope;

use function Sentry\withIsolationScope;

/**
* This Monolog handler captures log messages as Sentry issues.
*/
Expand All @@ -23,11 +25,6 @@ class LogToSentryIssueHandler extends AbstractProcessingHandler

private const CONTEXT_EXCEPTION_KEY = 'exception';

/**
* @var HubInterface
*/
private $hub;

/**
* @var bool
*/
Expand All @@ -36,9 +33,8 @@ class LogToSentryIssueHandler extends AbstractProcessingHandler
/**
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = true, bool $fillExtraContext = false)
public function __construct($level = Logger::DEBUG, bool $bubble = true, bool $fillExtraContext = false)
{
$this->hub = $hub;
$this->fillExtraContext = $fillExtraContext;

parent::__construct($level, $bubble);
Expand Down Expand Up @@ -70,7 +66,7 @@ protected function doWrite($record): void

$hint = new EventHint();

$this->hub->withScope(function (Scope $scope) use ($record, $event, $hint): void {
withIsolationScope(function (Scope $scope) use ($record, $event, $hint): void {
$scope->setExtra('monolog.channel', $record['channel']);
$scope->setExtra('monolog.level', $record['level_name']);

Expand All @@ -88,7 +84,7 @@ protected function doWrite($record): void
}
}

$this->hub->captureEvent($event, $hint);
EventCapturer::captureEvent($event, $hint);
});
}

Expand Down
30 changes: 15 additions & 15 deletions src/Tracing/GuzzleTracingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Sentry\Breadcrumb;
use Sentry\ClientInterface;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\BreadcrumbRecorder;
use Sentry\State\Scope;

use function Sentry\getBaggage;
use function Sentry\getTraceparent;
Expand All @@ -21,13 +22,13 @@
*/
final class GuzzleTracingMiddleware
{
public static function trace(?HubInterface $hub = null): \Closure
public static function trace(?Scope $scope = null): \Closure
{
return static function (callable $handler) use ($hub): \Closure {
return static function (RequestInterface $request, array $options) use ($hub, $handler) {
$hub = $hub ?? SentrySdk::getCurrentHub();
$client = $hub->getClient();
$parentSpan = $hub->getSpan();
return static function (callable $handler) use ($scope): \Closure {
return static function (RequestInterface $request, array $options) use ($handler, $scope) {
$scope = $scope ?? SentrySdk::getIsolationScope();
$client = SentrySdk::getClient($scope);
$parentSpan = $scope->getSpan();

$partialUri = Uri::fromParts([
'scheme' => $request->getUri()->getScheme(),
Expand Down Expand Up @@ -59,28 +60,27 @@ public static function trace(?HubInterface $hub = null): \Closure

$childSpan = $parentSpan->startChild($spanContext);

$hub->setSpan($childSpan);
$scope->setSpan($childSpan);
}

if (self::shouldAttachTracingHeaders($client, $request)) {
$traceParent = getTraceparent();
$traceParent = getTraceparent($scope);
if ($traceParent !== '') {
$request = $request->withHeader('sentry-trace', $traceParent);
}

$baggage = getBaggage();
$baggage = getBaggage($scope);
if ($baggage !== '') {
$request = $request->withHeader('baggage', $baggage);
}
}

$handlerPromiseCallback = static function ($responseOrException) use ($hub, $spanAndBreadcrumbData, $childSpan, $parentSpan, $partialUri) {
$handlerPromiseCallback = static function ($responseOrException) use ($client, $scope, $spanAndBreadcrumbData, $childSpan, $parentSpan, $partialUri) {
if ($childSpan !== null) {
// We finish the span (which means setting the span end timestamp) first to ensure the measured time
// the span spans is as close to only the HTTP request time and do the data collection afterwards
// We finish the span first to keep the measured duration as close as possible to the HTTP request time.
$childSpan->finish();

$hub->setSpan($parentSpan);
$scope->setSpan($parentSpan);
}

$response = null;
Expand Down Expand Up @@ -113,7 +113,7 @@ public static function trace(?HubInterface $hub = null): \Closure
}
}

$hub->addBreadcrumb(new Breadcrumb(
BreadcrumbRecorder::record($client, $scope, new Breadcrumb(
$breadcrumbLevel,
Breadcrumb::TYPE_HTTP,
'http',
Expand Down
Loading