# Buggy code
def _get_conn(self, instance):
    key = self._generate_instance_key(instance)
    if key not in self.connections:
        try:
            # Only send useful parameters to the redis client constructor
            list_params = [
                "host",
                "port",
                "db",
                "password",
                "socket_timeout",
                "connection_pool",
                "charset",
                "errors",
                "unix_socket_path",
                "ssl",
                "ssl_certfile",
                "ssl_keyfile",
                "ssl_ca_certs",
                "ssl_cert_reqs",
            ]

            # Set a default timeout (in seconds) if no timeout is specified in the instance config
            instance["socket_timeout"] = instance.get("socket_timeout", 5)

            connection_params = dict(
                (k, instance[k]) for k in list_params if k in instance
            )

            self.connections[key] = redis.Redis(**connection_params)

        except TypeError:
            msg = "You need a redis library that supports authenticated connections. Try sudo easy_install redis."
            raise Exception(msg)

    return self.connections[key]


# Traceback (ConnectionError)
2018-06-04 11:08:37 UTC | INFO | dd.collector | config(config.py:1249) | initialized checks.d checks: ['system_core', 'network', 'kubernetes', 'redisdb', 'ntp', 'disk', 'kube_proxy', 'docker_daemon', 'http_check']
2018-06-04 11:08:37 UTC | INFO | dd.collector | config(config.py:1250) | initialization failed checks.d checks: []
2018-06-04 11:08:37 UTC | INFO | dd.collector | collector(agent.py:166) | Check reload was successful. Running 10 checks.
2018-06-04 11:08:51 UTC | ERROR | dd.collector | checks.redisdb(__init__.py:829) | Check 'redisdb' instance #0 failed
Traceback (most recent call last):
File "/opt/datadog-agent/agent/checks/__init__.py", line 812, in run
self.check(copy.deepcopy(instance))
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/redisdb/redisdb.py", line 377, in check
self._check_db(instance, custom_tags)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/redisdb/redisdb.py", line 173, in _check_db
info = conn.info()
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/redis/client.py", line 665, in info
return self.execute_command('INFO')
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/redis/client.py", line 578, in execute_command
connection.send_command(*args)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/redis/connection.py", line 563, in send_command
self.send_packed_command(self.pack_command(*args))
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/redis/connection.py", line 538, in send_packed_command
self.connect()
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error connecting to 10.0.0.4:6379. timed out.
2018-06-04 11:08:52 UTC | INFO | dd.collector | checks.http_check(network_checks.py:93) | Starting Thread Pool

# Fixed code
def _get_conn(self, instance):
    no_cache = is_affirmative(instance.get("disable_connection_cache", False))
    key = self._generate_instance_key(instance)

    if no_cache or key not in self.connections:
        try:
            # Only send useful parameters to the redis client constructor
            list_params = [
                "host",
                "port",
                "db",
                "password",
                "socket_timeout",
                "connection_pool",
                "charset",
                "errors",
                "unix_socket_path",
                "ssl",
                "ssl_certfile",
                "ssl_keyfile",
                "ssl_ca_certs",
                "ssl_cert_reqs",
            ]

            # Set a default timeout (in seconds) if no timeout is specified in the instance config
            instance["socket_timeout"] = instance.get("socket_timeout", 5)
            connection_params = dict(
                (k, instance[k]) for k in list_params if k in instance
            )
            # If caching is disabled, we overwrite the dictionary value so the old connection
            # will be closed as soon as the corresponding Python object gets garbage collected
            self.connections[key] = redis.Redis(**connection_params)

        except TypeError:
            msg = "You need a redis library that supports authenticated connections. Try sudo easy_install redis."
            raise Exception(msg)

    return self.connections[key]