# Buggy code
def _metric_tags(self, metric_name, val, sample, scraper_config, hostname=None):
    custom_tags = scraper_config["custom_tags"]
    _tags = list(custom_tags)
    _tags.extend(scraper_config["_metric_tags"])
    for label_name, label_value in iteritems(sample[self.SAMPLE_LABELS]):
        if label_name not in scraper_config["exclude_labels"]:
            tag_name = scraper_config["labels_mapper"].get(label_name, label_name)
            _tags.append("{}:{}".format(tag_name, label_value))
    return self._finalize_tags_to_submit(
        _tags, metric_name, val, sample, custom_tags=custom_tags, hostname=hostname
    )


# Traceback (nUnicodeEncodeError)
[ AGENT ] 2018-08-14 19:31:51 UTC | ERROR | (runner.go:277 in work) | Error running check prometheus: [{"message": "'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)", "traceback": "Traceback (most recent call last):\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/base.py\", line 303, in run\n    self.check(copy.deepcopy(self.instances[0]))\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/base_check.py\", line 105, in check\n    ignore_unmapped=True\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/mixins.py\", line 385, in process\n    self.process_metric(metric, **kwargs)\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/mixins.py\", line 466, in process_metric\n    self._submit(message.name, message, send_histograms_buckets, send_monotonic_counter, custom_tags)\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/mixins.py\", line 566, in _submit\n    self._submit_gauges_from_histogram(metric_name, metric, send_histograms_buckets, custom_tags, custom_hostname)\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/mixins.py\", line 634, in _submit_gauges_from_histogram\n    self._submit_gauge(\"{}.count\".format(name), val, metric, custom_tags)\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/base_check.py\", line 41, in _submit_gauge\n    _tags = self._metric_tags(metric_name, val, metric, custom_tags, hostname)\n  File \"/opt/datadog-agent/embedded/lib/python2.7/site-packages/datadog_checks/checks/prometheus/base_check.py\", line 65, in _metric_tags\n    _tags.append('{}:{}'.format(tag_name, label.value))\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)\n"}]

# Fixed code
def _metric_tags(self, metric_name, val, sample, scraper_config, hostname=None):
    custom_tags = scraper_config["custom_tags"]
    _tags = list(custom_tags)
    _tags.extend(scraper_config["_metric_tags"])
    for label_name, label_value in iteritems(sample[self.SAMPLE_LABELS]):
        if label_name not in scraper_config["exclude_labels"]:
            tag_name = scraper_config["labels_mapper"].get(label_name, label_name)
            _tags.append(
                "{}:{}".format(ensure_unicode(tag_name), ensure_unicode(label_value))
            )
    return self._finalize_tags_to_submit(
        _tags, metric_name, val, sample, custom_tags=custom_tags, hostname=hostname
    )