# Buggy code
def scrape_prometheus(endpoints, retries=3, err_output_file=sys.stdout):
    """Scrape a list of Prometheus/FAUCET/Gauge endpoints and aggregate results."""
    metrics = []
    for endpoint in endpoints:
        content = None
        err = None
        for _ in range(retries):
            try:
                if endpoint.startswith("http"):
                    response = requests.get(endpoint)
                    if response.status_code == requests.status_codes.codes.ok:  # pylint: disable=no-member
                        content = response.content.decode("utf-8", "strict")
                        break
                else:
                    response = urllib.request.urlopen(
                        endpoint
                    )  # pytype: disable=module-attr
                    content = response.read().decode("utf-8", "strict")
                    break
            except requests.exceptions.ConnectionError as exception:
                err = exception
                time.sleep(1)
        if err is not None:
            err_output_file.write(str(err))
            return None
        endpoint_metrics = parser.text_string_to_metric_families(content)
        metrics.extend(endpoint_metrics)
    return metrics


# Traceback (ValueError)
$ python3 fctl.py -n --endpoints=http://0.0.0.0:9090 --metrics=learned_macs --labels=dp_id:0x1
Traceback (most recent call last):
File "/home/foo/.local/lib/python3.5/site-packages/prometheus_client/parser.py", line 122, in _parse_sample
label_start, label_end = text.index("{"), text.rindex("}")
ValueError: substring not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "fctl.py", line 176, in <module>
main()
File "fctl.py", line 162, in main
metrics = scrape_prometheus(endpoints)
File "fctl.py", line 73, in scrape_prometheus
metrics.extend(endpoint_metrics)
File "/home/foo/.local/lib/python3.5/site-packages/prometheus_client/parser.py", line 22, in text_string_to_metric_families
for metric_family in text_fd_to_metric_families(StringIO.StringIO(text)):
File "/home/foo/.local/lib/python3.5/site-packages/prometheus_client/parser.py", line 217, in text_fd_to_metric_families
sample = _parse_sample(line)
File "/home/foo/.local/lib/python3.5/site-packages/prometheus_client/parser.py", line 140, in _parse_sample
value = float(_parse_value(text[name_end:]))
ValueError: could not convert string to float: 'html>'

# Fixed code
def scrape_prometheus(endpoints, retries=3, err_output_file=sys.stdout):
    """Scrape a list of Prometheus/FAUCET/Gauge endpoints and aggregate results."""
    metrics = []
    for endpoint in endpoints:
        content = None
        err = None
        for _ in range(retries):
            try:
                if endpoint.startswith("http"):
                    response = requests.get(endpoint)
                    if response.status_code == requests.status_codes.codes.ok:  # pylint: disable=no-member
                        content = response.content.decode("utf-8", "strict")
                        break
                else:
                    response = urllib.request.urlopen(
                        endpoint
                    )  # pytype: disable=module-attr
                    content = response.read().decode("utf-8", "strict")
                    break
            except (requests.exceptions.ConnectionError, ValueError) as exception:
                err = exception
                time.sleep(1)
        if err is not None:
            err_output_file.write(str(err))
            return None
        try:
            endpoint_metrics = parser.text_string_to_metric_families(content)
            metrics.extend(endpoint_metrics)
        except ValueError as err:
            err_output_file.write(str(err))
            return None
    return metrics