Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1gcloud run services update csvconf --region=us-central1 --platform=managed --update-labels service=csvconfI 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:
1#!/bin/bash2for line in $(3 gcloud run services list --platform=managed \4 --format="csv(SERVICE,REGION)" \5 --filter "NOT metadata.labels.service:*" \6 | tail -n +2)7do8 IFS=$','; service_and_region=($line); unset IFS;9 service=${service_and_region[0]}10 region=${service_and_region[1]}11 echo "service: $service region: $region"12 gcloud run services update $service \13 --region=$region --platform=managed \14 --update-labels service=$service15 echo16doneIt runs the equivalent of this for each service:
1gcloud run services update asgi-log-demo --region=us-central1 --platform=managed --update-labels service=asgi-log-demoI saved that as a runme.sh script, run chmod 755 runme.sh and then ./runme.sh to run it.
The output of the script looked like this (one entry for each service) - each one took ~30s to run.
1Service [covid-19] revision [covid-19-00122-zod] has been deployed and is serving 100 percent of traffic at https://covid-19-j7hipcg4aq-uc.a.run.app2✓ Deploying... Done.3 ✓ Creating Revision...4 ✓ Routing traffic...5Done.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:

Bookmarked query (only works for me)