Skip to content

Commit

Permalink
Adds CacheAspect (#444)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Nov 20, 2023
1 parent 6558ae6 commit 595c944
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
],

'breadcrumbs' => [
'cache' => env('SENTRY_BREADCRUMBS_CACHE', true),
'sql_queries' => env('SENTRY_BREADCRUMBS_SQL_QUERIES', true),
'sql_bindings' => env('SENTRY_BREADCRUMBS_SQL_BINDINGS', true),
'sql_transaction' => env('SENTRY_BREADCRUMBS_SQL_TRANSACTION', true),
Expand Down
62 changes: 62 additions & 0 deletions src/Aspect/CacheAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Sentry\Aspect;

use FriendsOfHyperf\Sentry\Integration;
use FriendsOfHyperf\Sentry\Switcher;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Sentry\Breadcrumb;

use function Hyperf\Tappable\tap;

class CacheAspect extends AbstractAspect
{
public array $classes = [
'Hyperf\Cache\Driver\*Driver::fetch',
'Hyperf\Cache\Driver\*Driver::get',
'Hyperf\Cache\Driver\*Driver::getMultiple',
'Hyperf\Cache\Driver\*Driver::set',
'Hyperf\Cache\Driver\*Driver::setMultiple',
'Hyperf\Cache\Driver\*Driver::delete',
'Hyperf\Cache\Driver\*Driver::deleteMultiple',
];

public function __construct(protected Switcher $switcher)
{
}

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$startTime = microtime(true);

return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint, $startTime) {
if (! $this->switcher->isBreadcrumbEnable('cache')) {
return;
}
$arguments = $proceedingJoinPoint->arguments['keys'];
$method = sprintf('%s::%s()', $proceedingJoinPoint->className, $proceedingJoinPoint->methodName);

$data['result'] = $result;
$data['arguments'] = $arguments;
$data['timeMs'] = (microtime(true) - $startTime) * 1000;

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'cache',
$method,
$data
));
});
}
}
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __invoke(): array
return [
'aspects' => [
Aspect\BreadcrumbAspect::class,
Aspect\CacheAspect::class,
Aspect\CoroutineAspect::class,
Aspect\CrontabExecutorAspect::class,
Aspect\GuzzleHttpClientAspect::class,
Expand Down

0 comments on commit 595c944

Please sign in to comment.