Avoid high-cardinality metrics
Avoid high-cardinality metrics
Each distinct label-value combination creates a separate time series. Cardinality is the number of such combinations. Raw URLs, customer IDs and request IDs can produce unbounded series, even with one metric name. Use stable route templates and bounded outcome labels; keep per-request detail in access-controlled logs or traces instead.
package main
import ( "fmt" "net/http" "net/http/httptest")
func main() { mux := http.NewServeMux() counts := map[string]int{} mux.HandleFunc("GET /items/{id}", func(w http.ResponseWriter, r *http.Request) { counts[r.Pattern]++; w.WriteHeader(204) }) for _, p := range []string{"/items/1", "/items/2", "/items/999"} { mux.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", p, nil)) } fmt.Println(len(counts), counts["GET /items/{id}"])}Go 1.23 Request.Pattern carries the matched ServeMux pattern. This sequential example avoids a shared-map race; production increments need synchronization or a metrics SDK. Unmatched requests need a fixed fallback label, not a raw URL.
Expected output:
1 3Estimate a series budget
package main
import ( "fmt")
func main() { routes, methods, outcomes, instances := 12, 3, 4, 5 fmt.Println(routes * methods * outcomes * instances)}This is an upper bound for one scalar metric if every combination occurs. Histograms with multiple buckets, replicas and additional dimensions multiply storage and query cost. Count actual series too; avoid adding free-text error messages as labels.
Expected output:
720Try it locally
Normalize status codes 200, 201, 404, 503 into three classes and print the number of distinct classes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reliability and observability 03: Avoid high-cardinality metrics
Check the lesson objective before moving on.
Which label should be avoided?
What happens when independent label dimensions are added?