javagoogle-cloud-platformgoogle-cloud-rungoogle-cloud-metrics

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


Solution

  • 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.