Chapters

Observability 1: what each log can tell you

Kadé's network is now built properly. The next problem is seeing it: when a shopper says "checkout failed at 9:14", you need to find that request and know which hop failed. GCP records something at almost every hop, but each log sees only its own part of the path, costs money, and is often off by default. This chapter maps every signal to the hop it sees, explains the ones that matter most, and ends with a query cookbook.

Running it · chapter 22 of 26 · 9 min read

The observability map

Each hop on a shopper's path has its own record. Click a signal to see where it sits, what it can answer, and what it cannot:

Shopperbrowser / appCloudflareedgeLoad balancer+ Cloud ArmorCloud Runkade-apiCloud SQLkade-sqlCloud NAT→ PayGateHA VPN→ officekade-workerVM in sn-appCloud DNSprivate zones

Can answer

  • Every request: URL, status, latency
  • Who answered (statusDetails)
  • Which Cloud Armor rule matched

Cannot answer

  • What happened inside kade-api
  • Requests Cloudflare stopped at the edge

On by default? off: turn on per backend service. Volume: 1 entry per request (sample to reduce).

Load balancer logs: one line per request

Each request through the load balancer can produce one log entry, written by Google's front end. It records what the shopper asked, what came back, how long it took, and who produced the answer (chapter 12 section 10). Logging is set per backend service, with a sample rate between 0 and 1.

Each tab is one real kind of entry, with its important fields picked out:

