Log-based metrics in GCP - Time & Space Complexity
When creating log-based metrics in GCP, it is important to understand how the time to process logs grows as more logs are generated.
We want to know how the number of logs affects the work done to update these metrics.
Analyze the time complexity of creating a log-based metric that counts occurrences of a specific log entry.
// Define a log-based metric filter
resource.type = "gce_instance"
logName = "projects/my-project/logs/syslog"
severity = "ERROR"
// Create metric to count matching logs
metric.type = "logging.googleapis.com/user/error_count"
metric.filter = "severity=\"ERROR\""
This sequence sets up a metric that counts how many error logs appear in the system.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Processing each log entry to check if it matches the metric filter.
- How many times: Once for every log entry generated in the system.
As the number of logs increases, the system must check each log to see if it fits the metric's filter.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 log checks |
| 100 | 100 log checks |
| 1000 | 1000 log checks |
Pattern observation: The number of operations grows directly with the number of logs.
Time Complexity: O(n)
This means the work to update the metric grows linearly with the number of logs processed.
[X] Wrong: "The metric updates instantly regardless of log volume."
[OK] Correct: Each log must be checked against the filter, so more logs mean more work and time.
Understanding how log-based metrics scale helps you design monitoring that stays efficient as systems grow.
"What if the metric filter became more complex with multiple conditions? How would the time complexity change?"
Practice
Solution
Step 1: Understand what log-based metrics do
Log-based metrics count log entries that match a filter and convert them into numerical data.Step 2: Compare options to this definition
Only To count specific log entries and turn them into numbers you can monitor describes counting logs and turning them into numbers for monitoring.Final Answer:
To count specific log entries and turn them into numbers you can monitor -> Option BQuick Check:
Log-based metrics = counting logs for monitoring [OK]
- Confusing log storage with metrics
- Thinking metrics delete or encrypt logs
- Assuming metrics store logs permanently
Solution
Step 1: Recall the correct filter syntax for log-based metrics
Google Cloud uses the syntax severity == "ERROR" to filter logs with severity ERROR.Step 2: Check each option
Double equals (==) is the correct equality operator; single equals (=) is invalid. Colon (:) is for substring matching, not exact equality. != excludes the value.Final Answer:
severity == "ERROR" -> Option DQuick Check:
Filter syntax uses == for equality [OK]
- Using single equals instead of double
- Using colon instead of equals
- Using not equals when filtering for ERROR
resource.type = "gce_instance" AND severity >= "ERROR", what logs will this metric count?Solution
Step 1: Analyze the filter conditions
The filter requires resource.type to be "gce_instance" and severity to be ERROR or higher (like CRITICAL).Step 2: Match options to filter meaning
All logs from GCE instances with severity ERROR or higher correctly states logs from GCE instances with severity ERROR or higher. Others either ignore resource type or severity condition.Final Answer:
All logs from GCE instances with severity ERROR or higher -> Option AQuick Check:
Filter = GCE instance + severity >= ERROR [OK]
- Ignoring resource.type condition
- Confusing severity >= ERROR with exact match
- Assuming severity lower than ERROR is included
severity = "ERROR" OR severity = "CRITICAL", but the metric shows zero counts. What is the likely problem?Solution
Step 1: Check if the filter syntax is valid
The syntax severity = "ERROR" OR severity = "CRITICAL" is valid; OR is supported in log-based metric filters.Step 2: Understand impact of zero counts
A valid filter with zero counts indicates no matching logs, likely because the logs do not have severity fields or lack ERROR/CRITICAL values.Final Answer:
The logs do not have severity fields -> Option CQuick Check:
Valid filter + zero = no matching logs [OK]
- Assuming filter syntax invalid
- Thinking OR unsupported
- Waiting 24 hours unnecessarily
jsonPayload.message. Which filter correctly counts logs where the message contains the word "timeout"?Solution
Step 1: Understand how to filter logs containing a word
The colon operator (:) is used to check if a field contains a substring in log filters.Step 2: Check resource type and filter syntax
resource.type = "cloud_function" AND jsonPayload.message : "timeout" correctly filters logs from Cloud Functions where jsonPayload.message contains "timeout".Final Answer:
resource.type = "cloud_function" AND jsonPayload.message : "timeout" -> Option AQuick Check:
Use : to check substring in log filters [OK]
- Using equals instead of colon for substring
- Using comparison operators like >= incorrectly
- Filtering for not equals when looking for matches
Ready to prove it?
Pass the test and this pattern moves unknown
