Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get instanceid from cloud_run?

The logs from cloud run spit out some good json with resource.labels.revision_name = my_name-00046-kip.

The json path labels.instanceId is more like this though

00bf4bf02d71261c0c1f55a601331b336a5d90d365cca1b28330dcf3e456fb7c07d5b72f1d3c9a971e391b5edc3512aea8559d172b24e639

per this document I was able to get revision_name

https://cloud.google.com/run/docs/reference/container-contract#env-vars

but I can't get the instance id and metrics must be reported per instance or two instances reporting in the same minute will be rejected. how do I get instance id (preferably through DockerFile and if not through api call). If cloud run boots up 10 instances under one revision name, I have to make sure to uniquely report metrics to Generic Task resource where I plan on filling in job_id with the instance id.

thanks, Dean

like image 771
Dean Hiller Avatar asked Sep 07 '26 22:09

Dean Hiller


2 Answers

Please try using the metadata server to get the instance ID using the url:

http://metadata.google.internal/computeMetadata/v1/instance/id

Note that "Metadata-Flavor: Google" header is also required.

like image 186
wlhee Avatar answered Sep 10 '26 11:09

wlhee


If you're using Java (as indicated by the tags), the easiest way to get the instance ID from the "internal metadata server" programmatically is probably to include the dependency com.google.cloud:google-cloud-core:1.93.5 (or newer) through Gradle/Maven and then call the following method:

import com.google.cloud.MetadataConfig;

String instanceId = MetadataConfig.getInstanceId();
like image 44
David Avatar answered Sep 10 '26 11:09

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!