Skip to content

Commit 5985b57

Browse files
authored
Use a shorter timeout for the metadata endpoint (#362)
* Use a shorter read timeout for the metadata endpoint Use a shorter timeout of 2 seconds instead of the read of 5 seconds to avoid blocking too much if the metadata server is not available. This makes the code behave more similar to the go one: https://github.com/googleapis/google-cloud-go/blob/main/compute/metadata/metadata.go#L65 * Add changelog
1 parent 1f17758 commit 5985b57

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

opentelemetry-resourcedetector-gcp/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
6+
- Use a shorter connection timeout for reading metadata
7+
([#362](https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/pull/362))
8+
59
## Version 1.7.0a0
610

711
Released 2024-08-27

opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector/_metadata.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
_INSTANCE = "instance"
2626
_RECURSIVE_PARAMS = {"recursive": "true"}
2727
_GCP_METADATA_URL_HEADER = {"Metadata-Flavor": "Google"}
28-
_TIMEOUT_SEC = 5
28+
# Use a shorter timeout for connection so we won't block much if it's unreachable
29+
_TIMEOUT = (2, 5)
2930

3031
_logger = logging.getLogger(__name__)
3132

@@ -69,7 +70,7 @@ def get_metadata() -> Metadata:
6970
f"{_GCP_METADATA_URL}",
7071
params=_RECURSIVE_PARAMS,
7172
headers=_GCP_METADATA_URL_HEADER,
72-
timeout=_TIMEOUT_SEC,
73+
timeout=_TIMEOUT,
7374
)
7475
res.raise_for_status()
7576
all_metadata = res.json()
@@ -84,7 +85,7 @@ def is_available() -> bool:
8485
requests.get(
8586
f"{_GCP_METADATA_URL}{_INSTANCE}/",
8687
headers=_GCP_METADATA_URL_HEADER,
87-
timeout=_TIMEOUT_SEC,
88+
timeout=_TIMEOUT,
8889
).raise_for_status()
8990
except requests.RequestException:
9091
_logger.debug(

0 commit comments

Comments
 (0)