Skip to content

Implements NamespacedPoolInterface for TraceableCacheAdapterForV3 #927

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 3 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tracing\Cache;

use Sentry\State\HubInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\NamespacedPoolInterface;

/**
* This implementation of a cache adapter supports the distributed tracing
* feature of Sentry.
*
* @internal
*/
final class TraceableCacheAdapterForV3WithNamespace implements AdapterInterface, NamespacedPoolInterface, CacheInterface, PruneableInterface, ResettableInterface
{
/**
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
*/
use TraceableCacheAdapterTrait;

/**
* @param HubInterface $hub The current hub
* @param AdapterInterface $decoratedAdapter The decorated cache adapter
*/
public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapter)
{
$this->hub = $hub;
$this->decoratedAdapter = $decoratedAdapter;
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*/
public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
}, $key);
}

public function withSubNamespace(string $namespace): static

Check warning on line 53 in src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php

View check run for this annotation

Codecov / codecov/patch

src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php#L53

Added line #L53 was not covered by tests
{
if (!$this->decoratedAdapter instanceof NamespacedPoolInterface) {
throw new \BadMethodCallException(\sprintf('The %s::withSubNamespace() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, NamespacedPoolInterface::class));

Check warning on line 56 in src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php

View check run for this annotation

Codecov / codecov/patch

src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php#L55-L56

Added lines #L55 - L56 were not covered by tests
}

$clone = clone $this;
$clone->decoratedAdapter = $this->decoratedAdapter->withSubNamespace($namespace);

Check warning on line 60 in src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php

View check run for this annotation

Codecov / codecov/patch

src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php#L59-L60

Added lines #L59 - L60 were not covered by tests

return $clone;

Check warning on line 62 in src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php

View check run for this annotation

Codecov / codecov/patch

src/Tracing/Cache/TraceableCacheAdapterForV3WithNamespace.php#L62

Added line #L62 was not covered by tests
}
}
8 changes: 7 additions & 1 deletion src/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV2;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3WithNamespace;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapter;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV2;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV3;
Expand Down Expand Up @@ -38,12 +39,17 @@
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Response\StreamableInterface;
use Symfony\Contracts\Cache\NamespacedPoolInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

if (interface_exists(AdapterInterface::class)) {
if (!class_exists(DoctrineProvider::class, false) && version_compare(\PHP_VERSION, '8.0.0', '>=')) {
if (!class_exists(TraceableCacheAdapter::class, false)) {
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
if (interface_exists(NamespacedPoolInterface::class)) {
class_alias(TraceableCacheAdapterForV3WithNamespace::class, TraceableCacheAdapter::class);
} else {
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
}
}

if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Tracing/Cache/TraceableCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Contracts\Cache\NamespacedPoolInterface;

/**
* @phpstan-extends AbstractTraceableCacheAdapterTest<TraceableCacheAdapter, AdapterInterface>
Expand All @@ -27,4 +28,16 @@ protected static function getAdapterClassFqcn(): string
{
return AdapterInterface::class;
}

public function testNamespacePoolImplementation(): void
{
if (!interface_exists(NamespacedPoolInterface::class)) {
$this->markTestSkipped('NamespacedPoolInterface does not exists.');
}

$decoratedAdapter = $this->createMock(static::getAdapterClassFqcn());
$adapter = $this->createCacheAdapter($decoratedAdapter);

static::assertInstanceOf(NamespacedPoolInterface::class, $adapter);
}
}
Loading