Skip to content

Commit dd8ef2c

Browse files
committed
Added timeout to http requests
1 parent 8672dc1 commit dd8ef2c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

sentry_sdk/integrations/cloud_resource_context.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
CONTEXT_TYPE = "cloud_resource"
1515

16+
HTTP_TIMEOUT = 2.0
17+
1618
AWS_METADATA_HOST = "169.254.169.254"
1719
AWS_TOKEN_URL = "http://{}/latest/api/token".format(AWS_METADATA_HOST)
1820
AWS_METADATA_URL = "http://{}/latest/dynamic/instance-identity/document".format(
@@ -59,7 +61,7 @@ class CloudResourceContextIntegration(Integration):
5961
cloud_provider = ""
6062

6163
aws_token = ""
62-
http = urllib3.PoolManager()
64+
http = urllib3.PoolManager(timeout=HTTP_TIMEOUT)
6365

6466
gcp_metadata = None
6567

@@ -83,7 +85,11 @@ def _is_aws(cls):
8385
cls.aws_token = r.data.decode()
8486
return True
8587

86-
except Exception:
88+
except urllib3.exceptions.TimeoutError:
89+
logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT)
90+
return False
91+
except Exception as e:
92+
logger.debug("Error checking AWS metadata service: %s", str(e))
8793
return False
8894

8995
@classmethod
@@ -131,8 +137,10 @@ def _get_aws_context(cls):
131137
except Exception:
132138
pass
133139

134-
except Exception:
135-
pass
140+
except urllib3.exceptions.TimeoutError:
141+
logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT)
142+
except Exception as e:
143+
logger.debug("Error fetching AWS metadata: %s", str(e))
136144

137145
return ctx
138146

@@ -152,7 +160,11 @@ def _is_gcp(cls):
152160
cls.gcp_metadata = json.loads(r.data.decode("utf-8"))
153161
return True
154162

155-
except Exception:
163+
except urllib3.exceptions.TimeoutError:
164+
logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT)
165+
return False
166+
except Exception as e:
167+
logger.debug("Error checking GCP metadata service: %s", str(e))
156168
return False
157169

158170
@classmethod
@@ -201,8 +213,10 @@ def _get_gcp_context(cls):
201213
except Exception:
202214
pass
203215

204-
except Exception:
205-
pass
216+
except urllib3.exceptions.TimeoutError:
217+
logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT)
218+
except Exception as e:
219+
logger.debug("Error fetching GCP metadata: %s", str(e))
206220

207221
return ctx
208222

0 commit comments

Comments
 (0)