Lesson 6 of 9 · 25m
Loki Label Design and the Cost of LogQL
Why labels are Loki's index, how cardinality destroys it, and how to design streams that stay cheap and queryable.
Loki's design is often described as 'Prometheus for logs', which is accurate and dangerous. Like Prometheus, it lives or dies by label cardinality. Unlike metrics, logs tempt people to attach unique identifiers as labels because it feels convenient in the moment.
Labels are the index
Loki indexes labels, not the full text of every log line. A stream is roughly a unique set of label values:
{service="checkout", namespace="prod", pod="checkout-6c9f9f7f4-xv2kq"}Good labels: service, environment, namespace, cluster, perhaps level. Bad labels: user ID, request ID, trace ID, full URL, email, session token, thread name. Every unique value creates another stream; a million users means a million streams, many tiny chunks, massive index growth, and angry operators.
Keep identifiers in the log body
Structured log:
{"ts":"2026-11-29T22:10:04Z","level":"error","service":"checkout","trace_id":"4f1c9a","msg":"payment failed","customer":"acct_9182"}Labels only:
service="checkout"
environment="prod"
namespace="payments"The trace ID is searchable as text or structured metadata depending on your Loki version and configuration. It must not become a stream label.
LogQL query shape determines cost
Cheap: {service="checkout", environment="prod"} |= "payment failed" selects a small set of streams and scans a bounded body of chunks.
Expensive: {environment="prod"} |~ ".*payment.*failed.*" | json | latency_ms > 500 scans every production stream, applies a regex, parses JSON, then filters. Sometimes that is necessary; it is not dashboard material.
Parsing after selection is the discipline:
{service="checkout", environment="prod"}
|= "payment failed"
| json
| line_format "{{.ts}} {{.trace_id}} {{.customer}}"Storage layout is object-storage first
Typical Loki deployments use object storage for chunks. Operationally, the pain is ingester replication, compactor lag, and tenant cardinality explosions; watch loki_ingester_memory_streams, index lookups per query, and compactor pending-delete age.
Field notes
- If a label value can exceed a few hundred distinct values, it probably does not belong as a label.
levelas a label looks attractive but duplicates streams; structured metadata or parsed filtering may be a better fit depending on version.- Queries are cheapest when the stream selector is narrow. Train teams to select service first, then filter text.
- Multi-tenancy is not just access control. It is a blast-radius boundary for cardinality mistakes.
Was this lesson helpful?