From 6ff949cb02c2697027f2f4710943deba3d885153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Fri, 13 Dec 2019 14:31:03 +0100 Subject: [PATCH] Handle failure on version endpoint (#5208) To query the version we retrieve the HTML stats page instead of the CSV one, which may not be exposed. Handle failure on that page so that we can keep pushing metrics if it's not available. --- haproxy/datadog_checks/haproxy/haproxy.py | 5 ++++- haproxy/tests/test_unit.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/haproxy/datadog_checks/haproxy/haproxy.py b/haproxy/datadog_checks/haproxy/haproxy.py index aac765f3dfa93..727e78085066a 100644 --- a/haproxy/datadog_checks/haproxy/haproxy.py +++ b/haproxy/datadog_checks/haproxy/haproxy.py @@ -107,7 +107,10 @@ def check(self, instance): info, data = self._fetch_socket_data(parsed_url) self._collect_version_from_socket(info) else: - self._collect_version_from_http(url) + try: + self._collect_version_from_http(url) + except Exception as e: + self.log.warning("Couldn't collect version information: %s", e) data = self._fetch_url_data(url) collect_aggregates_only = instance.get('collect_aggregates_only', True) diff --git a/haproxy/tests/test_unit.py b/haproxy/tests/test_unit.py index 42a54db2747ee..0e45dbf31b791 100644 --- a/haproxy/tests/test_unit.py +++ b/haproxy/tests/test_unit.py @@ -2,6 +2,8 @@ import os from collections import defaultdict +import mock + from . import common BASE_CONFIG = {'url': 'http://localhost/admin?stats', 'collect_status_metrics': True, 'enable_service_check': True} @@ -249,3 +251,19 @@ def test_regex_tags(aggregator, check, haproxy_mock): 'backend:i-1', ] aggregator.assert_service_check('haproxy.backend_up', tags=tags) + + +def test_version_failure(aggregator, check, datadog_agent): + config = copy.deepcopy(BASE_CONFIG) + haproxy_check = check(config) + filepath = os.path.join(common.HERE, 'fixtures', 'mock_data') + with open(filepath, 'rb') as f: + data = f.read() + with mock.patch('requests.get') as m: + m.side_effect = [RuntimeError("Ooops"), mock.Mock(content=data)] + haproxy_check.check(config) + + # Version failed, but we should have some metrics + aggregator.assert_metric('haproxy.count_per_status', value=1, tags=['status:open', 'service:a']) + # But no metadata + datadog_agent.assert_metadata_count(0)