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

EmissionTracker failed on GCP Colab Enteprise #572

Closed
cl3m3nt opened this issue Jun 12, 2024 · 9 comments · Fixed by #580
Closed

EmissionTracker failed on GCP Colab Enteprise #572

cl3m3nt opened this issue Jun 12, 2024 · 9 comments · Fixed by #580
Assignees
Labels
bug Something isn't working good first issue Good for newcomers P1 Priority 1

Comments

@cl3m3nt
Copy link

cl3m3nt commented Jun 12, 2024

  • CodeCarbon version: 2.4.2
  • Python version: 3.10.12
  • Operating System: Ubuntu 22.04.3 LTS

Description

Tried to launch codecarbon EmissionTracker on GCP Colab Enterprise.
It works correctly on Colab Web with same codecarbon version 2.4.2.
It failed on different GCP region trials always with same error message below.
As you recently updated GCP location metatadata this might be related.
I appreciate your help.

What I Did

from codecarbon import EmissionsTracker
tracker = EmissionsTracker()

[/usr/local/lib/python3.10/dist-packages/codecarbon/emissions_tracker.py](https://localhost:8080/#) in __init__(self, project_name, measure_power_secs, api_call_interval, api_endpoint, api_key, output_dir, output_file, save_to_file, save_to_api, save_to_logger, logging_logger, save_to_prometheus, prometheus_url, gpu_ids, emissions_endpoint, experiment_id, experiment_name, co2_signal_api_token, tracking_mode, log_level, on_csv_write, logger_preamble, default_cpu_power, pue)
    379         self._data_source = DataSource()
    380 
--> 381         cloud: CloudMetadata = self._get_cloud_metadata()
    382 
    383         if cloud.is_on_private_infra:

[/usr/local/lib/python3.10/dist-packages/codecarbon/emissions_tracker.py](https://localhost:8080/#) in _get_cloud_metadata(self)
    881     def _get_cloud_metadata(self) -> CloudMetadata:
    882         if self._cloud is None:
--> 883             self._cloud = CloudMetadata.from_utils()
    884         return self._cloud
    885 

[/usr/local/lib/python3.10/dist-packages/codecarbon/external/geography.py](https://localhost:8080/#) in from_utils(cls)
     44 
     45         provider: str = cloud_metadata["provider"].lower()
---> 46         region: str = extract_region_for_provider.get(provider)(cloud_metadata)
     47         if provider in ["aws", "azure"]:
     48             logger.warning(

[/usr/local/lib/python3.10/dist-packages/codecarbon/external/geography.py](https://localhost:8080/#) in <lambda>(x)
     35             "aws": lambda x: x["metadata"]["region"],
     36             "azure": lambda x: x["metadata"]["compute"]["location"],
---> 37             "gcp": lambda x: extract_gcp_region(x["metadata"]["zone"]),
     38         }
     39 

KeyError: 'zone'

@eherra

@benoit-cty
Copy link
Contributor

Thanks for reporting this. Can you run the command curl http://169.254.169.254/computeMetadata/v1/instance/?recursive=true&alt=json because I expect that there is a missing field "zone" in "metadata". It is not related to the update of GCP location data in CodeCarbon. GCP may have modify the output of their API we use to know if we are on GCP and where.

@cl3m3nt
Copy link
Author

cl3m3nt commented Jun 14, 2024

Hi Benoit,

thanks for your reply.
After I ran the command from GCP colab entreprise notebook I got the following:

Missing required header: Metadata-Flavor

I appreciate your feedback.

@benoit-cty
Copy link
Contributor

benoit-cty commented Jun 14, 2024

Sorry, from https://stackoverflow.com/questions/62026424/is-it-possible-to-obtain-instance-metadata-for-gcp-on-google-collaboratory it seems to be

curl -v -w "\n" -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/?recursive=true&alt=json

@cl3m3nt
Copy link
Author

cl3m3nt commented Jun 14, 2024

Hi,

here is the output:

  • Trying 169.254.169.254:80...
  • Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)

GET /computeMetadata/v1/instance/machine-type HTTP/1.1
Host: 169.254.169.254
User-Agent: curl/7.81.0
Accept: /
Metadata-Flavor: Google

  • Mark bundle as not supporting multiuse
    < HTTP/1.1 404 Not Found
    < Content-Type: text/plain; charset=utf-8
    < Metadata-Flavor: Google
    < X-Content-Type-Options: nosniff
    < Date: Fri, 14 Jun 2024 11:31:14 GMT
    < Content-Length: 10
    <
    Not Found
  • Connection #0 to host 169.254.169.254 left intact

@benoit-cty
Copy link
Contributor

Sorry, it's curl -v -w "\n" -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/?recursive=true&alt=json

I tested it and I get this from Cloud Shell (I delete un-relevant lines)

{
  "cpuPlatform": "Intel Broadwell",
  "machineType": "projects/8xxxxx2/machineTypes/e2-standard-4",
  "zone": "projects/8xxxx2/zones/europe-west1-b"
}

@cl3m3nt
Copy link
Author

cl3m3nt commented Jun 14, 2024

Hi again,

tx for your reply.
Not sure your test makes sense as it's comparing output from one environment being colab enteprise, the other being cloud shell. I appreciate your feedback.

Testing it from gcp colab enteprise notebook:

  • Trying 169.254.169.254:80...
  • Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)

GET /computeMetadata/v1/instance/?recursive=true HTTP/1.1
Host: 169.254.169.254
User-Agent: curl/7.81.0
Accept: /
Metadata-Flavor: Google

  • Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < Content-Type: application/json
    < Metadata-Flavor: Google
    < Date: Fri, 14 Jun 2024 13:08:01 GMT
    < Content-Length: 2
    <
  • Connection #0 to host 169.254.169.254 left intact
    {}

Testing from gcp cloud shell, got info like (deleted others lines):

"cpuPlatform": "Intel Broadwell",
"description": "",
"disks": [
{
"deviceName": "boot",
"index": 0,
"interface": "SCSI",
"mode": "READ_WRITE",
"type": "PERSISTENT-BALANCED"
},
{
"deviceName": "home",
"index": 1,
"interface": "SCSI",
"mode": "READ_WRITE",
"type": "PERSISTENT"
}

@benoit-cty
Copy link
Contributor

So it seems that on Colab Entreprise you get an answer from the GCP API but it's an empty JSON {}

We have to update our GCP detection to handle this case. Thanks for reporting the details.

@benoit-cty benoit-cty added bug Something isn't working good first issue Good for newcomers P1 Priority 1 labels Jun 14, 2024
@cl3m3nt
Copy link
Author

cl3m3nt commented Jun 17, 2024

Hi,

tx for your feedback. Our team is looking forward to getting the fix.
Kind regards

@benoit-cty
Copy link
Contributor

Hello, I've opened a PR for that : #580

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers P1 Priority 1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants