Skip to content

Commit 84db2cc

Browse files
committed
test(Integrations: Symfony): Add readyness check on Redis, PDO and Memcached
Signed-off-by: Alexandre Rulleau <alexandre.rulleau@datadoghq.com>
1 parent 2790073 commit 84db2cc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/DDTrace/Integrations/Symfony/SymfonyIntegration.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,59 @@ static function() {
439439
if (!\method_exists($cache, 'getItem')) {
440440
return;
441441
}
442+
443+
// Check if the cache adapter is actually accessible before trying to use it
444+
if (\method_exists($cache, 'getAdapter')) {
445+
try {
446+
$adapter = $cache->getAdapter();
447+
448+
if (
449+
$adapter instanceof \Symfony\Component\Cache\Adapter\RedisAdapter ||
450+
$adapter instanceof \Symfony\Component\Cache\Adapter\RedisTagAwareAdapter
451+
) {
452+
$reflection = new \ReflectionClass($adapter);
453+
$redisProperty = $reflection->getProperty('redis');
454+
$redisProperty->setAccessible(true);
455+
$redis = $redisProperty->getValue($adapter);
456+
457+
if (\method_exists($redis, 'ping')) {
458+
$pong = $redis->ping();
459+
if (
460+
$pong !== true && $pong !== '+PONG' && $pong !== 'PONG' &&
461+
(!is_object($pong) || (string)$pong !== 'PONG')
462+
) {
463+
return;
464+
}
465+
} else {
466+
return;
467+
}
468+
} elseif ($adapter instanceof \Symfony\Component\Cache\Adapter\MemcachedAdapter) {
469+
if (\method_exists($adapter, 'getClient')) {
470+
$client = $adapter->getClient();
471+
// getVersion() return false if the server is not reachable
472+
if (\method_exists($client, 'getVersion')) {
473+
$version = $client->getVersion();
474+
if ($version === false || empty($version)) {
475+
return;
476+
}
477+
}
478+
}
479+
} elseif ($adapter instanceof \Symfony\Component\Cache\Adapter\PdoAdapter) {
480+
if (\method_exists($adapter, 'getConnection')) {
481+
$pdo = $adapter->getConnection();
482+
// getAttribute() throws exception if connection is down
483+
if ($pdo instanceof \PDO) {
484+
$version = $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
485+
if ($version === null || $version === false) {
486+
return;
487+
}
488+
}
489+
}
490+
}
491+
} catch (\Exception $e) {
492+
return;
493+
}
494+
}
442495
$itemName = "_datadog.route.path.$route_name";
443496
$locale = $request->get('_locale');
444497
if ($locale !== null) {

0 commit comments

Comments
 (0)