Skip to content

Commit

Permalink
Optimize sentry (#66)
Browse files Browse the repository at this point in the history
* Optimize

* Optimize SentryExceptionHandler

* Adds swoole/swow version

* Adds base_path() helper

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Nov 30, 2022
1 parent a6568b0 commit 73906e9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
2 changes: 1 addition & 1 deletion publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

Expand Down
50 changes: 25 additions & 25 deletions src/Aspect/LoggerAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/SentryExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

class SentryExceptionHandler extends ExceptionHandler
{
public function __construct(protected ContainerInterface $container, protected StdoutLoggerInterface $logger)
public function __construct(protected ContainerInterface $container)
{
}

Expand All @@ -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);
}
});

Expand Down
14 changes: 1 addition & 13 deletions src/SentryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 73906e9

Please sign in to comment.