Audit logging in GCP - Time & Space Complexity
Audit logging records actions in a system to keep track of what happened and when.
We want to understand how the time to write audit logs changes as more events happen.
Analyze the time complexity of the following audit logging code snippet.
// Pseudocode for audit logging in GCP
function writeAuditLog(events) {
for (let event of events) {
// Prepare log entry
let logEntry = formatLog(event);
// Write log entry to Cloud Logging
cloudLogging.write(logEntry);
}
}
This code writes each event's audit log entry one by one to Cloud Logging.
- Primary operation: Loop over each event to write its log entry.
- How many times: Once for each event in the input list.
As the number of events grows, the number of log writes grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log writes |
| 100 | 100 log writes |
| 1000 | 1000 log writes |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to write audit logs grows linearly with the number of events.
[X] Wrong: "Writing multiple audit logs at once takes the same time as writing one."
[OK] Correct: Each event requires its own log write, so more events mean more time.
Understanding how audit logging scales helps you design systems that handle many events efficiently.
"What if we batch multiple events into a single log write? How would the time complexity change?"
Practice
Solution
Step 1: Understand audit logging function
Audit logging captures user and system actions in your cloud environment.Step 2: Identify the purpose
This helps with security monitoring and troubleshooting issues.Final Answer:
To record actions for security and troubleshooting -> Option BQuick Check:
Audit logging = record actions [OK]
- Confusing audit logs with storage or network features
- Thinking audit logs create resources
- Assuming audit logs improve performance
my-logs-bucket?Solution
Step 1: Identify correct gcloud command syntax
The correct command usesgcloud logging sinks createfollowed by sink name and destination URL.Step 2: Verify destination format
Cloud Storage buckets usestorage.googleapis.com/BUCKET_NAMEformat.Final Answer:
gcloud logging sinks create my-sink storage.googleapis.com/my-logs-bucket --log-filter="resource.type=\"gce_instance\"" -> Option AQuick Check:
Correct sink creation syntax = gcloud logging sinks create my-sink storage.googleapis.com/my-logs-bucket --log-filter="resource.type=\"gce_instance\"" [OK]
- Using wrong command verbs like 'create sink' or 'add sink'
- Incorrect bucket URL format (missing storage.googleapis.com)
- Using 'gs://' instead of full URL
resource.type="bigquery_resource" AND protoPayload.methodName="jobservice.jobcompleted", what logs will be exported?Solution
Step 1: Analyze the resource type filter
The filter specifiesresource.type="bigquery_resource", so logs relate to BigQuery.Step 2: Analyze the method name filter
protoPayload.methodName="jobservice.jobcompleted"means only job completion events are included.Final Answer:
All BigQuery job completion audit logs -> Option CQuick Check:
Filter matches BigQuery job completions = All BigQuery job completion audit logs [OK]
- Confusing resource types with unrelated services
- Ignoring methodName filter
- Assuming all logs are exported
Solution
Step 1: Check sink permissions
Sinks need permission to write to destinations like Pub/Sub topics.Step 2: Identify common permission issue
If logs do not appear, the sink likely lacks thepubsub.publisherrole on the topic.Final Answer:
The sink lacks permission to publish to the Pub/Sub topic -> Option AQuick Check:
Missing sink permissions = The sink lacks permission to publish to the Pub/Sub topic [OK]
- Assuming audit logging is off without checking
- Blaming filter without verifying permissions
- Ignoring typo in topic name
Solution
Step 1: Identify admin activity logs
Admin activity logs uselogName:"cloudaudit.googleapis.com/admin_activity".Step 2: Filter for Cloud Storage buckets
Useresource.type="gcs_bucket"to target storage buckets.Step 3: Confirm filter matches requirement
logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" correctly combines admin activity logs with GCS buckets.Final Answer:
logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" -> Option DQuick Check:
Admin activity + GCS bucket filter = logName:"cloudaudit.googleapis.com/admin_activity" AND resource.type="gcs_bucket" [OK]
- Using data_access logs instead of admin_activity
- Filtering by specific methods unnecessarily
- Confusing severity with log type
Ready to prove it?
Pass the test and this pattern moves unknown
