Skip to content

Commit

Permalink
Handle failure on version endpoint (#5208)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
therve committed Dec 13, 2019
1 parent 912fa52 commit 6ff949c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion haproxy/datadog_checks/haproxy/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions haproxy/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)

0 comments on commit 6ff949c

Please sign in to comment.