Exploratory analysis of GKE container logs via gcloud CLI — discovers containers, log patterns, error rates, and anomalies across GCP projects.
Perform exploratory analysis of GKE container logs to understand what data is available, identify patterns, classify errors, and detect anomalies.
Query a sample of logs to identify all active containers in a project/namespace:
gcloud logging read \
'resource.type="k8s_container" AND resource.labels.namespace_name="NAMESPACE"' \
--project=PROJECT_ID \
--limit=500 \
--freshness=1d \
--format=json > /tmp/discover_logs.json
Extract unique containers with Python:
import json
data = json.load(open('/tmp/discover_logs.json'))
containers = set()
for entry in data:
name = entry.get('resource', {}).get('labels', {}).get('container_name', 'unknown')
containers.add(name)
for c in sorted(containers):
print(c)
For each discovered container, pull a focused sample:
gcloud logging read \
'resource.type="k8s_container" AND resource.labels.container_name="CONTAINER_NAME" AND resource.labels.namespace_name="NAMESPACE"' \
--project=PROJECT_ID \
--limit=200 \
--freshness=2d \
--format=json > /tmp/sample_CONTAINER.json
Parse each sample to identify:
ANSI color stripping (Java Spring Boot logs):
import re
clean = re.sub(r'\x1b\[[0-9;]*m|\[[\d;]*m', '', raw_text)
Compare findings across containers:
httpRequest field)?Produce a summary per container:
| Container | Log Format | Volume (est/day) | Error Rate | Key Patterns |
|---|---|---|---|---|
| name | format | count | % | patterns found |
--limit and narrow time windows.roles/logging.viewer (minimum) on target project(s)