diff --git a/elastic/changelog.d/19249.fixed b/elastic/changelog.d/19249.fixed new file mode 100644 index 0000000000000..f82f8964ea031 --- /dev/null +++ b/elastic/changelog.d/19249.fixed @@ -0,0 +1 @@ +Regression fix: handle missing index fields in payload. diff --git a/elastic/datadog_checks/elastic/elastic.py b/elastic/datadog_checks/elastic/elastic.py index cb0b1cbd012de..16165921847b5 100644 --- a/elastic/datadog_checks/elastic/elastic.py +++ b/elastic/datadog_checks/elastic/elastic.py @@ -261,7 +261,7 @@ def _get_index_metrics(self, admin_forwarder, version, base_tags): index_data['health_reverse'] = dd_health.reverse_status # Ensure that index_data does not contain None values - for key, value in index_data.items(): + for key, value in list(index_data.items()): if value is None: del index_data[key] self.log.debug("The index %s has no metric data for %s", idx['index'], key) diff --git a/elastic/tests/test_unit.py b/elastic/tests/test_unit.py index 214d49b27e56d..ce0ca93e21346 100644 --- a/elastic/tests/test_unit.py +++ b/elastic/tests/test_unit.py @@ -11,7 +11,7 @@ from datadog_checks.dev.http import MockResponse from datadog_checks.elastic import ESCheck from datadog_checks.elastic.elastic import AuthenticationError, get_value_from_path -from datadog_checks.elastic.metrics import stats_for_version +from datadog_checks.elastic.metrics import INDEX_STATS_METRICS, stats_for_version from .common import URL, get_fixture_path @@ -242,3 +242,33 @@ def test_v8_process_stats_data(aggregator, instance): aggregator.assert_metric( "elasticsearch.breakers.inflight_requests.estimated_size_in_bytes", metric_type=aggregator.GAUGE ) + + +def test__get_index_metrics_empty_key(aggregator, instance, mock_http_response): + mock_http_response( + json_data=[ + { + # 'docs.count' is missing + 'docs.deleted': '0', + 'health': 'yellow', + 'index': 'testindex', + 'pri': '1', + 'pri.store.size': '225', + 'rep': '1', + 'status': 'open', + 'store.size': '225', + 'uuid': 'AHSf1ILbSHucwl2X6og55g', + }, + ] + ) + check = ESCheck('elastic', {}, instances=[instance]) + # Focus only on index metrics, so mock out index search stats. + check._get_index_search_stats = mock.MagicMock() + + check._get_index_metrics(admin_forwarder=False, version=[8, 8, 2], base_tags=[]) + + for m in INDEX_STATS_METRICS: + if m == 'elasticsearch.index.docs.count': + aggregator.assert_metric(m, count=0) + else: + aggregator.assert_metric(m)