Cloud Logging, Monitoring and SLOs on Google Cloud
Query logs, chart metrics, follow a trace and define a latency and availability SLO with an error-budget alert.
By Invicly Tech, , in Google Cloud Engineering and Services. About 10 minutes to read.
In short
Observability means you can ask your running service questions and get answers from its logs, metrics and traces. On Google Cloud these live in Cloud Logging, Cloud Monitoring and Cloud Trace. A service level objective (SLO) turns that data into a promise, such as 99.9 percent of requests succeed, and an error budget tells you how much failure you can afford.
Think of it like this: Logs are the diary, metrics are the dashboard gauges, and a trace is the receipt that follows one order through every kitchen station.
Explained simply
When something breaks at night, you need answers fast. Observability is the habit of recording enough about your running service that you can find the cause without guessing. Google Cloud gives you three tools for this: Cloud Logging for events, Cloud Monitoring for numbers over time, and Cloud Trace for the path of a single request.
A log is one line that says what happened, like a payment failed. A metric is a number sampled over time, such as requests per second. A trace follows one request through several services and shows how long each step took. You use metrics to notice a problem, traces to find the slow step, and logs to read the details.
An SLO, or service level objective, is a promise you make to yourself about quality. For example: 99.9 percent of requests must succeed over 30 days. The measurement behind it is called an SLI, a service level indicator. It is simply good requests divided by all requests.
The leftover part is your error budget. With a 99.9 percent goal, 0.1 percent of requests may fail. If you spend the budget slowly, you can keep shipping changes. If you spend it fast, you slow down and fix reliability first.
Good alerts watch how fast the budget is being used. One failed request is noise. Ten percent of the month's budget gone in an hour is a real signal, and that is when someone should be paged.
Press Play on a diagram to walk through it one step at a time.
- What happened in this one event?
- Text or JSON lines
- Search in Logs Explorer
- Costs grow with volume
- How much, how often, how slow?
- Numbers over time
- Charts and alerts in Monitoring
- Cheap to keep and graph
- Where did this request spend time?
- One request across services
- Spans shown in Cloud Trace
- Best for slow requests
- 1Request
Users call the Cloud Run service.
- 2SLI
Good requests divided by all requests.
- 3SLO
A target, such as 99.9 percent over 30 days.
- 4Error budget
The 0.1 percent you may spend on failures.
- 5Burn-rate alert
Pages someone when the budget burns too fast.
- Cloud Logging
- Logs Explorer
- Log buckets
- Log-based metrics
- Cloud Monitoring
- Metrics Explorer
- Dashboards
- Alert policies
- Service monitoring and SLOs
- Cloud Trace
- Latency waterfall
- OpenTelemetry spans
- 1Measure SLI
From real traffic.
- 2Compare with SLO
How much budget is left?
- 3Decide
Ship faster or fix reliability.
- 4Act
Release, or pause and repair.
- Back to step 1
In depth
Logs Explorer and log buckets
Logs Explorer is the console page for searching logs. You write a filter using fields such as resource.type, severity, and textPayload or jsonPayload. Structured JSON logs are easier to search than plain text, because each field becomes searchable. Cloud Run turns anything your app prints to stdout as JSON into a structured entry, and the fields severity and message are recognised.
Logs are stored in log buckets. The _Required bucket holds audit logs and cannot be changed. The _Default bucket holds the rest, and its default retention is 30 days. Ingesting logs beyond the free monthly allotment costs money, so check the current pricing page and use exclusion filters for noisy debug lines.
- Filter by resource.type and service name first
- Add severity>=ERROR to cut noise
- Use JSON logs so fields are searchable
- Default retention is 30 days; change it per bucket
- Exclude debug logs you never read
Metrics, dashboards and alerts
Cloud Run, load balancers and databases publish metrics automatically. For Cloud Run, request count and request latency are the two to start with. You can chart them in Metrics Explorer, then save the chart on a dashboard. Cloud Monitoring queries can be built in the chart builder or written in PromQL.
You can also create a log-based metric, which counts log lines that match a filter, for example every entry with event name order_failed. That is how a business event becomes a chart and an alert. Alert policies watch a metric against a condition and notify a channel such as email or a chat tool.
- Start with request count, latency and error ratio
- Log-based metrics turn log lines into numbers
- Alert policies send to notification channels
- Label dashboards by environment
Traces: finding the slow step
A trace is made of spans. Each span is one timed step, such as the call to the database. Cloud Trace shows the spans as a waterfall, so the slow one is easy to see. Many Google Cloud services create traces for you, and your code can add more with OpenTelemetry.
To link logs and traces, include the trace ID in your log entries. Then Logs Explorer can jump from an error line to its trace. Without this link you end up matching by timestamp, which is slow and error-prone.
- Client to orders-api: POST /orders1POST /orders
- orders-api to Database: Insert order (20 ms)2Insert order (20 ms)
- orders-api to payments-api: Charge card3Charge card
- payments-api to Database: Slow query (900 ms)4Slow query (900 ms)
- payments-api to orders-api: Charge result5Charge result
- orders-api to Client: 201 Created (total 980 ms)6201 Created (total 980 ms)
SLIs, SLOs and burn-rate alerts
Choose indicators that match what users feel. For availability, count good responses (not 5xx) over all responses. For latency, count requests faster than a threshold, such as 500 ms, over all requests. Set a goal and a window, for example 99.9 percent over a rolling 30 days.
The budget in minutes is easy to compute. 30 days is 43,200 minutes. At 99.9 percent, 0.1 percent of that, about 43 minutes, may be bad. Cloud Monitoring's service monitoring lets you define an SLO and then create an alert on burn rate. A burn rate of 1 means you will use exactly the whole budget by the end of the window. A burn rate of 10 means you will run out in a tenth of the time.
- Availability SLI: good responses over all responses
- Latency SLI: fast responses over all responses
- 99.9 percent over 30 days is about 43 minutes
- Use a fast and a slow burn-rate alert together
Learn it in 5 short steps
About 40m each. Track them with a timer in your plan.
- 1Query logs in Logs Explorer~40m
- 2Chart metrics and build a dashboard~40m
- 3Follow a request in Cloud Trace~40m
- 4Define SLIs and an SLO~40m
- 5Alert on error-budget burn~40m
Key points
- Cloud Run, Cloud Functions and GKE send logs and basic metrics automatically; you add structured fields and custom metrics for your own business events.
- Logs Explorer uses a query language: filter by resource.type, severity and text, then narrow the time range.
- An SLI is a measurement, an SLO is the target for it, and the error budget is 100 percent minus the target.
- Alert on how fast the budget burns, not on every single error, so people are paged only when users are really affected.
- A 99.9 percent target over 30 days allows about 43 minutes of full failure.
Key terms
Tap a card to flip it. 0/6 known
Examples
# Last 10 server errors from one Cloud Run service (placeholder names)
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="orders-api" AND severity>=ERROR' \
--project=<project-id> --limit=10 --freshness=1hThe same filter works when you paste it into Logs Explorer.
Find recent errors from one service
You have a Cloud Run service called orders-api. Customers report failed checkouts. Read the newest ERROR entries from the last hour. Expect to see the message, a timestamp and the revision name. If nothing comes back, widen the time window or check you used the right project.
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="orders-api" AND severity>=ERROR' \
--project=<project-id> --limit=5 --freshness=1h \
--format='value(timestamp,textPayload)'Compute an error budget
Your goal is 99.95 percent availability over 30 days. 30 days has 43,200 minutes. The allowed bad time is 0.05 percent of that, which is 21.6 minutes. A 15-minute outage on day 3 has used about 69 percent of the budget. So you pause risky releases for the rest of the month.
window = 30 * 24 * 60 = 43200 minutes
budget = 43200 * (1 - 0.9995) = 21.6 minutes
used = 15 / 21.6 = 0.69 (69 percent)
left = 6.6 minutesQuick reference
- Logs for details, metrics for trends, traces for slow steps
- gcloud logging read with a filter, --limit and --freshness
- Logs _Default bucket keeps 30 days; _Required is for audit logs
- SLI = good events / total events; SLO = target for the SLI
- Error budget = 1 - SLO target; 99.9 percent over 30 days is about 43 min
- Alert on burn rate, not on single errors
- Add the trace ID to log entries to link logs and traces
Common mistakes
- Setting an SLO of 100 percent. It leaves no error budget, so every release looks dangerous.
- Alerting on raw error counts. A small service with three errors at night wakes people for nothing.
- Measuring from the server only. If the load balancer rejects requests, your server logs look healthy while users see errors.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write a Logs Explorer filter that shows only errors from a Cloud Run service named orders-api.
- Exercise 2medium
A service must be 99.9 percent available over 30 days. Calculate the error budget in minutes and say how much a 10-minute outage uses.
- Exercise 3hard
Design two alerts for a latency SLO (95 percent of requests under 500 ms over 30 days): one fast and one slow. Say what each catches.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between monitoring and logging?
- Monitoring keeps numbers over time and alerts on them. Logging keeps individual events with detail. You normally use both.
- How long are logs kept?
- In the default log bucket the default retention is 30 days. You can change retention per bucket, and longer retention costs more.
- Do I need an SLO for a small project?
- Not formally, but writing one target such as 99.5 percent teaches you what matters to users and when to stop releasing.