Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #81: For the Prometheus exporter, the values based on ElecticityMaps provider are estimations and not the real values #83

Merged
merged 8 commits into from
Jun 11, 2024

Conversation

locomundo
Copy link
Contributor

This is the fix discussed in issue #81.

We noticed that when using the grid-intensity exporter with ElectricityMaps as provider we always obtained an estimation, never the real value.

By using the history endpoint as opposed to the latest one, we can get both the latest estimated value, as well as the latest real measured value.

This change includes:

  • For ElectricityMaps, we now use the history endpoint instead of the latest one, to return both the latest estimated and latest real values.
  • The exporter now sends the timestamp with the Prometheus metrics, as the latest real value is usually in the past in the case of ElectricityMaps.
  • Tested that it works with ElectricityMaps, Ember and CarbonIntensityOrgUK, and Watttime providers (so all of them), but didn't run the tests.
  • The label is_estimated has been added in the Prometheus metrics. For ElectricityMaps it can provide values for estimated or real, or for one of the two, depending on the location. For example, for NL we get:
grid_intensity_carbon_average{is_estimated="false",location="NL",node="",provider="ElectricityMaps",region="",units="gCO2e per kWh"} 92 1717758000000
grid_intensity_carbon_average{is_estimated="true",location="NL",node="",provider="ElectricityMaps",region="",units="gCO2e per kWh"} 94 1717765200000

  • For the other providers it always returns the value as is_estimated=false (the default). Is this correct or are you aware of some of the other providers returning estimated data? @rossf7 @mrchrisadams

Flavia Paganelli and others added 4 commits June 3, 2024 17:24
thegreenwebfoundation#81

- Now sending always the timestamp in the Prometheus metrics.
- For ElectricityMaps, we now use the historic endpoint instead of the carbon-intensity one, so we can get both the latest estimated and real values.
- Tested that it works with Ember and CarbonIntensityOrgUK, Watttime not yet (I need to register)
- The label is_estimated has been added in the Prometheus metrics. For ElectricityMaps it can provide values for both, or for one of the two, depending on the location.
- For the other providers it always returns the value as is_estimated=false (the default) because I didn't make any changes. I'm. not 100% sure this is correct for all of them, or if some of the providers also return estimations (to investigate).
- The error handling when parsing generates a lot of clutter, not sure if there's a better way to do it in go...
for relative carbon intensity

Otherwise we see this error, for example:

panic: inconsistent label cardinality: expected 5 label values but got 6 in []string{"CAISO_NORTH", "", "WattTime", "", "percent", "false"}
@mrchrisadams
Copy link
Member

Thanks for this @locomundo, this looks great!

I'm currently travelling but if @rossf7 doesn't get to it before Wednesday I'll aim to review it by the end of the week.

@rossf7
Copy link
Contributor

rossf7 commented Jun 10, 2024

@locomundo Yes this looks good. I think / hope we can get it merged this week and then cut a release with it and #82

For the other providers it always returns the value as is_estimated=false (the default). Is this correct or are you aware of some of the other providers returning estimated data?

For the other providers I think we'll need to check their methodologies. Unless you know @mrchrisadams ?

but if @rossf7 doesn't get to it before Wednesday I'll aim to review it by the end of the week.

I should be able to review first half of this week. Chris, I'll let you know if I get delayed

@mrchrisadams
Copy link
Member

mrchrisadams commented Jun 11, 2024

Hey @rossf7 - I think pretty much all of these are forecasts / estimates rather than historical, recorded figures.

Ember - the free annual figures we use are an estimate, based on either the latest data for the last full year, or data from the last full month. In each case, if we used the Ember data to get an idea of the carbon intensity for "now", I think we'd be looking at historical data as the basis to project forward to now. More in the methodology documentation here

CarbonIntensityOrgUK - the figures here are forecasts, based on an ML model. I don't think it's historical data per se. They briefly describe the M methodology on their front page

