@@ -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