go-micro.dev/v5@v5.12.0/internal/website/docs/observability.md (about) 1 --- 2 layout: default 3 --- 4 5 # Observability 6 7 Observability in Go Micro spans logs, metrics, and traces. The goal is rapid insight into service behavior with minimal configuration. 8 9 ## Core Principles 10 11 1. Structured Logs – Machine-parsable, leveled output 12 2. Metrics – Quantitative trends (counters, gauges, histograms) 13 3. Traces – Request flows across service boundaries 14 4. Correlation – IDs flowing through all three signals 15 16 ## Logging 17 18 The default logger can be replaced. Use env vars to adjust level: 19 20 ```bash 21 MICRO_LOG_LEVEL=debug go run main.go 22 ``` 23 24 Recommended fields: 25 - `service` – service name 26 - `version` – release identifier 27 - `trace_id` – propagated context id 28 - `span_id` – current operation id 29 30 ## Metrics 31 32 Patterns: 33 - Emit counters for request totals 34 - Use histograms for latency 35 - Track error rates per endpoint 36 37 Example (pseudo-code): 38 39 ```go 40 // Wrap handler to record metrics 41 func MetricsWrapper(fn micro.HandlerFunc) micro.HandlerFunc { 42 return func(ctx context.Context, req micro.Request, rsp interface{}) error { 43 start := time.Now() 44 err := fn(ctx, req, rsp) 45 latency := time.Since(start) 46 metrics.Inc("requests_total", req.Endpoint(), errorLabel(err)) 47 metrics.Observe("request_latency_seconds", latency, req.Endpoint()) 48 return err 49 } 50 } 51 ``` 52 53 ## Tracing 54 55 Distributed tracing links calls across services. 56 57 Propagation strategy: 58 - Extract trace context from incoming headers 59 - Inject into outgoing RPC calls/broker messages 60 - Create spans per handler and client call 61 62 ## Local Development Strategy 63 64 Start with only structured logs. Add metrics when operating multiple services. Introduce tracing once debugging multi-hop latency or failures. 65 66 ## Roadmap (Planned Enhancements) 67 68 - Native OpenTelemetry exporter helpers 69 - Automatic handler/client wrapping for spans 70 - Default correlation IDs across broker messages 71 72 ## Deployment Recommendations 73 74 | Scale | Suggested Stack | 75 |-------|-----------------| 76 | Dev | Console logs only | 77 | Staging | Logs + basic metrics (Prometheus) | 78 | Prod (basic) | Logs + metrics + sampling traces | 79 | Prod (complex) | Full tracing + profiling + anomaly detection | 80 81 ## Troubleshooting 82 83 | Symptom | Cause | Fix | 84 |---------|-------|-----| 85 | Missing trace IDs in logs | Context not propagated | Ensure wrappers add IDs | 86 | Metrics server empty | Endpoint not scraped | Verify Prometheus config | 87 | High cardinality metrics | Dynamic labels | Reduce labeled dimensions | 88 89 ## Related 90 91 - [Getting Started](getting-started.md) 92 - [Plugins](plugins.md) 93 - [Architecture Decisions](architecture/index.md)