Recently Iāve been doing a bit of writing for The Green Web Foundation. Most of that has involved building better, more beginner-friendly documentation for some of the foundationās open-source code libraries.
One of those libraries, the Grid Intensity CLI, aims to provide developers with global grid intensity data from a range of providers. In doing so, it can enable developers to surface, monitor, and understand the carbon intensity of the code they write. This, in turn, allows them to make decisions on when/where to run their code so that it uses as much green energy as possible.
Section titled The Grid Intensity CLIThe Grid Intensity CLI
The Grid Intensity CLI is a library written in Go. At itās very basic, the CLI can be used to return the carbon intensity data of electricity grids around the world. It does this by leveraging APIs of several providers including Ember, Electricity Maps, and WattTime.
At a more practical level, the CLI comes with a data exporter. This can be used to spin up a server that exposes data to graphing, scraping, and monitoring tools. Setting this up on the servers or clusters that are being used for a site or app can allow operations teams to gain insights into the carbon intensity of the code they run. This makes it possible for them to then influence the carbon intensity of that code by moving it through time and space.
Section titled Moving through time
Sometime codeĀ needsĀ to be run in a particular region. In these instances, developers can use grid information to identify periods of low carbon intensity. These are times when more green or renewable electricity is in the fuel mix. Scheduled jobs or heavy computational tasks can then be run during periods of low grid intensity, making them less carbon intensive.
Section titled Moving through space
Increasingly, code is deployed to multiple regions around the world. In this scenario, developers can use the Grid Intensity CLI to easily consolidate carbon intensity data for different regions into a single dashboard or dataset.
With this information at hand, they can look for ways to run code in locations with a greener fuel mix. This can be extended to smarter, carbon-aware routing so that more requests are directed to code running in regions with a lower carbon intensity.
Section titled Using the Grid Intensity CLIUsing the Grid Intensity CLI
Section titled InstallingInstalling
You can install the Grid Intensity CLI locally to try it out. If youāre familiar with using the terminal, then running the curl command below is all you need to get going.
curl -fsSL <https://raw.githubusercontent.com/thegreenwebfoundation/grid-intensity-go/install-script/install.sh> | sudo sh
Section titled Getting dataGetting data
Once youāve installed the CLI, you can run grid-intensity --region=TW to get the last calendar yearās grid intensity data for Taiwan.
By default, the Grid Intensity CLI uses data from Ember Climate. You can change the value passed to the --region flag to return data for different parts of the world. With Ember, youāll need to use an Alpha-2 or Alpha-3 ISO country code.
Section titled Changing providerChanging provider
Ember provides historical data. However, if we want to get more recent data for a particular region then we can use one of the other provider integrations.
Letās say we have a server in the UK, and want to know the latest grid intensity we can use take the UK Carbon Intensity API for a spin. Running grid-intensity --provider=carbonintensity.org.uk --region=UK in the terminal will bring back the latest intensity data (which is updated every half hour).
The docs have more details on the other providers available with the CLI.
Section titled Exporting dataExporting data
Running these commands in the terminal is all well and good, but in the real world you probably want to expose this data to some monitoring or decision-making systems. The Grid Intensity CLI has an exporter command for doing just this.
Running the grid-intensity exporter --provider=carbonintensity.org.uk --region=UK command will start aĀ PrometheusĀ server on localhost port 8000. Once the server has started, you can go to localhost:8000/metrics where youāll be presented with a page full of data and stats. Doing a search for grid_intensity_carbon_average will bring up the grid intensity information for the UK. Since the UK Carbon Intensity API is updated every 30 minutes, you can leave the server running & refresh it later to see updated results.
To set this up in production, you can use Docker toĀ deploy the exporterĀ to aĀ KubernetesĀ orĀ NomadĀ cluster. Prometheus can also be used as a data source forĀ Grafana visualisations. None of these are things Iām overly familiar with, so I canāt go into more detail.
Section titled Learning moreLearning more
Documentation for the Grid Intensity CLI can be found on Developer Docs @ The Green Web Foundations. You can also check out the repository on GitHub.
Itās relatively early days, and what Iāve covered above is really just a taste of whatās possible with the Grid Intensity CLI. As more provider integrations are added, and the tool matures, itāll be interesting to see what real world use cases it serves.