Skip to main content

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

Project description

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

Devices Description
Mac ✅ Global energy consumption of your Mac (must be plugged into a wall adapter). Supports Apple Silicon GPU power tracking via powermetrics (requires sudo).
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 ❌ Not yet implemented. See #184.
Cloud Provider Description
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

GPU Description
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 ✅ Supported via powermetrics on Mac (requires sudo). Tracks integrated GPU power on M1/M2/M3/M4 chips.
Intel ❌ Not yet implemented.

📡 Exporters

Exporter Description
Stdout Print the metrics in Stdout.
JSON Write the metrics in a JSON file.
Prometheus Send the metrics to Prometheus.
Datadog Send the metrics to Datadog.

🗺️ Locations

Location Description Source
Worldwide Get the latest co2g/kwh in near real-time using the CO2Signal or ElectricityMaps APIs. See here for the list of available zones. CO2Signal API or ElectricityMaps
Europe Static file created from the European Environment Agency Emission for the co2g/kwh in European countries. EEA website
AWS Static file of the AWS Grid emissions factors. cloud-carbon-coefficients
GCP Static file of the GCP Grid emissions factors (2024 yearly data). GoogleCloudPlatform/region-carbon-info
Azure Static file of the Azure Grid emissions factors. cloud-carbon-coefficients

⚙️ Configuration

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

Parameter Description
TRACARBON_CO2SIGNAL_API_KEY The api key received from CO2Signal or ElectricityMaps.
TRACARBON_CO2SIGNAL_URL The 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_NAME The prefix to use in all the metrics name.
TRACARBON_INTERVAL_IN_SECONDS The interval in seconds to wait between the metrics evaluation.
TRACARBON_LOG_LEVEL The level to use for displaying the logs.
TRACARBON_IPINFO_TOKEN An optional ipinfo.io API token used for country detection from the IP address, lifting the anonymous rate limit.

🔎 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

API

from tracarbon import TracarbonBuilder, TracarbonConfiguration

configuration = TracarbonConfiguration() # Your configuration
tracarbon = TracarbonBuilder(configuration=configuration).build()
tracarbon.start()
# Your code
tracarbon.stop()

with tracarbon:
    # Your code

report = tracarbon.report() # Get the report

💻 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tracarbon-0.10.1.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tracarbon-0.10.1-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

Details for the file tracarbon-0.10.1.tar.gz.

File metadata

  • Download URL: tracarbon-0.10.1.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for tracarbon-0.10.1.tar.gz
Algorithm Hash digest
SHA256 caf65bea6284e75f0ddcb17499131fd6a9f85e72b9a6de0d8e4dbbee8b876a9f
MD5 c42fffc81f2f595c060bb3646964df2f
BLAKE2b-256 d4558a221d79bff068cfd3f69ae0b74de55747792ba069efedd91ee5753f560c

See more details on using hashes here.

File details

Details for the file tracarbon-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: tracarbon-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 45.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for tracarbon-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9361b385bed70fe483c6d23f8b4fc322663a24fda72b9789b3b0b9b12be7467
MD5 927401fff15c686e35703c114dee473f
BLAKE2b-256 ddd1ab69bd93d96aa9cf316d8e55299f32e8f86e16e61909b7d85891505556b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page