{ one load balancer log entry, simplified

httpRequest.requestMethod: "POST"
httpRequest.requestUrl: "https://api.kade.lk/v1/orders"
httpRequest.status: 200
← what came back
httpRequest.latency: "0.184s"
← total time at the LB
httpRequest.remoteIp: "104.23.211.40"
← a Cloudflare edge, not the shopper
jsonPayload.statusDetails: "response_sent_by_backend"
← kade-api answered
jsonPayload.enforcedSecurityPolicy.outcome: "ACCEPT"
jsonPayload.enforcedSecurityPolicy.priority: 2147483647
← default rule allowed it
resource.labels.backend_service_name: "kade-api-backend"

A normal request: the backend answered (response_sent_by_backend) in 184 ms, and Cloud Armor's default rule allowed it. Note remoteIp is a Cloudflare address; the shopper's IP is in the app's own log.

#FieldWhat it tells you
1httpRequest.status, requestUrl, requestMethodWhat was asked and what came back
2httpRequest.latencyTotal time at the load balancer, from first byte in to last byte out
3httpRequest.remoteIpThe client Google saw. Behind Cloudflare, that is a Cloudflare address (chapter 13)
4jsonPayload.statusDetailsWho answered: the backend, the cache, Cloud Armor, or nobody (no healthy backend, timeout)
5jsonPayload.enforcedSecurityPolicyWhich Cloud Armor rule matched, its priority and outcome. previewSecurityPolicy shows rules in preview mode
6resource.labels.backend_service_nameWhich backend service the URL map chose

Cloud Run logs: the request log and the app's own lines

Cloud Run writes two kinds of entry for kade-api, both under resource.type="cloud_run_revision":

#LogWritten byContains
1Request log (run.googleapis.com/requests)Cloud Run itself, for every requestStatus, latency, instance, URL. Like the load balancer log, but from Cloud Run's side, so it also catches cold starts and container crashes
2App log (stdout and stderr)kade-api's own codeWhatever the app prints. Printed as JSON, fields like severity and a trace ID become searchable

VPC Flow Logs: connections inside the VPC

Flow logs are switched on per subnet. They record a sample of connections to and from addresses in that subnet: who talked to whom, on which ports, how many bytes and packets, and for TCP, the round-trip time. They never contain the content.

kade-vpc · flow logs on sn-run and sn-appShoppersvia Cloudflare + LBkade-api egressCloud Run, via sn-runkade-workerVM in sn-appCloud SQLPayGate (via NAT)Office (via VPN)✕LB → Cloud Run does notenter the VPC: not inflow logs. Use LB logs.✓ recorded: connections leaving or entering these subnetsOne record: src 10.10.3.17:43122 → dest 10.10.32.3:5432 · TCP · 48 packets · 21 KB · rtt 2 ms · 14:02:05-14:02:10+ metadata: src Cloud Run kade-api (sn-run, asia-southeast1) · dest Cloud SQL range · sampled at 0.5
#SettingWhat it meansKadé's value
1Aggregation intervalRecords are summed over this window before being written5 s (default) for sn-run and sn-app; longer windows mean fewer, coarser records
2Flow samplingThe share of connections recorded (0 to 1)0.5 (default). Enough to see patterns; not every connection
3MetadataExtra details: VM names, regions, countries for internet addressesAll metadata; it is what makes records readable
4FilterA condition that drops records before they are writtenNone yet; useful later to keep only traffic to PayGate or the office
Flow logs answer questions like "is kade-worker really talking to the office?", "which instance sends traffic to PayGate?", or "what is this unknown address our VM keeps contacting?". They are a sample, so never use them to count requests.

Firewall rule logs and Firewall Insights

Logging is a setting on each firewall rule (chapter 5.1 used --enable-logging). When on, every connection the rule allows or denies is recorded with the rule's name, which answers "which rule let this in?" and "which rule blocked this?".

#FactWhat it means
1Only TCP and UDP connections are loggedPing (ICMP) never appears
2The implied rules cannot be loggedTraffic dropped by the implied deny ingress leaves no trace. To see what is being denied, add an explicit "deny all" ingress rule at priority 65534 with logging on
3One record per connection, not per packetVolume follows connection count, which is far lower than packet count
4Firewall InsightsUses the hit counts to point out rules that are shadowed, never used, or allow far more than the traffic needs (chapter 5.2)

NAT, DNS, Cloud SQL, and Cloudflare

#SignalTurn onAnswersChapter
1Cloud NAT logsErrors only"Are PayGate calls being dropped?" allocation_status: DROPPED means ports ran out7
2Cloud DNS query logsA server policy on kade-vpc with logging"What names does this VM look up?" Useful to spot a compromised VM, or a typo in a hostname8
3Cloud SQL logsOn by default (PostgreSQL log), plus flags such as log_connections"Are connections being refused? Is a query slow?"9
4Cloudflare analytics and logsDashboard analytics on every plan; Logpush on higher plansRequests stopped at the edge never reach GCP, so only Cloudflare knows about them. Join with GCP by CF-Ray13
5Ops Agent (VMs only)Installed on the VM, ideally baked into the imageMemory and disk usage, and the VM's system and app log files. Without it, Google only sees CPU, network and disk throughput from outside the VM12.2
6Audit logsAdmin Activity always on; Data Access off by default"Who deleted that firewall rule?" Admin Activity records every configuration change, free17

Cost, retention and where logs go

Logs are billed by volume, and volume grows with users. A setting that is harmless at 1,000 requests a day can be a real bill at 1,000,000. Estimate before you turn things on:

  • Load balancer, API (100%)17.2 GiB
  • Load balancer, images (10%, 200s excluded)0.2 GiB
  • Cloud Run request logs (API)11.4 GiB
  • App logs (≈2 lines per API request)13.7 GiB
  • VPC Flow Logs (sampling 0.5)5.1 GiB
  • Firewall rule logs1.4 GiB

≈ 49.1 GiB per month with Kadé's settings. Everything at 100% with no exclusions: ≈ 139.9 GiB. Compare with the free monthly allotment and price per GiB on the Cloud Logging pricing page.

Assumed entry sizes: load balancer 1.5 KB, Cloud Run request 1 KB, app line 0.6 KB, flow record 1.2 KB, firewall 1 KB. Measure yours after a day: Logs Storage in the console shows volume per log.

#ToolWhat it doesUse for
1Sample rateRecords only a share of requests or connectionsLoad balancer logs on very busy paths, flow logs
2Exclusion filterDrops matching entries before they are stored (and billed)Health check noise, successful requests for static images
3Log bucket retentionHow long entries are kept. The default bucket keeps 30 daysLonger only where needed (security, payments)
4Sink to Cloud Storage or BigQueryCopies entries out for cheap long-term storage or SQL analysisKeeping a year of load balancer logs for audits
5Log AnalyticsRun SQL directly on a log bucket"Top 20 IPs hitting /api/login this week" in one query

Query cookbook

Ready-to-paste Logs Explorer queries, organized by the question you are asking. Each shows what it finds and what to look for:

resource.type="http_load_balancer"
httpRequest.status>=500
  • What it shows: Every 5xx at the front door.
  • What to look for: Group mentally by jsonPayload.statusDetails: backend errors, timeouts, or no backend.

Back to Kadé

Commands in this chapter

Commands 1 and 2 are read-only. Commands 3 to 7 change the project.

Is logging on for each backend service?

  • What it shows: Logging on or off, and the sample rate, for every backend service.
  • What to look for: False or an empty value on anything public-facing.
gcloud compute backend-services list \  --format="table(name,logConfig.enable,logConfig.sampleRate)"

Which subnets have flow logs, and which firewall rules log?

  • What it shows: Flow log settings per subnet, then logging per firewall rule.
  • What to look for: Gaps against section 09's table.
gcloud compute networks subnets list --network=kade-vpc \  --format="table(name,logConfig.enable,logConfig.flowSampling,logConfig.aggregationInterval)"gcloud compute firewall-rules list --filter="network~kade-vpc" \  --format="table(name,direction,priority,logConfig.enable)"

Turn on load balancer logging (changes the project)

  • What it shows: Logs every request through kade-api-backend.
  • What to look for: Entries under resource.type="http_load_balancer" within a minute.
gcloud compute backend-services update kade-api-backend --global \  --enable-logging --logging-sample-rate=1.0

Turn on flow logs for a subnet (changes the project)

  • What it shows: Sampled connection records for sn-run, with metadata.
  • What to look for: Entries under logName:"vpc_flows" after a few minutes of traffic.
gcloud compute networks subnets update sn-run --region=asia-southeast1 \  --enable-flow-logs --logging-flow-sampling=0.5 \  --logging-aggregation-interval=interval-5-sec --logging-metadata=include-all

Log what the implied deny would drop (changes the project)

  • What it shows: An explicit, logged "deny everything" ingress rule just above the implied one. It changes no behaviour, only adds the record.
  • What to look for: Denied connections with rule_details.reference ending in kade-deny-all-log.
gcloud compute firewall-rules create kade-deny-all-log --network=kade-vpc \  --direction=INGRESS --action=DENY --rules=all --source-ranges=0.0.0.0/0 \  --priority=65534 --enable-logging

Exclude successful image requests from load balancer logs (changes the project)

  • What it shows: An exclusion on the default sink: these entries are dropped before storage.
  • What to look for: Log volume for http_load_balancer falls the next day. Errors on images are still kept.
gcloud logging sinks update _Default --add-exclusion=name=lb-image-200s,\filter='resource.type="http_load_balancer" AND httpRequest.requestUrl:"/images/" AND httpRequest.status=200'

Send load balancer logs to BigQuery (changes the project)

  • What it shows: A sink copying load balancer entries to a dataset for long-term SQL analysis.
  • What to look for: The sink's writer identity needs BigQuery Data Editor on the dataset; the command prints it.
gcloud logging sinks create kade-lb-to-bq \  bigquery.googleapis.com/projects/kade-prod/datasets/kade_lb_logs \  --log-filter='resource.type="http_load_balancer"' --use-partitioned-tables

Spotted a mistake, or something unclear? Suggest an edit on GitHub