Skip to content

Commit

Permalink
Adjust endpoint detection logic for warn log (#11567)
Browse files Browse the repository at this point in the history
* change logic for endpoint detection

* check additional endpoints

* created helper function and wrote unit test for helper function

* rename function
  • Loading branch information
steveny91 authored Feb 24, 2022
1 parent d24c7e6 commit e0420b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nginx/datadog_checks/nginx/nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def _get_plus_api_data(self, endpoint, nest):
r = self._perform_request(url)
payload = self._nest_payload(nest, r.json())
except Exception as e:
if not self.only_query_enabled_endpoints and endpoint in PLUS_API_STREAM_ENDPOINTS.values():
plus_endpoints = self.list_endpoints(PLUS_API_STREAM_ENDPOINTS)
if not self.only_query_enabled_endpoints and endpoint in plus_endpoints:
self.log.warning(
"Error querying %s metrics at %s: %s. Stream may not be initialized, "
"you can avoid this error by enabling `only_query_enabled_endpoints` option.",
Expand Down Expand Up @@ -416,3 +417,7 @@ def _normalize_tags_type(self, tags, device_name=None, metric_name=None):
continue
normalized_tags.extend(list({tag, self.degeneralise_tag(tag)}))
return normalized_tags

def list_endpoints(self, api_dict_list):
endpoints = [endpoint for api_dict in list(api_dict_list.values()) for endpoint in api_dict.keys()]
return endpoints
38 changes: 38 additions & 0 deletions nginx/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,41 @@ def mock_get_return(*args, **kwargs):
endpoints = check._get_enabled_endpoints()
expected_endpoints = [('nginx', []), ('http/requests', ['requests'])]
assert sorted(expected_endpoints) == sorted(list(endpoints))


@pytest.mark.parametrize(
'test_input, expected_output',
[
(
{
'1': {
"stream/server_zones": ["stream", "server_zones"],
"stream/upstreams": ["stream", "upstreams"],
},
'3': {
"stream/zone_sync": ["stream", "zone_sync"],
},
'6': {
"stream/limit_conns": ["stream", "limit_conns"],
},
},
['stream/server_zones', 'stream/upstreams', 'stream/zone_sync', 'stream/limit_conns'],
),
(
{
'foo': {"biz": ["stream1"], "buz": ["stream1", "stream2"], "bes": "stream3"},
"baz": {"bux": "zone_sync", "bus": "zone_sync"},
"bar": {
"bis": ["stream1", "stream2", "stream3"],
},
},
['biz', 'buz', 'bes', 'bux', 'bus', 'bis'],
),
],
)
def test_list_endpoints(instance, test_input, expected_output):
nginx = Nginx('nginx', {}, [instance])
# Python 2 seems to have some different order of processing the keys.
# Sorting the arrays before comparison to account for this.
sorted_test_output = nginx.list_endpoints(test_input).sort()
assert eval(str(sorted_test_output)) == expected_output.sort()

0 comments on commit e0420b8

Please sign in to comment.