Skip to content

Commit 4879a26

Browse files
committed
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
1 parent 1f17758 commit 4879a26

File tree

1 file changed

+4
-3
lines changed
  • opentelemetry-resourcedetector-gcp/src/opentelemetry/resourcedetector/gcp_resource_detector

1 file changed

+4
-3
lines changed

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)