fvaleye/tracarbon

๐ŸŒ Tracarbon tracks your device's energy consumption and calculates your carbon emissions using your location.

111

stars

814

commits

Python

primary language

Sep 7, 2026

updated

fvaleye.github.io/tracarbon/documentation/
carbon-footprint
electricity-consumption
energy
energy-consumption
energy-efficiency
sustainability

README

Tracarbon Logo

example workflow pypi doc licence

๐Ÿ“Œ Overview

Tracarbon is a Python library that tracks your device's energy consumption and calculates your carbon emissions.

It detects your location and your device automatically before starting to export measurements to an exporter. It could be used as a CLI with already defined metrics or programmatically with the API by defining the metrics that you want to have.

Read more in this article.

๐Ÿ“ฆ Where to get it

# Install Tracarbon
pip install tracarbon
# Install one or more exporters from the list
pip install 'tracarbon[datadog,prometheus,kubernetes]'

๐Ÿ”Œ Devices: energy consumption

DevicesDescription
Macโœ… Apple Silicon CPU, GPU, memory and Neural Engine energy via IOReport (no sudo). Excludes display and peripherals. Fallbacks: powermetrics (sudo), then ioreg.
Linuxโœ… Supports Intel and AMD processors via RAPL. Intel uses the powercap interface. AMD is supported on kernel 5.8+ (powercap) or via the amd_energy driver (HWMON). Works with containers on Kubernetes using the Metric API if available.
Windowsโœ… NVIDIA GPU power via nvidia-smi. CPU, memory and host totals are unavailable.
Cloud ProviderDescription
AWSโœ… Use the hardware's usage with the EC2 instances carbon emissions datasets of cloud-carbon-coefficients.
GCPโœ… Use the hardware's usage with the GCP instances carbon emissions datasets of cloud-carbon-coefficients.
Azureโœ… Use the hardware's usage with the Azure instances carbon emissions datasets of cloud-carbon-coefficients.

๐ŸŽฎ GPU: power tracking

GPUDescription
NVIDIAโœ… Supported via nvidia-smi. Works on Linux, Windows, and Intel Macs. Supports multiple GPUs.
AMDโœ… Supported via rocm-smi or amd-smi on Linux. Supports multiple GPUs.
Apple Siliconโœ… Integrated GPU power via IOReport, without sudo. Falls back to powermetrics (requires sudo).
IntelโŒ Not yet implemented.

๐Ÿ“ก Exporters

ExporterDescription
StdoutPrint the metrics in Stdout.
JSONWrite the metrics in a JSON file.
PrometheusSend the metrics to Prometheus.
DatadogSend the metrics to Datadog.

๐Ÿ—บ๏ธ Locations

LocationDescriptionSource
WorldwideGet the latest co2g/kwh in near real-time using the CO2Signal or ElectricityMaps APIs. See here for available Electricity Maps query modes.CO2Signal API or ElectricityMaps
EuropeStatic file created from the European Environment Agency Emission for the co2g/kwh in European countries.EEA website
AWSStatic file of the AWS Grid emissions factors.cloud-carbon-coefficients
GCPStatic file of the GCP Grid emissions factors (2024 yearly data).GoogleCloudPlatform/region-carbon-info
AzureStatic file of the Azure Grid emissions factors.cloud-carbon-coefficients

โš™๏ธ Configuration

The environment variables can be set from an environment file .env.

ParameterDescription
TRACARBON_CO2SIGNAL_API_KEYThe api key received from CO2Signal or ElectricityMaps.
TRACARBON_CO2SIGNAL_URLThe url of CO2Signal is the default endpoint to retrieve the last known state of the zone, but it could be changed to ElectricityMaps.
TRACARBON_METRIC_PREFIX_NAMEThe prefix to use in all the metrics name.
TRACARBON_INTERVAL_IN_SECONDSThe interval in seconds to wait between the metrics evaluation.
TRACARBON_LOG_LEVELThe level to use for displaying the logs.
TRACARBON_IPINFO_TOKENAn optional ipinfo.io API token used for country detection from the IP address, lifting the anonymous rate limit.
TRACARBON_KUBERNETES_NODE_NAMEThe Kubernetes node name used to scope container metrics to the node being measured. Falls back to NODE_NAME when unset.

๐Ÿ”Ž Usage

Request your API key

  • Go to CO2Signal and get your free API key for non-commercial use, or go to ElectricityMaps for commercial use.
  • This API is used to retrieve the last known carbon intensity (in gCO2eq/kWh) of electricity consumed in your location.
  • Set your API key in the environment variables, in the .env file or directly in the configuration.
  • If you would like to start without an API key, it's possible, the carbon intensity will be loaded statistically from a file.
  • Launch Tracarbon ๐Ÿš€

Command Line

tracarbon run

Prometheus with Kubernetes containers

tracarbon run --exporter-name Prometheus --containers

With the default metric prefix, container metrics are exposed with these Prometheus names:

MetricLabels
tracarbon_energy_consumption_kubernetes_totalpod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_energy_consumption_kubernetes_cpupod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_energy_consumption_kubernetes_memorypod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_carbon_emission_kubernetes_totalpod_name, pod_namespace, container_name, platform, containers, location, source, units
tracarbon_carbon_emission_kubernetes_cpupod_name, pod_namespace, container_name, platform, containers, location, source, units
tracarbon_carbon_emission_kubernetes_memorypod_name, pod_namespace, container_name, platform, containers, location, source, units

Zero values are exported. If Kubernetes returns no pod metrics, the CLI logs No Kubernetes container metrics were collected. Host metrics are still exported.

When running in Kubernetes, deploy Tracarbon per node and set NODE_NAME from spec.nodeName with the Downward API so container metrics are scoped to the measured node.

API

from tracarbon import TracarbonBuilder, TracarbonConfiguration

configuration = TracarbonConfiguration() # Your configuration
tracarbon = TracarbonBuilder(configuration=configuration).build()
tracarbon.start()
# Your code
total_co2g = tracarbon.stop() # The CO2 grams emitted while it was running

with tracarbon:
    # Your code

report = tracarbon.report # Get the report
print(report.total_co2g)

total_co2g is None when no host carbon emission metric was collected. The total reflects collected samples.

๐Ÿ’ป Development

Local: using uv

make init
make test-unit

๐Ÿ›ก๏ธ Licence

Apache License 2.0

๐Ÿ“š Documentation

The documentation is hosted here: https://fvaleye.github.io/tracarbon/documentation

๐Ÿ“– Cited in

Contributors

fvaleye

504 commits

dependabot[bot]

308 commits

patgruber

2 commits

fvaleye/tracarbon

๐ŸŒ Tracarbon tracks your device's energy consumption and calculates your carbon emissions using your location.

111

stars

814

commits

Python

primary language

Sep 7, 2026

updated

fvaleye.github.io/tracarbon/documentation/
carbon-footprint
electricity-consumption
energy
energy-consumption
energy-efficiency
sustainability

README

Tracarbon Logo

example workflow pypi doc licence

๐Ÿ“Œ Overview

Tracarbon is a Python library that tracks your device's energy consumption and calculates your carbon emissions.

It detects your location and your device automatically before starting to export measurements to an exporter. It could be used as a CLI with already defined metrics or programmatically with the API by defining the metrics that you want to have.

Read more in this article.

๐Ÿ“ฆ Where to get it

# Install Tracarbon
pip install tracarbon
# Install one or more exporters from the list
pip install 'tracarbon[datadog,prometheus,kubernetes]'

๐Ÿ”Œ Devices: energy consumption

DevicesDescription
Macโœ… Apple Silicon CPU, GPU, memory and Neural Engine energy via IOReport (no sudo). Excludes display and peripherals. Fallbacks: powermetrics (sudo), then ioreg.
Linuxโœ… Supports Intel and AMD processors via RAPL. Intel uses the powercap interface. AMD is supported on kernel 5.8+ (powercap) or via the amd_energy driver (HWMON). Works with containers on Kubernetes using the Metric API if available.
Windowsโœ… NVIDIA GPU power via nvidia-smi. CPU, memory and host totals are unavailable.
Cloud ProviderDescription
AWSโœ… Use the hardware's usage with the EC2 instances carbon emissions datasets of cloud-carbon-coefficients.
GCPโœ… Use the hardware's usage with the GCP instances carbon emissions datasets of cloud-carbon-coefficients.
Azureโœ… Use the hardware's usage with the Azure instances carbon emissions datasets of cloud-carbon-coefficients.

๐ŸŽฎ GPU: power tracking

GPUDescription
NVIDIAโœ… Supported via nvidia-smi. Works on Linux, Windows, and Intel Macs. Supports multiple GPUs.
AMDโœ… Supported via rocm-smi or amd-smi on Linux. Supports multiple GPUs.
Apple Siliconโœ… Integrated GPU power via IOReport, without sudo. Falls back to powermetrics (requires sudo).
IntelโŒ Not yet implemented.

