Use labels on Cloud Run services for a billing breakdown

Thanks to @glasnt for the tip on this one. If you want a per-service breakdown of pricing on your Google Cloud Run services within a project (each service is a different deployed application) the easiest way to do it is to apply labels to those services, then request a by-label pricing breakdown.

This command will update a service (restarting it) with a new label:

gcloud run services update csvconf --region=us-central1 --platform=managed --update-labels service=csvconf

I found it needed the --platform=managed and --region=X options to avoid it asking interactive questions.

Here's a bash script which loops through all of the services that do NOT have a service label and applies one:

for line in $(
  gcloud run services list --platform=managed \
    --format="csv(SERVICE,REGION)" \
    --filter "NOT metadata.labels.service:*" \
  | tail -n +2)
  IFS=$','; service_and_region=($line); unset IFS;
  echo "service: $service    region: $region"
  gcloud run services update $service \
    --region=$region --platform=managed \
    --update-labels service=$service

It runs the equivalent of this for each service:

gcloud run services update asgi-log-demo --region=us-central1 --platform=managed --update-labels service=asgi-log-demo

I saved that as a script, run chmod 755 and then ./ to run it.

The output of the script looked like this (one entry for each service) - each one took ~30s to run.

Service [covid-19] revision [covid-19-00122-zod] has been deployed and is serving 100 percent of traffic at
✓ Deploying... Done.                                                                                                                                                                                                 
  ✓ Creating Revision...                                                                                                                                                                                             
  ✓ Routing traffic...                                                                                                                                                                                               

I had to wait a couple of days for this to take effect, but once it did I could get results by visiting Billing -> Reports, then selecting service from the group by menu here:

The graph (I picked bar chart over line chart) looked like this:


Using the metrics explorer

Even without setting up these extra service labels the Metrics explorer can show you breakdowns of billing time against different services.

Here's how to configure that:

Created 2020-04-21T17:52:57-07:00, updated 2020-09-02T12:21:31-07:00 · History · Edit