Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 6754bf4

Browse files
authored
Add a timeout to azure meta data service (#939)
1 parent 862885a commit 6754bf4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

contrib/opencensus-ext-azure/opencensus/ext/azure/metrics_exporter/heartbeat_metrics/heartbeat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ def get_azure_compute_metadata(self):
100100
try:
101101
request_url = "{0}?{1}&{2}".format(
102102
_AIMS_URI, _AIMS_API_VERSION, _AIMS_FORMAT)
103-
response = requests.get(request_url, headers={"MetaData": "True"})
104-
except requests.exceptions.ConnectionError:
103+
response = requests.get(
104+
request_url, headers={"MetaData": "True"}, timeout=5.0)
105+
except (requests.exceptions.ConnectionError, requests.Timeout):
105106
# Not in VM
106107
self.is_vm = False
107108
return False

contrib/opencensus-ext-azure/tests/test_azure_heartbeat_metrics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ def test_heartbeat_metric_not_vm(self):
218218
keys = list(metric.properties.keys())
219219
self.assertEqual(len(keys), 2)
220220

221+
def test_heartbeat_metric_not_vm_timeout(self):
222+
with mock.patch(
223+
'requests.get',
224+
throw(requests.Timeout)
225+
):
226+
metric = heartbeat_metrics.HeartbeatMetric()
227+
self.assertFalse(metric.is_vm)
228+
self.assertEqual(metric.NAME, 'Heartbeat')
229+
keys = list(metric.properties.keys())
230+
self.assertEqual(len(keys), 2)
231+
221232
def test_heartbeat_metric_vm_error_response(self):
222233
with mock.patch('requests.get') as get:
223234
get.return_value = MockResponse(

0 commit comments

Comments
 (0)