-
Notifications
You must be signed in to change notification settings - Fork 185
feat(cache): enable auto-instrumentation for symfony cache #942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1a9ff70
feat(cache): enable auto-instrumentation for symfony cache
Litarnus 84c3cb7
lints and tests
Litarnus 45cf2ba
lints
Litarnus 0343388
types
Litarnus 172ee36
types
Litarnus 519b0ac
lints
Litarnus 9c8cec3
Merge branch 'master' into martinl/cache-tracing
Litarnus 9df5668
lints
Litarnus a3e4f2b
lints
Litarnus d86babe
handle callback errors
Litarnus d107da4
comment
Litarnus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Sentry\SentryBundle\Tracing\Cache; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\InvalidArgumentException; | ||
use Sentry\State\HubInterface; | ||
use Sentry\Tracing\SpanContext; | ||
use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
|
@@ -38,7 +39,7 @@ trait TraceableCacheAdapterTrait | |
*/ | ||
public function getItem($key): CacheItem | ||
{ | ||
return $this->traceFunction('cache.get_item', function () use ($key): CacheItem { | ||
return $this->traceFunction('cache.get', function () use ($key): CacheItem { | ||
return $this->decoratedAdapter->getItem($key); | ||
}, $key); | ||
} | ||
|
@@ -48,7 +49,7 @@ public function getItem($key): CacheItem | |
*/ | ||
public function getItems(array $keys = []): iterable | ||
{ | ||
return $this->traceFunction('cache.get_items', function () use ($keys): iterable { | ||
return $this->traceFunction('cache.get', function () use ($keys): iterable { | ||
return $this->decoratedAdapter->getItems($keys); | ||
}); | ||
} | ||
|
@@ -58,7 +59,7 @@ public function getItems(array $keys = []): iterable | |
*/ | ||
public function clear(string $prefix = ''): bool | ||
{ | ||
return $this->traceFunction('cache.clear', function () use ($prefix): bool { | ||
return $this->traceFunction('cache.flush', function () use ($prefix): bool { | ||
return $this->decoratedAdapter->clear($prefix); | ||
}, $prefix); | ||
} | ||
|
@@ -68,7 +69,7 @@ public function clear(string $prefix = ''): bool | |
*/ | ||
public function delete(string $key): bool | ||
{ | ||
return $this->traceFunction('cache.delete_item', function () use ($key): bool { | ||
return $this->traceFunction('cache.remove', function () use ($key): bool { | ||
if (!$this->decoratedAdapter instanceof CacheInterface) { | ||
throw new \BadMethodCallException(\sprintf('The %s::delete() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class)); | ||
} | ||
|
@@ -92,7 +93,7 @@ public function hasItem($key): bool | |
*/ | ||
public function deleteItem($key): bool | ||
{ | ||
return $this->traceFunction('cache.delete_item', function () use ($key): bool { | ||
return $this->traceFunction('cache.remove', function () use ($key): bool { | ||
return $this->decoratedAdapter->deleteItem($key); | ||
}, $key); | ||
} | ||
|
@@ -102,7 +103,7 @@ public function deleteItem($key): bool | |
*/ | ||
public function deleteItems(array $keys): bool | ||
{ | ||
return $this->traceFunction('cache.delete_items', function () use ($keys): bool { | ||
return $this->traceFunction('cache.remove', function () use ($keys): bool { | ||
return $this->decoratedAdapter->deleteItems($keys); | ||
}); | ||
} | ||
|
@@ -112,7 +113,7 @@ public function deleteItems(array $keys): bool | |
*/ | ||
public function save(CacheItemInterface $item): bool | ||
{ | ||
return $this->traceFunction('cache.save', function () use ($item): bool { | ||
return $this->traceFunction('cache.put', function () use ($item): bool { | ||
return $this->decoratedAdapter->save($item); | ||
}); | ||
} | ||
|
@@ -122,7 +123,7 @@ public function save(CacheItemInterface $item): bool | |
*/ | ||
public function saveDeferred(CacheItemInterface $item): bool | ||
{ | ||
return $this->traceFunction('cache.save_deferred', function () use ($item): bool { | ||
return $this->traceFunction('cache.put', function () use ($item): bool { | ||
return $this->decoratedAdapter->saveDeferred($item); | ||
}); | ||
} | ||
|
@@ -162,6 +163,10 @@ public function reset(): void | |
} | ||
|
||
/** | ||
* Traces a symfony operation and creating one span in the process. | ||
* | ||
* If you want to trace a get operation with callback, use {@see self::traceGet()} instead. | ||
* | ||
* @phpstan-template TResult | ||
* | ||
* @phpstan-param \Closure(): TResult $callback | ||
|
@@ -172,27 +177,155 @@ private function traceFunction(string $spanOperation, \Closure $callback, ?strin | |
{ | ||
$span = $this->hub->getSpan(); | ||
|
||
if (null !== $span) { | ||
$spanContext = SpanContext::make() | ||
->setOp($spanOperation) | ||
->setOrigin('auto.cache'); | ||
// Exit early if we have no span. | ||
if (null === $span) { | ||
return $callback(); | ||
} | ||
|
||
if (null !== $spanDescription) { | ||
$spanContext->setDescription(urldecode($spanDescription)); | ||
} | ||
$spanContext = SpanContext::make() | ||
->setOp($spanOperation) | ||
->setOrigin('auto.cache'); | ||
|
||
$span = $span->startChild($spanContext); | ||
if (null !== $spanDescription) { | ||
$spanContext->setDescription(urldecode($spanDescription)); | ||
} | ||
|
||
$span = $span->startChild($spanContext); | ||
|
||
try { | ||
return $callback(); | ||
$result = $callback(); | ||
|
||
// Necessary for static analysis. Otherwise, the TResult type is assumed to be CacheItemInterface. | ||
if (!$result instanceof CacheItemInterface) { | ||
return $result; | ||
} | ||
|
||
$data = ['cache.hit' => $result->isHit()]; | ||
if ($result->isHit()) { | ||
$data['cache.item_size'] = static::getCacheItemSize($result->get()); | ||
} | ||
$span->setData($data); | ||
|
||
return $result; | ||
} finally { | ||
if (null !== $span) { | ||
$span->finish(); | ||
$span->finish(); | ||
} | ||
} | ||
|
||
/** | ||
* Traces a Symfony Cache get() call with a get and optional put span. | ||
* | ||
* Produces 2 spans in case of a cache miss: | ||
* 1. 'cache.get' span | ||
* 2. 'cache.put' span | ||
* | ||
* If the callback uses code with sentry traces, those traces will be available in the trace explorer. | ||
* | ||
* Use this method if you want to instrument {@see CacheInterface::get()}. | ||
* | ||
* @param string $key | ||
* @param callable $callback | ||
* @param float|null $beta | ||
* @param array<int|string,mixed>|null $metadata | ||
* | ||
* @return mixed | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function traceGet(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null) | ||
{ | ||
if (!$this->decoratedAdapter instanceof CacheInterface) { | ||
throw new \BadMethodCallException(\sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class)); | ||
} | ||
$parentSpan = $this->hub->getSpan(); | ||
|
||
// If we don't have a parent span we can just forward it. | ||
if (null === $parentSpan) { | ||
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata); | ||
} | ||
|
||
$spanContext = SpanContext::make() | ||
->setOp('cache.get') | ||
->setOrigin('auto.cache'); | ||
|
||
$spanContext->setDescription(urldecode($key)); | ||
|
||
$getSpan = $parentSpan->startChild($spanContext); | ||
|
||
try { | ||
$this->hub->setSpan($getSpan); | ||
|
||
$wasMiss = false; | ||
$saveStartTimestamp = null; | ||
|
||
try { | ||
$value = $this->decoratedAdapter->get($key, function (CacheItemInterface $item, &$save) use ($callback, &$wasMiss, &$saveStartTimestamp) { | ||
$wasMiss = true; | ||
|
||
$result = $callback($item, $save); | ||
|
||
if ($save) { | ||
$saveStartTimestamp = microtime(true); | ||
} | ||
|
||
return $result; | ||
}, $beta, $metadata); | ||
} catch (\Throwable $t) { | ||
$getSpan->finish(); | ||
throw $t; | ||
} | ||
|
||
$now = microtime(true); | ||
|
||
$getSpan->setData([ | ||
'cache.hit' => !$wasMiss, | ||
'cache.item_size' => self::getCacheItemSize($value), | ||
]); | ||
|
||
// If we got a timestamp here we know that we missed | ||
if (null !== $saveStartTimestamp) { | ||
$getSpan->finish($saveStartTimestamp); | ||
$saveContext = SpanContext::make() | ||
->setOp('cache.put') | ||
->setOrigin('auto.cache') | ||
->setDescription(urldecode($key)); | ||
$saveSpan = $parentSpan->startChild($saveContext); | ||
$saveSpan->setStartTimestamp($saveStartTimestamp); | ||
$saveSpan->setData([ | ||
'cache.item_size' => self::getCacheItemSize($value), | ||
]); | ||
$saveSpan->finish($now); | ||
} else { | ||
$getSpan->finish(); | ||
} | ||
|
||
return $value; | ||
} finally { | ||
// We always want to restore the previous parent span. | ||
$this->hub->setSpan($parentSpan); | ||
} | ||
} | ||
|
||
/** | ||
* Calculates the size of the cached item. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return int|null | ||
*/ | ||
public static function getCacheItemSize($value): ?int | ||
{ | ||
// We only gather the payload size for strings since this is easy to figure out | ||
// and has basically no overhead. | ||
// Getting the size of objects would be more complex, and it would potentially | ||
// introduce more overhead since we don't get the size from the current framework abstraction. | ||
if (\is_string($value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment why we only gather the payload size on strings for now. |
||
return \strlen($value); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @phpstan-param \Closure(CacheItem): CacheItem $callback | ||
* @phpstan-param string $key | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.