๐Ÿ“ก Exporters

ExporterDescription
StdoutPrint the metrics in Stdout.
JSONWrite the metrics in a JSON file.
PrometheusSend the metrics to Prometheus.
DatadogSend the metrics to Datadog.

๐Ÿ—บ๏ธ Locations

LocationDescriptionSource
WorldwideGet the latest co2g/kwh in near real-time using the CO2Signal or ElectricityMaps APIs. See here for available Electricity Maps query modes.CO2Signal API or ElectricityMaps
EuropeStatic file created from the European Environment Agency Emission for the co2g/kwh in European countries.EEA website
AWSStatic file of the AWS Grid emissions factors.cloud-carbon-coefficients
GCPStatic file of the GCP Grid emissions factors (2024 yearly data).GoogleCloudPlatform/region-carbon-info
AzureStatic file of the Azure Grid emissions factors.cloud-carbon-coefficients

โš™๏ธ Configuration

The environment variables can be set from an environment file .env.

ParameterDescription
TRACARBON_CO2SIGNAL_API_KEYThe api key received from CO2Signal or ElectricityMaps.
TRACARBON_CO2SIGNAL_URLThe url of CO2Signal is the default endpoint to retrieve the last known state of the zone, but it could be changed to ElectricityMaps.
TRACARBON_METRIC_PREFIX_NAMEThe prefix to use in all the metrics name.
TRACARBON_INTERVAL_IN_SECONDSThe interval in seconds to wait between the metrics evaluation.
TRACARBON_LOG_LEVELThe level to use for displaying the logs.
TRACARBON_IPINFO_TOKENAn optional ipinfo.io API token used for country detection from the IP address, lifting the anonymous rate limit.
TRACARBON_KUBERNETES_NODE_NAMEThe Kubernetes node name used to scope container metrics to the node being measured. Falls back to NODE_NAME when unset.

๐Ÿ”Ž Usage

Request your API key

  • Go to CO2Signal and get your free API key for non-commercial use, or go to ElectricityMaps for commercial use.
  • This API is used to retrieve the last known carbon intensity (in gCO2eq/kWh) of electricity consumed in your location.
  • Set your API key in the environment variables, in the .env file or directly in the configuration.
  • If you would like to start without an API key, it's possible, the carbon intensity will be loaded statistically from a file.
  • Launch Tracarbon ๐Ÿš€

Command Line

tracarbon run

Prometheus with Kubernetes containers

tracarbon run --exporter-name Prometheus --containers

With the default metric prefix, container metrics are exposed with these Prometheus names:

MetricLabels
tracarbon_energy_consumption_kubernetes_totalpod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_energy_consumption_kubernetes_cpupod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_energy_consumption_kubernetes_memorypod_name, pod_namespace, container_name, platform, containers, location, units
tracarbon_carbon_emission_kubernetes_totalpod_name, pod_namespace, container_name, platform, containers, location, source, units
tracarbon_carbon_emission_kubernetes_cpupod_name, pod_namespace, container_name, platform, containers, location, source, units
tracarbon_carbon_emission_kubernetes_memorypod_name, pod_namespace, container_name, platform, containers, location, source, units

Zero values are exported. If Kubernetes returns no pod metrics, the CLI logs No Kubernetes container metrics were collected. Host metrics are still exported.

When running in Kubernetes, deploy Tracarbon per node and set NODE_NAME from spec.nodeName with the Downward API so container metrics are scoped to the measured node.

API

from tracarbon import TracarbonBuilder, TracarbonConfiguration

configuration = TracarbonConfiguration() # Your configuration
tracarbon = TracarbonBuilder(configuration=configuration).build()
tracarbon.start()
# Your code
total_co2g = tracarbon.stop() # The CO2 grams emitted while it was running

with tracarbon:
    # Your code

report = tracarbon.report # Get the report
print(report.total_co2g)

total_co2g is None when no host carbon emission metric was collected. The total reflects collected samples.

๐Ÿ’ป Development

Local: using uv

make init
make test-unit

๐Ÿ›ก๏ธ Licence

Apache License 2.0

๐Ÿ“š Documentation

The documentation is hosted here: https://fvaleye.github.io/tracarbon/documentation

๐Ÿ“– Cited in

Contributors

fvaleye

504 commits

dependabot[bot]

308 commits

patgruber

2 commits

Languages

Python

99.2%