Watttime - I'm less clear on Watttime. Because marginal emissions rely on a comparison to a counterfactual that doesn't exist (we don't have two versions of the same grid to compare side by side), I think you'd refer to all of these as estimates, until there was some explicit validation for each reading. I think this is somewhat comparable to Electricity Maps saying a reading is estimated until it is replaced with 'real' data. You can read some more about the Watttime methodologies here

Until we hear otherwise, I think it's safer to assume all the other readings are estimates.

@locomundo
Copy link
Contributor Author

In that case we can invert it and make the default as isEstimated=true. If you all agree on this I can make that change in the PR @rossf7 @mrchrisadams

@rossf7
Copy link
Contributor

rossf7 commented Jun 11, 2024

@locomundo Yes, good idea lets invert it. @mrchrisadams thanks for clarifying and I agree for WattTime since they return marginal emissions we should treat them as estimated.

@locomundo
Copy link
Contributor Author

Ok, so now all the other providers are returning is_estimated=true. No default, though, I thought I could change the default value, but that was not possible. However, this might be better so it's explicit and clear.

Copy link
Contributor

@rossf7 rossf7 left a comment

Choose a reason for hiding this comment

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

@locomundo This is really cool to be able to show both values!

Just some minor nits from me and then I think this is good to go.

@@ -64,34 +64,139 @@ func (e *ElectricityMapsClient) GetCarbonIntensity(ctx context.Context, location
return nil, errBadStatus(resp)
}

data := &electricityMapsData{}
err = json.NewDecoder(resp.Body).Decode(data)
type EMHistoryResponse struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type EMHistoryResponse struct {
type electricityMapsHistoryResponse struct {

Rather than have this struct inline could you rename it and add it to the end of the file?

For now we have all the providers in a single package but we use the provider name as a prefix for the types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!


// Helper struct to remove clutter in the calling function
// while finding the latest (and greatest) data points
type mostRecentDatapoints struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type mostRecentDatapoints struct {
type electricityMapsDatapoints struct {

Same here to add the prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

pkg/provider/electricity_maps.go Outdated Show resolved Hide resolved
Flavia Paganelli and others added 2 commits June 11, 2024 13:40
Copy link
Contributor

@rossf7 rossf7 left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@rossf7 rossf7 merged commit cc6e43d into thegreenwebfoundation:main Jun 11, 2024
locomundo pushed a commit to aknostic/grid-intensity-go that referenced this pull request Jun 13, 2024
in Prometheus.

Related to PR thegreenwebfoundation#83 "For the Prometheus exporter, the values based on ElecticityMaps provider are estimations and not the real values"
rossf7 added a commit that referenced this pull request Aug 29, 2024
* Fix/improvement for issue #81

#81

- Now sending always the timestamp in the Prometheus metrics.
- For ElectricityMaps, we now use the historic endpoint instead of the carbon-intensity one, so we can get both the latest estimated and real values.
- Tested that it works with Ember and CarbonIntensityOrgUK, Watttime not yet (I need to register)
- The label is_estimated has been added in the Prometheus metrics. For ElectricityMaps it can provide values for both, or for one of the two, depending on the location.
- For the other providers it always returns the value as is_estimated=false (the default) because I didn't make any changes. I'm. not 100% sure this is correct for all of them, or if some of the providers also return estimations (to investigate).
- The error handling when parsing generates a lot of clutter, not sure if there's a better way to do it in go...

* Fix for Watttime - was missing the is_estimated label...

for relative carbon intensity

Otherwise we see this error, for example:

panic: inconsistent label cardinality: expected 5 label values but got 6 in []string{"CAISO_NORTH", "", "WattTime", "", "percent", "false"}

* Cleaned up some commented code

* It turns out that all the other providers return estimated values

Thus, we are setting IsEstimated to true.
See for more info #83 (comment)

* Some renaming as suggested by Chris

* Update pkg/provider/electricity_maps.go

Co-authored-by: Ross Fairbanks <[email protected]>

* Document how to fix "old" samples not being ingested

in Prometheus.

Related to PR #83 "For the Prometheus exporter, the values based on ElecticityMaps provider are estimations and not the real values"

* Corrections suggested by Ross

Co-authored-by: Ross Fairbanks <[email protected]>

---------

Co-authored-by: Flavia Paganelli <[email protected]>
Co-authored-by: Ross Fairbanks <[email protected]>
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