GCP for DevOps — Error Reporting & Profiler
Error Reporting & Profiler
Error Reporting automatically groups and deduplicates errors from your applications. Cloud Profiler continuously profiles production applications to identify CPU and memory hotspots with minimal overhead.
Error Reporting
How It Works
Error Reporting Flow:
┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
│ Application │───▶│ Cloud │───▶│ Error Reporting │
│ (exception) │ │ Logging │ │ │
│ │ │ │ │ • Group errors │
│ Stack trace │ │ Log entry │ │ • Count / trend │
│ logged to │ │ with severity │ │ • Notify │
│ stderr/stdout │ │ ERROR │ │ • Link to source │
└──────────────┘ └──────────────┘ └──────────────────┘
Error Reporting automatically detects errors from:
- Cloud Functions, Cloud Run, App Engine, GKE
- Compute Engine VMs (with Ops Agent)
- Any service writing structured error logs
Report Errors from Code
from google.cloud import error_reporting
client = error_reporting.Client()
try:
result = 1 / 0
except ZeroDivisionError:
client.report_exception()
# Report a custom error
client.report("Custom error: database connection timeout")
Structured Error Logging
Cloud Logging entries with severity ERROR and a stack trace are automatically picked up:
import traceback
import json
def handle_request():
try:
# Application logic
process_data()
except Exception as e:
error_entry = {
"severity": "ERROR",
"message": str(e),
"stack_trace": traceback.format_exc(),
"serviceContext": {
"service": "my-api",
"version": "v1.2.3",
},
}
print(json.dumps(error_entry))
Error Groups
Error Reporting groups similar errors together:
| Feature | Description |
|---|---|
| Grouping | Same stack trace = same group |
| Count | Total occurrences over time |
| Trend | Increasing, decreasing, stable |
| First/Last seen | When error first/last occurred |
| Affected services | Which services are impacted |
| Resolution status | Open, acknowledged, resolved, muted |
Cloud Profiler
How Profiler Works
Profiler Architecture:
┌──────────────────┐ ┌──────────────┐
│ Application │ │ Cloud │
│ │ │ Profiler │
│ ┌──────────────┐ │ │ Service │
│ │ Profiler │─┼───▶│ │
│ │ Agent │ │ │ • Collect │
│ │ │ │ │ • Analyze │
│ │ ~0.5% CPU │ │ │ • Flame graph │
│ │ overhead │ │ │ • Comparison │
│ └──────────────┘ │ └──────────────┘
└──────────────────┘
Enable Profiler (Python)
# pip install google-cloud-profiler
import googlecloudprofiler
def main():
googlecloudprofiler.start(
service="my-api",
service_version="1.0.0",
verbose=0,
)
# Your application code
start_server()
Enable Profiler (Go)
import "cloud.google.com/go/profiler"
func main() {
cfg := profiler.Config{
Service: "my-api",
ServiceVersion: "1.0.0",
}
if err := profiler.Start(cfg); err != nil {
log.Fatalf("Profiler failed: %v", err)
}
// Your application code
}
Profile Types
| Profile Type | What It Measures | Use Case |
|---|---|---|
| CPU time | Time spent executing code | Find CPU-intensive functions |
| Wall time | Real elapsed time | Find I/O waiting, blocking |
| Heap | Memory allocation | Find memory leaks |
| Allocated heap | Total bytes allocated | Find allocation hotspots |
| Threads | Active goroutines/threads | Find concurrency issues |
| Contention | Lock contention time | Find synchronization bottlenecks |
Analyzing Flame Graphs
In the Cloud Console:
- Navigate to Profiler
- Select service, profile type, and time range
- Read the flame graph:
- Width = time spent in that function
- Depth = call stack depth
- Color = different packages/modules
- Compare across versions or time periods
Web Security Scanner
# Create a scan config
$ gcloud beta web-security-scanner scan-configs create \
--display-name="My App Scan" \
--starting-urls="https://myapp.example.com"
# Run a scan
$ gcloud beta web-security-scanner scan-runs start SCAN_CONFIG_ID
# List scan results
$ gcloud beta web-security-scanner scan-runs list SCAN_CONFIG_ID
Common Mistakes
| Mistake | Why It’s Wrong | Fix |
|---|---|---|
| No stack traces in error logs | Errors not grouped properly | Always log full stack traces |
Missing serviceContext |
Can’t filter by service | Add service name and version |
| Not resolving error groups | Stale errors pile up | Regularly triage and resolve |
| Profiler not enabled in prod | Can’t diagnose prod issues | Enable with minimal overhead |
| No version labeling | Can’t compare across deploys | Always set service version |
Quick Reference
| Feature | Access |
|---|---|
| Error Reporting | Console > Error Reporting |
| Cloud Profiler | Console > Profiler |
| Web Security Scanner | Console > Security > Web Security Scanner |
| Error Reporting API | google.cloud.error_reporting client |
| Profiler Agent | google-cloud-profiler package |
Next → Practice Exercises