feat: add cloud_detection switch and probe providers concurrently - #1359
Open
davidberenstein1957 wants to merge 2 commits into
Open
feat: add cloud_detection switch and probe providers concurrently#1359davidberenstein1957 wants to merge 2 commits into
davidberenstein1957 wants to merge 2 commits into
Conversation
Probe the cloud metadata services concurrently instead of one after another, so detection costs one timeout rather than one per provider on machines that are not on a cloud. Resolution stays in mapping order, so the detected provider does not depend on which probe returns first. Add a `cloud_detection` option (constructor, config file and CODECARBON_CLOUD_DETECTION) to skip the probe entirely, for air-gapped and egress-filtered environments. Closes #1352 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1359 +/- ##
==========================================
+ Coverage 91.39% 91.45% +0.06%
==========================================
Files 49 49
Lines 5056 5069 +13
==========================================
+ Hits 4621 4636 +15
+ Misses 435 433 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
davidberenstein1957
marked this pull request as ready for review
August 12, 2026 19:14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
Two small changes to the cloud metadata detection path.
Concurrent probing.
get_env_cloud_details()walkedCLOUD_METADATA_MAPPINGsequentially with a 1 s timeout per provider. All three entries live on169.254.169.254, so any machine where that address is not routed paid up to three consecutive timeouts at tracker startup before concluding it is not on a cloud. The loop now fans out over aThreadPoolExecutor(stdlib, no new dependency) and detection costs about one timeout regardless of how many providers are in the mapping. Results are resolved in mapping order rather than completion order, so the provider reported for a machine that answers on more than one entry is the same as before.A
cloud_detectionswitch. New tracker option, wired through_set_from_conflike every other one, so it works three ways:export CODECARBON_CLOUD_DETECTION=falseDefault is
true, so nothing changes for existing users. When disabled,EmissionsTracker._get_cloud_metadata()returns an emptyCloudMetadatawithout any HTTP call and the tracker takes the normal geolocation path. This is aimed at air-gapped and egress-filtered environments, where the probe today is a guaranteed-failing request with no way to opt out short of switching toOfflineEmissionsTracker.The public return shape of
get_env_cloud_details()and theCloudMetadatadataclass are unchanged, as is the GCPpostprocess_functionthat stripsattributes(Kubernetes config and secrets).Verification
uv run pytest tests/test_cloud.py tests/test_geography.py tests/test_config.py tests/test_emissions_tracker.pypasses. Four new tests intests/test_cloud.py, none of them touching the network:cloud_detection=False,The existing per-provider
responses-based tests are untouched and still pass, which covers the refactor of the probe itself.black --checkis clean on the four touched files.uv run task lintwas not run: it currently reports pre-existing findings across the whole repository unrelated to this change.Deliberately left out
The proposal this came from also covered broader vendor support and region-level intensity. Both are better argued separately and neither is needed for the wins above:
codecarbon/data/cloud/impact.csv. The schema already supports it; the cost is data curation and provenance, not code.carbon_intensity_sourcefield onEmissionsDatarecording which fallback rung produced the intensity.track_emissionsdecorator: the new option is not exposed as a decorator keyword, since the config file and environment variable already cover it.Closes #1352
🤖 Generated with Claude Code