-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds `Tracing\CacheAspect` * Refactor CacheAspect to handle cache clearing * Refactor CacheAspect to handle cache clearing * Refactor CacheAspect to handle cache clearing in ConfigProvider * Refactor CacheAspect to handle cache clearing in ConfigProvider * Refactor CacheAspect to handle cache clearing in ConfigProvider * Refactor cache aspect to enable cache clearing in ConfigProvider * Refactor CacheAspect to remove unnecessary code in handleClear method * Refactor CacheAspect to remove unnecessary code in process method * Revert "Refactor CacheAspect to remove unnecessary code in process method" This reverts commit 0a272b4. * Refactor CacheAspect to remove unnecessary code in process method * Refactor CacheAspect to add cache fetching in process method * Refactor RedisAspect to improve key handling in process method * Refactor TracingCommandListener to improve data handling in process method --------- Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
1 parent
7924bd3
commit b80a5c2
Showing
6 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?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\Tracing\Aspect; | ||
|
||
use FriendsOfHyperf\Sentry\Switcher; | ||
use FriendsOfHyperf\Sentry\Tracing\SpanStarter; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
use Sentry\SentrySdk; | ||
use Sentry\Tracing\Span; | ||
|
||
use function Hyperf\Tappable\tap; | ||
|
||
/** | ||
* @see https://develop.sentry.dev/sdk/telemetry/traces/modules/caches/ | ||
*/ | ||
class CacheAspect extends AbstractAspect | ||
{ | ||
use SpanStarter; | ||
|
||
public array $classes = [ | ||
'Hyperf\Cache\Driver\*Driver::set', | ||
'Hyperf\Cache\Driver\*Driver::setMultiple', | ||
'Hyperf\Cache\Driver\*Driver::fetch', | ||
'Hyperf\Cache\Driver\*Driver::get', | ||
'Hyperf\Cache\Driver\*Driver::getMultiple', | ||
'Hyperf\Cache\Driver\*Driver::delete', | ||
'Hyperf\Cache\Driver\*Driver::deleteMultiple', | ||
'Hyperf\Cache\Driver\*Driver::clear', | ||
]; | ||
|
||
public function __construct(protected Switcher $switcher) | ||
{ | ||
} | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
if (! $this->switcher->isTracingSpanEnable('cache') || Switcher::isDisableCoroutineTracing()) { | ||
return $proceedingJoinPoint->process(); | ||
} | ||
|
||
$parent = SentrySdk::getCurrentHub()->getSpan(); | ||
|
||
try { | ||
$method = $proceedingJoinPoint->methodName; | ||
$op = match ($method) { | ||
'set' => 'cache.put', | ||
'get', 'fetch' => 'cache.get', | ||
'delete' => 'cache.remove', | ||
'setMultiple' => 'cache.put', | ||
'getMultiple' => 'cache.get', | ||
'deleteMultiple' => 'cache.remove', | ||
'clear' => 'cache.flush', | ||
default => 'cache', | ||
}; | ||
|
||
/** @var string|string[] $key */ | ||
$key = match ($method) { | ||
'set', 'get', 'delete', 'setMultiple', 'getMultiple', 'deleteMultiple' => $proceedingJoinPoint->arguments['order'][0] ?? 'unknown', | ||
default => '', | ||
}; | ||
|
||
$span = $this->startSpan(op: $op, description: $key); | ||
|
||
return tap($proceedingJoinPoint->process(), function ($value) use ($span, $method, $key) { | ||
match ($method) { | ||
'set', => $this->handleSet($span, $key, $value), | ||
'get', 'fetch' => $this->handleGet($span, $key, $value), | ||
'delete' => $this->handleDelete($span, $key, $value), | ||
'setMultiple' => $this->handleSetMultiple($span, $key, $value), | ||
'getMultiple' => $this->handleGetMultiple($span, $key, $value), | ||
'deleteMultiple' => $this->handleDeleteMultiple($span, $key, $value), | ||
'clear' => $this->handleClear($span), | ||
default => null, | ||
}; | ||
}); | ||
} finally { | ||
SentrySdk::getCurrentHub()->setSpan($parent); | ||
} | ||
} | ||
|
||
private function handleSet(Span $span, string $key, mixed $value) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $key, | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleGet(Span $span, string $key, mixed $value) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $key, | ||
'cache.hit' => ! is_null($value), | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleDelete(Span $span, string $key, mixed $value) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $key, | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleSetMultiple(Span $span, array $keys, array $values) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $keys, | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleGetMultiple(Span $span, array $keys, array $values) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $keys, | ||
'cache.hit' => ! empty($values), | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleDeleteMultiple(Span $span, array $keys, array $values) | ||
{ | ||
$span | ||
->setData([ | ||
'cache.key' => $keys, | ||
]) | ||
->finish(); | ||
} | ||
|
||
private function handleClear(Span $span) | ||
{ | ||
$span->finish(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters