From dd8ef2c26cc4c5145da251500281479db3bd9969 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 6 Mar 2025 11:30:41 +0100 Subject: [PATCH 1/2] Added timeout to http requests --- .../integrations/cloud_resource_context.py | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/cloud_resource_context.py b/sentry_sdk/integrations/cloud_resource_context.py index 8d080899f3..e605ab0ea9 100644 --- a/sentry_sdk/integrations/cloud_resource_context.py +++ b/sentry_sdk/integrations/cloud_resource_context.py @@ -13,6 +13,8 @@ CONTEXT_TYPE = "cloud_resource" +HTTP_TIMEOUT = 2.0 + AWS_METADATA_HOST = "169.254.169.254" AWS_TOKEN_URL = "http://{}/latest/api/token".format(AWS_METADATA_HOST) AWS_METADATA_URL = "http://{}/latest/dynamic/instance-identity/document".format( @@ -59,7 +61,7 @@ class CloudResourceContextIntegration(Integration): cloud_provider = "" aws_token = "" - http = urllib3.PoolManager() + http = urllib3.PoolManager(timeout=HTTP_TIMEOUT) gcp_metadata = None @@ -83,7 +85,11 @@ def _is_aws(cls): cls.aws_token = r.data.decode() return True - except Exception: + except urllib3.exceptions.TimeoutError: + logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT) + return False + except Exception as e: + logger.debug("Error checking AWS metadata service: %s", str(e)) return False @classmethod @@ -131,8 +137,10 @@ def _get_aws_context(cls): except Exception: pass - except Exception: - pass + except urllib3.exceptions.TimeoutError: + logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT) + except Exception as e: + logger.debug("Error fetching AWS metadata: %s", str(e)) return ctx @@ -152,7 +160,11 @@ def _is_gcp(cls): cls.gcp_metadata = json.loads(r.data.decode("utf-8")) return True - except Exception: + except urllib3.exceptions.TimeoutError: + logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT) + return False + except Exception as e: + logger.debug("Error checking GCP metadata service: %s", str(e)) return False @classmethod @@ -201,8 +213,10 @@ def _get_gcp_context(cls): except Exception: pass - except Exception: - pass + except urllib3.exceptions.TimeoutError: + logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT) + except Exception as e: + logger.debug("Error fetching GCP metadata: %s", str(e)) return ctx From 6ec46a89d4858bb88dc35b82a8a3a5f660f73210 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 6 Mar 2025 14:13:54 +0100 Subject: [PATCH 2/2] formatting --- .../integrations/cloud_resource_context.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/integrations/cloud_resource_context.py b/sentry_sdk/integrations/cloud_resource_context.py index e605ab0ea9..ca5ae47e6b 100644 --- a/sentry_sdk/integrations/cloud_resource_context.py +++ b/sentry_sdk/integrations/cloud_resource_context.py @@ -86,7 +86,9 @@ def _is_aws(cls): return True except urllib3.exceptions.TimeoutError: - logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT) + logger.debug( + "AWS metadata service timed out after %s seconds", HTTP_TIMEOUT + ) return False except Exception as e: logger.debug("Error checking AWS metadata service: %s", str(e)) @@ -138,7 +140,9 @@ def _get_aws_context(cls): pass except urllib3.exceptions.TimeoutError: - logger.debug("AWS metadata service timed out after %s seconds", HTTP_TIMEOUT) + logger.debug( + "AWS metadata service timed out after %s seconds", HTTP_TIMEOUT + ) except Exception as e: logger.debug("Error fetching AWS metadata: %s", str(e)) @@ -161,7 +165,9 @@ def _is_gcp(cls): return True except urllib3.exceptions.TimeoutError: - logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT) + logger.debug( + "GCP metadata service timed out after %s seconds", HTTP_TIMEOUT + ) return False except Exception as e: logger.debug("Error checking GCP metadata service: %s", str(e)) @@ -214,7 +220,9 @@ def _get_gcp_context(cls): pass except urllib3.exceptions.TimeoutError: - logger.debug("GCP metadata service timed out after %s seconds", HTTP_TIMEOUT) + logger.debug( + "GCP metadata service timed out after %s seconds", HTTP_TIMEOUT + ) except Exception as e: logger.debug("Error fetching GCP metadata: %s", str(e))