Skip to content

Added timeout to HTTP requests in CloudResourceContextIntegration #4120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions sentry_sdk/integrations/cloud_resource_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -59,7 +61,7 @@ class CloudResourceContextIntegration(Integration):
cloud_provider = ""

aws_token = ""
http = urllib3.PoolManager()
http = urllib3.PoolManager(timeout=HTTP_TIMEOUT)

gcp_metadata = None

Expand All @@ -83,7 +85,13 @@ 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
Expand Down Expand Up @@ -131,8 +139,12 @@ 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

Expand All @@ -152,7 +164,13 @@ 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
Expand Down Expand Up @@ -201,8 +219,12 @@ 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

Expand Down
Loading