Metrics
Metrics
Section titled “Metrics”go-zero automatically records key metrics for HTTP and gRPC services and exposes them via a Prometheus scrape endpoint.
Enable
Section titled “Enable”Prometheus: Host: 0.0.0.0 Port: 9101 Path: /metricsAutomatic Metrics
Section titled “Automatic Metrics”HTTP service:
| Metric | Description |
|---|---|
http_server_requests_total | Total requests by route and status |
http_server_duration_ms | Request duration histogram |
http_server_active_requests | In-flight requests gauge |
gRPC service:
| Metric | Description |
|---|---|
rpc_server_requests_total | Total RPC calls by method and code |
rpc_server_duration_ms | RPC duration histogram |
Custom Metrics
Section titled “Custom Metrics”import "github.com/zeromicro/go-zero/core/metric"
// CounterordersTotal := metric.NewCounterVec(&metric.CounterVecOpts{ Namespace: "order", Subsystem: "service", Name: "created_total", Help: "Total orders created", Labels: []string{"status"},})ordersTotal.Inc("success")
// Histogramlatency := metric.NewHistogramVec(&metric.HistogramVecOpts{ Namespace: "order", Name: "payment_duration_ms", Help: "Payment processing latency", Labels: []string{"provider"}, Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000},})latency.Observe(float64(elapsed.Milliseconds()), "stripe")Grafana
Section titled “Grafana”go-zero provides community dashboards on Grafana’s marketplace — search for “go-zero”.