From 73906e97790bd8ffc414c9cabb1103719b39138a Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 30 Nov 2022 08:06:54 +0800 Subject: [PATCH] Optimize sentry (#66) * Optimize * Optimize SentryExceptionHandler * Adds swoole/swow version * Adds base_path() helper Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- publish/sentry.php | 2 +- src/Aspect/LoggerAspect.php | 50 +++++++++++++++++----------------- src/ConfigProvider.php | 18 ++++++------ src/Integration.php | 15 ++++++++++ src/SentryExceptionHandler.php | 4 +-- src/SentryHandler.php | 14 +--------- 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/publish/sentry.php b/publish/sentry.php index e3814a3..8b55b81 100644 --- a/publish/sentry.php +++ b/publish/sentry.php @@ -12,7 +12,7 @@ 'dsn' => env('SENTRY_DSN', ''), // capture release as git sha - // 'release' => trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')), + // 'release' => trim(exec('git --git-dir ' . (BASE_PATH . '/.git') . ' log --pretty="%h" -n1 HEAD')), 'environment' => env('APP_ENV'), diff --git a/src/Aspect/LoggerAspect.php b/src/Aspect/LoggerAspect.php index 143184b..291be2a 100644 --- a/src/Aspect/LoggerAspect.php +++ b/src/Aspect/LoggerAspect.php @@ -30,34 +30,34 @@ public function __construct(protected ConfigInterface $config) { } - public function process(ProceedingJoinPoint $proceedingJoinPoint) - { - return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->config->get('sentry.breadcrumbs.logs', false)) { - return; - } + public function process(ProceedingJoinPoint $proceedingJoinPoint) + { + return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { + if (! $this->config->get('sentry.breadcrumbs.logs', false)) { + return; + } - $level = $proceedingJoinPoint->arguments['keys']['level']; - $level = $level instanceof UnitEnum ? (int) $level->value : (int) $level; - $message = $proceedingJoinPoint->arguments['keys']['message']; - $context = $proceedingJoinPoint->arguments['keys']['context']; - /** @var null|DateTimeImmutable $datetime */ - $datetime = $proceedingJoinPoint->arguments['keys']['datetime']; + $level = $proceedingJoinPoint->arguments['keys']['level']; + $level = $level instanceof UnitEnum ? (int) $level->value : (int) $level; + $message = $proceedingJoinPoint->arguments['keys']['message']; + $context = $proceedingJoinPoint->arguments['keys']['context']; + /** @var null|DateTimeImmutable $datetime */ + $datetime = $proceedingJoinPoint->arguments['keys']['datetime']; - if (isset($context['no_aspect']) && $context['no_aspect'] === true) { - return; - } + if (isset($context['no_aspect']) && $context['no_aspect'] === true) { + return; + } - Integration::addBreadcrumb(new Breadcrumb( - (string) $this->getLogLevel($level), - Breadcrumb::TYPE_DEFAULT, - 'log.' . Logger::getLevelName($level), - $message, - $context, - $datetime?->getTimestamp() - )); - }); - } + Integration::addBreadcrumb(new Breadcrumb( + (string) $this->getLogLevel($level), + Breadcrumb::TYPE_DEFAULT, + 'log.' . Logger::getLevelName($level), + $message, + $context, + $datetime?->getTimestamp() + )); + }); + } /** * Translates Monolog log levels to Sentry Severity. diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index ad98914..d13e200 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -17,13 +17,19 @@ public function __invoke(): array defined('BASE_PATH') or define('BASE_PATH', ''); return [ - 'dependencies' => [ - \Sentry\ClientBuilderInterface::class => Factory\ClientBuilderFactory::class, - \Sentry\State\HubInterface::class => Factory\HubFactory::class, + 'aspects' => [ + Aspect\HttpClientAspect::class, + Aspect\LoggerAspect::class, + Aspect\RedisAspect::class, + Aspect\SingletonAspect::class, ], 'commands' => [ Command\TestCommand::class, ], + 'dependencies' => [ + \Sentry\ClientBuilderInterface::class => Factory\ClientBuilderFactory::class, + \Sentry\State\HubInterface::class => Factory\HubFactory::class, + ], 'listeners' => [ Listener\InitHubListener::class, Listener\DbQueryListener::class, @@ -33,12 +39,6 @@ public function __invoke(): array \Sentry\SentrySdk::class => __DIR__ . '/../class_map/SentrySdk.php', ], ], - 'aspects' => [ - Aspect\HttpClientAspect::class, - Aspect\LoggerAspect::class, - Aspect\RedisAspect::class, - Aspect\SingletonAspect::class, - ], 'publish' => [ [ 'id' => 'config', diff --git a/src/Integration.php b/src/Integration.php index 3dc9bb7..9f7e366 100644 --- a/src/Integration.php +++ b/src/Integration.php @@ -20,6 +20,8 @@ use function Sentry\addBreadcrumb; use function Sentry\configureScope; +use const SWOOLE_VERSION; + class Integration implements IntegrationInterface { private static ?string $transaction = null; @@ -36,6 +38,19 @@ public function setupOnce(): void return $event; } + if (defined('\SWOOLE_VERSION')) { + $event->setContext('swoole', [ + 'version' => SWOOLE_VERSION, + ]); + } + + if (defined('\Swow\Extension::VERSION')) { + $event->setContext('swow', [ + /* @phpstan-ignore-next-line */ + 'version' => \Swow\Extension::VERSION, + ]); + } + if (empty($event->getTransaction())) { $event->setTransaction($self->getTransaction()); } diff --git a/src/SentryExceptionHandler.php b/src/SentryExceptionHandler.php index 92e609b..bc0dabd 100644 --- a/src/SentryExceptionHandler.php +++ b/src/SentryExceptionHandler.php @@ -19,7 +19,7 @@ class SentryExceptionHandler extends ExceptionHandler { - public function __construct(protected ContainerInterface $container, protected StdoutLoggerInterface $logger) + public function __construct(protected ContainerInterface $container) { } @@ -35,7 +35,7 @@ public function handle(Throwable $throwable, ResponseInterface $response): Respo $hub->getClient()->flush(); } catch (Throwable $e) { - $this->logger->error((string) $e); + $this->container->get(StdoutLoggerInterface::class)->error((string) $e); } }); diff --git a/src/SentryHandler.php b/src/SentryHandler.php index a7eadfa..78ee601 100644 --- a/src/SentryHandler.php +++ b/src/SentryHandler.php @@ -48,21 +48,11 @@ class SentryHandler extends AbstractProcessingHandler */ protected ?FormatterInterface $batchFormatter = null; - /** - * Indicates if we should report exceptions, if `false` this handler will ignore records with an exception set in the context. - */ - private bool $reportExceptions; - - /** - * Indicates if we should use the formatted message instead of just the message. - */ - private bool $useFormattedMessage; - /** * @param int $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 */ - public function __construct($level = Logger::DEBUG, bool $bubble = true, bool $reportExceptions = true, bool $useFormattedMessage = false) + public function __construct($level = Logger::DEBUG, bool $bubble = true, protected bool $reportExceptions = true, protected bool $useFormattedMessage = false) { parent::__construct($level, $bubble); @@ -75,8 +65,6 @@ public function __construct($level = Logger::DEBUG, bool $bubble = true, bool $r $this->release = $release; } $this->hub = $container->get(HubInterface::class); - $this->reportExceptions = $reportExceptions; - $this->useFormattedMessage = $useFormattedMessage; } /**