# Buggy code
def _get_replica_stats(self, db, is_mariadb, replication_channel):
    replica_results = {}
    try:
        with closing(db.cursor(pymysql.cursors.DictCursor)) as cursor:
            if is_mariadb and replication_channel:
                cursor.execute(
                    "SET @@default_master_connection = '{0}';".format(
                        replication_channel
                    )
                )
                cursor.execute("SHOW SLAVE STATUS;")
            elif replication_channel:
                cursor.execute(
                    "SHOW SLAVE STATUS FOR CHANNEL '{0}';".format(replication_channel)
                )
            else:
                cursor.execute("SHOW SLAVE STATUS;")

            if replication_channel:
                slave_results = cursor.fetchone()
            else:
                slave_results = cursor.fetchall()

            if slave_results:
                if replication_channel:
                    replica_results.update(slave_results)
                elif len(slave_results) > 0:
                    for slave_result in slave_results:
                        # MySQL <5.7 does not have Channel_Name.
                        # For MySQL >=5.7 'Channel_Name' is set to an empty string by default
                        channel = slave_result.get("Channel_Name") or "default"
                        for key in slave_result:
                            if slave_result[key] is not None:
                                if key not in replica_results:
                                    replica_results[key] = {}
                                replica_results[key]["channel:{0}".format(channel)] = (
                                    slave_result[key]
                                )
    except (pymysql.err.InternalError, pymysql.err.OperationalError) as e:
        errno, msg = e.args
        if errno == 1617 and msg == "There is no master connection '{0}'".format(
            replication_channel
        ):
            # MariaDB complains when you try to get slave status with a
            # connection name on the master, without connection name it
            # responds an empty string as expected.
            # Mysql behaves the same with or without connection name.
            pass
        else:
            self.warning(
                "Privileges error getting replication status (must grant REPLICATION CLIENT): %s"
                % str(e)
            )

    try:
        with closing(db.cursor(pymysql.cursors.DictCursor)) as cursor:
            cursor.execute("SHOW MASTER STATUS;")
            binlog_results = cursor.fetchone()
            if binlog_results:
                replica_results.update({"Binlog_enabled": True})
    except (pymysql.err.InternalError, pymysql.err.OperationalError) as e:
        self.warning(
            "Privileges error getting binlog information (must grant REPLICATION CLIENT): %s"
            % str(e)
        )

    return replica_results


# Traceback (ValueError)
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:702) | Collecting data with performance_schema
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:706) | Collecting done, value OFF
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:653) | MySQL version ['5', '7', '20']
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:702) | Collecting data with Slave_running
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:704) | Slave_running returned None
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:702) | Collecting data with Slaves_connected
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:706) | Collecting done, value 0
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:653) | MySQL version ['5', '7', '20']
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:702) | Collecting data with Slave_IO_Running
2018-02-16 06:40:03 PST | DEBUG | dd.collector | checks.mysql(mysql.py:706) | Collecting done, value Yes
2018-02-16 06:40:03 PST | ERROR | dd.collector | checks.mysql(mysql.py:313) | error!
Traceback (most recent call last):
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/mysql/mysql.py", line 306, in check
self._collect_metrics(host, db, tags, options, queries)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/mysql/mysql.py", line 550, in _collect_metrics
slave_io_running = self._collect_type('Slave_IO_Running', results, dict)
File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/mysql/mysql.py", line 707, in _collect_type
return the_type(dict[key])
ValueError: dictionary update sequence element #0 has length 1; 2 is required
2018-02-16 06:40:03 PST | ERROR | dd.collector | checks.mysql(__init__.py:829) | Check 'mysql' 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/mysql/mysql.py", line 314, in check
raise e
ValueError: dictionary update sequence element #0 has length 1; 2 is required

# Fixed code
def _get_replica_stats(self, db, is_mariadb, replication_channel):
    replica_results = defaultdict(dict)
    try:
        with closing(db.cursor(pymysql.cursors.DictCursor)) as cursor:
            if is_mariadb and replication_channel:
                cursor.execute(
                    "SET @@default_master_connection = '{0}';".format(
                        replication_channel
                    )
                )
                cursor.execute("SHOW SLAVE STATUS;")
            elif replication_channel:
                cursor.execute(
                    "SHOW SLAVE STATUS FOR CHANNEL '{0}';".format(replication_channel)
                )
            else:
                cursor.execute("SHOW SLAVE STATUS;")

            for slave_result in cursor.fetchall():
                # MySQL <5.7 does not have Channel_Name.
                # For MySQL >=5.7 'Channel_Name' is set to an empty string by default
                channel = (
                    replication_channel or slave_result.get("Channel_Name") or "default"
                )
                for key, value in iteritems(slave_result):
                    if value is not None:
                        replica_results[key]["channel:{0}".format(channel)] = value
    except (pymysql.err.InternalError, pymysql.err.OperationalError) as e:
        errno, msg = e.args
        if errno == 1617 and msg == "There is no master connection '{0}'".format(
            replication_channel
        ):
            # MariaDB complains when you try to get slave status with a
            # connection name on the master, without connection name it
            # responds an empty string as expected.
            # Mysql behaves the same with or without connection name.
            pass
        else:
            self.warning(
                "Privileges error getting replication status (must grant REPLICATION CLIENT): %s"
                % str(e)
            )

    try:
        with closing(db.cursor(pymysql.cursors.DictCursor)) as cursor:
            cursor.execute("SHOW MASTER STATUS;")
            binlog_results = cursor.fetchone()
            if binlog_results:
                replica_results.update({"Binlog_enabled": True})
    except (pymysql.err.InternalError, pymysql.err.OperationalError) as e:
        self.warning(
            "Privileges error getting binlog information (must grant REPLICATION CLIENT): %s"
            % str(e)
        )

    return replica_results