Skip to content

Commit b3c75a9

Browse files
Prevent AttributeError when Proxmox Metric Server is not configured (DataDog#22698)
* Handle non-200 HTTP response from /cluster/metrics/export when Proxmox Metric Server is not configured to prevent AttributeError * Add changelog * Lint * Remove unnecessary comments * Refactor the test to use parametrization * Fix formatting
1 parent a426069 commit b3c75a9

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

proxmox/changelog.d/22698.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Handle non-200 HTTP response from /cluster/metrics/export when Proxmox Metric Server is not configured to prevent AttributeError.

proxmox/datadog_checks/proxmox/check.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ def _collect_ha_metrics(self):
224224
def _collect_performance_metrics(self):
225225
self.log.debug("Collecting performance metrics")
226226
metrics_response = self.http.get(f"{self.config.proxmox_server}/cluster/metrics/export")
227+
if not metrics_response.ok:
228+
self.log.warning(
229+
"Performance metrics endpoint not available (HTTP %s), skipping collection",
230+
metrics_response.status_code,
231+
)
232+
return
227233
metrics_response_json = metrics_response.json()
228234
metrics = metrics_response_json.get('data', {}).get('data', [])
229235

proxmox/tests/test_unit.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,29 @@ def test_perf_metrics(dd_run_check, aggregator, instance):
485485
aggregator.assert_metric(metric, count=0, tags=pool_tags)
486486

487487

488+
@pytest.mark.parametrize(
489+
('mock_http_get'),
490+
[
491+
pytest.param(
492+
{'http_error': {'/api2/json/cluster/metrics/export': MockResponse(status_code=501)}},
493+
id='501',
494+
),
495+
],
496+
indirect=['mock_http_get'],
497+
)
498+
@pytest.mark.usefixtures('mock_http_get')
499+
def test_performance_metrics_endpoint_unavailable(dd_run_check, aggregator, instance, mock_http_get):
500+
check = ProxmoxCheck('proxmox', {}, [instance])
501+
dd_run_check(check)
502+
503+
aggregator.assert_metric(
504+
"proxmox.api.up", 1, tags=['proxmox_server:http://localhost:8006/api2/json', 'proxmox_status:up', 'testing']
505+
)
506+
aggregator.assert_metric("proxmox.node.up", 1, tags=[], hostname='ip-122-82-3-112')
507+
aggregator.assert_metric("proxmox.vm.up", 1, tags=[], hostname="debian")
508+
aggregator.assert_metric("proxmox.ha.quorum", hostname='ip-122-82-3-112', tags=['node_status:OK'])
509+
510+
488511
@pytest.mark.usefixtures('mock_http_get')
489512
def test_perf_metrics_error(dd_run_check, caplog, instance):
490513
check = ProxmoxCheck('proxmox', {}, [instance])

0 commit comments

Comments
 (0)