Skip to content

A tool written in go to help you factor carbon intensity into decisions about where and when to run computing jobs.

License

Notifications You must be signed in to change notification settings

thegreenwebfoundation/grid-intensity-go

Repository files navigation

GoDoc go-unit-test docker kubernetes nomad

grid-intensity-go

A tool written in Go, designed to be integrated into Kubernetes, Nomad, and other schedulers, to help you factor carbon intensity into decisions about where and when to run jobs.

The tool has 3 components.

  • The grid-intensity CLI for interacting with carbon intensity data.
  • A Prometheus exporter with carbon intensity metrics that can be deployed via Docker, Nomad, or Kubernetes.
  • A Go library that can be integrated into your Go code.

Changelog

See CHANGELOG.md.

Background

We know that the internet runs on electricity. That electricity comes from a mix of energy sources, including wind and solar, nuclear power, biomass, fossil gas, oil and coal and so on,

We call this the fuel mix, and this fuel mix can impact on the carbon intensity of your code.

Move your code through time and space

Because the fuel mix will be different depending when and where you run your code, you can influence the carbon intensity of the code you write by moving it through time and space - either by making it run when the grid is greener, or making it run where it's greener, like a CDN running on green power.

Inspired By

This tool builds on research and tools developed from across the sustainable software community.

Articles

Papers

Tools

Installing

brew install thegreenwebfoundation/carbon-aware-tools/grid-intensity
  • Install via curl (feel free to do due diligence and check the script first).
curl -fsSL https://raw.githubusercontent.com/thegreenwebfoundation/grid-intensity-go/main/install.sh | sudo sh 
  • Fetch a binary release from the releases page.

grid-intensity CLI

The CLI allows you to interact with carbon intensity data from multiple providers.

$ grid-intensity
Provider ember-climate.org needs an ISO country code as a location parameter.
ESP detected from your locale.
ESP
[
	{
		"emissions_type": "average",
		"metric_type": "absolute",
		"provider": "Ember",
		"location": "ESP",
		"units": "gCO2e per kWh",
		"valid_from": "2021-01-01T00:00:00Z",
		"valid_to": "2021-12-31T23:59:00Z",
		"value": 193.737
	}
]

The --provider and --location flags allow you to select other providers and locations. You can also set the GRID_INTENSITY_PROVIDER and GRID_INTENSITY_LOCATION environment variables or edit the config file at ~/.config/grid-intensity/config.yaml.

$ grid-intensity --provider CarbonIntensityOrgUK --location UK
{
	"from": "2022-07-14T14:30Z",
	"to": "2022-07-14T15:00Z",
	"intensity": {
		"forecast": 184,
		"actual": 194,
		"index": "moderate"
	}
}

The providers section shows how to configure other providers.

grid-intensity exporter

The exporter subcommand starts the prometheus exporter on port 8000.

$ grid-intensity exporter --provider Ember --location FR
Using provider "Ember" with location "FR"
Metrics available at :8000/metrics

View the metrics with curl.

$ curl -s http://localhost:8000/metrics | grep grid
# HELP grid_intensity_carbon_average Average carbon intensity for the electricity grid in this location.
# TYPE grid_intensity_carbon_average gauge
grid_intensity_carbon_average{provider="Ember",location="FR",units="gCO2 per kWh"} 67.781 1718258400000

Note about Prometheus and samples in the past

If you are using the exporter with the ElectricityMaps provider, it will return a value for estimated, which will be the most recent one, and another value for the real value, which can be a few hours in the past. Depending on your Prometheus installation, it could be that the metrics that have a timestamp in the past are not accepted, with an error such as this:

Error on ingesting samples that are too old or are too far into the future

In that case, you can configure the property tsdb.outOfOrderTimeWindow to extend the time window accepted, for example to 3h.

Docker Image

Build the docker image to deploy the exporter.

CGO_ENABLED=0 GOOS=linux go build -o grid-intensity .
docker build -t thegreenwebfoundation/grid-intensity:latest .

Kubernetes

Install the helm chart in /helm/grid-intensity-exporter. Needs the Docker image to be available in the cluster.

helm install --set gridIntensity.location=FR grid-intensity-exporter helm/grid-intensity-exporter

Nomad

Edit the Nomad job in /nomad/grid-intensity-exporter.nomad to set the env vars GRID_INTENSITY_LOCATION and GRID_INTENSITY_PROVIDER

Start the Nomad job. Needs the Docker image to be available in the cluster.

nomad run ./nomad/grid-intensity-exporter.nomad

grid-intensity-go library

See the /examples/ directory for examples of how to integrate each provider.

Providers

Currently these providers of carbon intensity data are integrated. If you would like us to integrate more providers please open an issue.

Electricity Maps

Electricity Maps have carbon intensity data from multiple sources. You need to get an API token and URL from their API portal to use the API. You can use their free tier for non-commercial use or sign up for a 30 day trial.

The location parameter needs to be set to a zone present in the public zones endpoint.

ELECTRICITY_MAPS_API_TOKEN=your-token \
ELECTRICITY_MAPS_API_URL=https://api-access.electricitymaps.com/free-tier/ \
grid-intensity --provider=ElectricityMaps --location=IN-KA

WattTime

WattTime have carbon intensity data from multiple sources. You need to register to use the API.

The location parameter should be set to a supported location. The /ba-from-loc endpoint allows you to provide a latitude and longitude. See the docs for more details.

WATT_TIME_USER=your-user \
WATT_TIME_PASSWORD=your-password \
grid-intensity --provider=WattTime --location=CAISO_NORTH

Ember

Carbon intensity data from Ember, is embedded in the binary in accordance with their licensing - CC-BY-SA 4.0

grid-intensity --provider=Ember --location=DE

The location parameter should be set to a 2 or 3 char ISO country code.

UK Carbon Intensity API

UK Carbon Intensity API https://carbonintensity.org.uk/ this is a public API and the only location supported is UK.

grid-intensity --provider=CarbonIntensityOrgUK --location=UK

About

A tool written in go to help you factor carbon intensity into decisions about where and when to run computing jobs.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published