Skip to content

Grafana Loki Label Model Cardinality Explosion Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Grafana Loki Label Model Cardinality Explosion Fix. We cover key concepts, practical examples, and best practices.

Your Grafana Loki cluster slows down and struggles with queries — label_names() returns thousands of unique values for a single label. High-cardinality labels like user_id, request_id, or ip_address create too many streams, degrading query performance and increasing storage.

The Problem

# Promtail config with high-cardinality labels
scrape_configs:
  - job_name: myapp
    static_configs:
      - targets: [localhost]
        labels:
          job: myapp
          user_id: __user_id__  # WRONG: user_id is high-cardinality
          request_id: __request_id__  # WRONG: each request is unique

Each unique user_id creates a new stream in Loki. With 10,000 users, this creates 10,000 streams — overwhelming the ingester.

Step-by-Step Fix

1. Identify high-cardinality labels

// Check label cardinality
max_over_time(
  count_over_time({job="myapp"} |= "error" [1h])
) by (user_id)

// Use Grafana's label explorer to see value counts
// Labels with 1000+ unique values are high-cardinality

2. Restructure labels to low cardinality

# Promtail config with fixed labels
scrape_configs:
  - job_name: myapp
    static_configs:
      - targets: [localhost]
        labels:
          job: myapp                # Low cardinality (1-10 values)
          env: production            # Low cardinality (3-5 values)
          service: web               # Low cardinality (5-20 values)
          level: info                # Low cardinality (5 values)

3. Move high-cardinality values to structured metadata

# Promtail config with structured metadata
scrape_configs:
  - job_name: myapp
    pipeline_stages:
      - json:
          expressions:
            user_id: user_id
            request_id: request_id
      - structured_metadata:
          user_id:     # Structured metadata, not labels
          request_id:  # Structured metadata, not labels

4. Query with structured metadata

{job="myapp", env="production"} |= "error" | user_id = "12345"

// Structured metadata is searchable but not indexed as labels
// More efficient than creating a stream per user_id

5. Monitor label cardinality

// Count unique label values
count by (__label_name__) (
  label_replace(
    {job="myapp"},
    "__label_name__",
    "$1",
    "",
    "(.+)"
  )
)

// Set alerts in Grafana for cardinality spikes
// Action: Investigate new labels or values

Expected output after fix:

Before: 10,000 streams (user_id label)
After: 5 streams (job, env, service labels)
Query performance: improved 20x

Prevention Tips

  • Keep labels to low-cardinality values (<100 unique values per label)
  • Use structured metadata for dynamic fields (user_id, request_id, IP)
  • Monitor label cardinality in Grafana with Loki label explorer
  • Set up alerting when new labels appear unexpectedly
  • Restrict label creation with Loki's per-stream limit configuration

Common Mistakes with loki label

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world GRAFANA code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the maximum recommended cardinality per label?

Keep label values under 10,000 per label, with a hard limit of 100-500 per label for optimal performance. Total streams per ingester should stay under 1 million. Use structured metadata for any field with unbounded unique values.

How do I fix an existing cardinality explosion?

(1) Identify the high-cardinality label with label_values(). (2) Remove it from the label configuration in Promtail or the Loki push API. (3) Restart the agents to re-ingest logs with the new label set. (4) Old logs with the bad labels remain — consider retention or re-ingestion.

What's the difference between labels and structured metadata?

Labels are indexed — they make queries fast but create streams. Structured metadata is stored with the log line but not indexed — it is queryable but does not create new streams. Use labels for topology (job, env, service) and structured metadata for operational context (user_id, trace_id).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro