Skip to content

Allow custom carbon intensity configuration#863

Open
benoit-cty wants to merge 1 commit intomasterfrom
feat/custom-carbon-intensity
Open

Allow custom carbon intensity configuration#863
benoit-cty wants to merge 1 commit intomasterfrom
feat/custom-carbon-intensity

Conversation

@benoit-cty
Copy link
Copy Markdown
Contributor

This PR introduces a new configuration option, custom_carbon_intensity_g_co2e_kwh, allowing you to specify a direct carbon intensity value (in gCO2e/kWh) for your energy consumption.

When this value is provided (either via the .codecarbon.config file or the CODECARBON_CUSTOM_CARBON_INTENSITY_G_CO2E_KWH environment variable), it overrides all other methods of determining carbon intensity, including cloud provider data, CO2 Signal API, and default geographical energy mixes.

Key changes include:

  • Modifications to codecarbon/core/config.py to recognize the new configuration parameter (though existing mechanisms were largely sufficient).
  • Updates to codecarbon/emissions_tracker.py (specifically BaseEmissionsTracker) to read, validate (must be a positive float), and pass the custom intensity to the Emissions class.
  • Updates to codecarbon/core/emissions.py to use this custom intensity value in get_cloud_emissions and get_private_infra_emissions if provided, bypassing other data lookups.
  • Comprehensive unit tests added to:
    • tests/core/test_config.py for configuration loading.
    • tests/test_tracker.py for validation and initialization logic in BaseEmissionsTracker.
    • tests/core/test_emissions.py for the Emissions class calculation logic using the custom value and fallback mechanisms.

This feature provides you with greater flexibility and accuracy in reporting emissions, especially for on-premise setups with specific energy mixes or when default data sources are not representative.

This commit introduces a new configuration option, `custom_carbon_intensity_g_co2e_kwh`,
allowing you to specify a direct carbon intensity value (in gCO2e/kWh) for your
energy consumption.

When this value is provided (either via the `.codecarbon.config` file or the
`CODECARBON_CUSTOM_CARBON_INTENSITY_G_CO2E_KWH` environment variable),
it overrides all other methods of determining carbon intensity, including
cloud provider data, CO2 Signal API, and default geographical energy mixes.

Key changes include:
- Modifications to `codecarbon/core/config.py` to recognize the new
  configuration parameter (though existing mechanisms were largely sufficient).
- Updates to `codecarbon/emissions_tracker.py` (specifically
  `BaseEmissionsTracker`) to read, validate (must be a positive float),
  and pass the custom intensity to the `Emissions` class.
- Updates to `codecarbon/core/emissions.py` to use this custom intensity
  value in `get_cloud_emissions` and `get_private_infra_emissions` if provided,
  bypassing other data lookups.
- Comprehensive unit tests added to:
    - `tests/core/test_config.py` for configuration loading.
    - `tests/test_tracker.py` for validation and initialization logic in
      `BaseEmissionsTracker`.
    - `tests/core/test_emissions.py` for the `Emissions` class calculation
      logic using the custom value and fallback mechanisms.

This feature provides you with greater flexibility and accuracy in
reporting emissions, especially for on-premise setups with specific energy
mixes or when default data sources are not representative.
@benoit-cty benoit-cty force-pushed the feat/custom-carbon-intensity branch from 21a9b0f to 5a523de Compare May 10, 2026 12:12
@benoit-cty benoit-cty requested a review from a team as a code owner May 10, 2026 12:12
@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

❌ Patch coverage is 82.60870% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.81%. Comparing base (e3bb7b1) to head (5a523de).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
codecarbon/emissions_tracker.py 75.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #863      +/-   ##
==========================================
- Coverage   88.85%   88.81%   -0.04%     
==========================================
  Files          45       45              
  Lines        4279     4302      +23     
==========================================
+ Hits         3802     3821      +19     
- Misses        477      481       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@SaboniAmine SaboniAmine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Collaborator

@inimaz inimaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @benoit-cty!

Comment thread codecarbon/emissions_tracker.py Outdated

# logger.info("base tracker init")
self._external_conf = get_hierarchical_config()
# self._external_conf = get_hierarchical_config() # Moved to the top
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# self._external_conf = get_hierarchical_config() # Moved to the top

Remove this instead of commenting n?

Comment thread tests/test_config.py Outdated
conf = dict(get_hierarchical_config())
target = {
"allow_multiple_runs": "True",
# "allow_multiple_runs": "True", # Removed
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Suggested change
# "allow_multiple_runs": "True", # Removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants