TExcelTAD
Home › Observability: Prometheus, Grafana, Loki & OpenTelemetry › Loki: Streams, Labels & Chunks

Loki: Streams, Labels & Chunks

Grafana Loki stores logs the way Prometheus stores metrics: each log line belongs to a stream identified by a small set of labels ({app="linkly", env="prod", pod="linkly-7d9f-x2k4p"}), and only the labels are indexed. The log text itself is compressed into chunks in object storage and scanned at query time. That makes Loki far cheaper to run than full-text-indexing systems like Elasticsearch or OpenSearch, at the price of slower searches over very large ranges without good label filters.

text
 Alloy / OTel Collector / Fluent Bit
          │  push (HTTP /loki/api/v1/push or OTLP)
          ▼
   distributor ──▶ ingester (builds chunks in memory, per stream) ──flush──▶ object storage: chunks + TSDB index
                                                                                   ▲
   Grafana / logcli ──LogQL──▶ query-frontend ──▶ querier ── recent data from ingesters, older from storage

Labels: few and bounded

Everything said about metric cardinality applies, more strongly: each distinct label set is a stream with its own chunks, and many tiny streams make Loki slow and expensive.

Good labels Bad labels
app, namespace, env, cluster, container trace_id, user_id, request_id, path
level (if you filter on it constantly; it has 4–5 values) status per request, timestamps, anything unbounded

Put high-cardinality values in the log line (JSON fields), and filter on them at query time with LogQL parsers. Loki 3 adds structured metadata: key-value pairs attached to each line, not indexed and not part of the stream, ideal for trace_id (OTLP ingestion puts most OpenTelemetry attributes there).

Loki in the sandbox

Loki runs as a single binary in "monolithic" mode with a small config. The settings that matter here: filesystem storage under the home directory, an in-memory ring (no cluster), the tsdb index with schema v13 (the current recommendation), and three settings for old timestamps: accept them (reject_old_samples: false), keep chunks in the ingester instead of flushing them as "too old" (max_chunk_age), and let queries for old times reach the ingesters (query_ingesters_within). The session pushes six lines in two streams with the push API and inspects what Loki indexed:

bash · try it yourself
cat > loki.yaml <<'YML'
auth_enabled: false                       # single tenant ("fake")
server: {http_listen_address: 127.0.0.1, http_listen_port: 3100, grpc_listen_address: 127.0.0.1, log_level: error}
common:
  path_prefix: /home/learner/loki
  instance_addr: 127.0.0.1
  replication_factor: 1
  ring: {kvstore: {store: inmemory}}
  storage: {filesystem: {chunks_directory: /home/learner/loki/chunks, rules_directory: /home/learner/loki/rules}}
schema_config:
  configs:
    - {from: 2020-01-01, store: tsdb, object_store: filesystem, schema: v13, index: {prefix: index_, period: 24h}}
limits_config: {reject_old_samples: false}
ingester: {max_chunk_age: 87600h, chunk_idle_period: 87600h, lifecycler: {min_ready_duration: 0s}}
querier: {query_ingesters_within: 87600h}
analytics: {reporting_enabled: false}
YML
loki -config.file=loki.yaml >loki.log 2>&1 &
for i in $(seq 150); do [ "$(curl -s 127.0.0.1:3100/ready)" = ready ] && break; sleep 0.2; done

cat > push.json <<'JSON'
{"streams": [
  {"stream": {"app": "linkly", "env": "prod", "pod": "linkly-0"},
   "values": [["1767261601000000000", "{\"level\":\"INFO\",\"msg\":\"redirect\",\"status\":302,\"duration_ms\":4}"],
              ["1767261605000000000", "{\"level\":\"ERROR\",\"msg\":\"create link failed\",\"status\":500,\"duration_ms\":2003}"],
              ["1767261611000000000", "{\"level\":\"INFO\",\"msg\":\"link created\",\"status\":201,\"duration_ms\":15}"]]},
  {"stream": {"app": "linkly", "env": "prod", "pod": "linkly-1"},
   "values": [["1767261602000000000", "{\"level\":\"INFO\",\"msg\":\"redirect\",\"status\":302,\"duration_ms\":6}"],
              ["1767261607000000000", "{\"level\":\"WARN\",\"msg\":\"slow redirect\",\"status\":302,\"duration_ms\":480}"],
              ["1767261609000000000", "{\"level\":\"ERROR\",\"msg\":\"create link failed\",\"status\":500,\"duration_ms\":2001}"]]}
]}
JSON
curl -s -o /dev/null -w 'push: HTTP %{http_code}\n' -H 'Content-Type: application/json' --data @push.json 127.0.0.1:3100/loki/api/v1/push

R='--from=2026-01-01T10:00:00Z --to=2026-01-01T11:00:00Z'
echo "--- labels:";        logcli labels --quiet $R
echo "--- values of pod:"; logcli labels --quiet $R pod
echo "--- streams:";       logcli series --quiet $R '{app="linkly"}' | sort   # unordered
echo "--- lines, oldest first:"
logcli query --quiet $R --forward '{app="linkly", env="prod"}'
push: HTTP 204
--- labels:
app
env
pod
service_name
--- values of pod:
linkly-0
linkly-1
--- streams:
{app="linkly", env="prod", pod="linkly-0", service_name="linkly"}
{app="linkly", env="prod", pod="linkly-1", service_name="linkly"}
--- lines, oldest first:
2026-01-01T10:00:01Z {detected_level="info", pod="linkly-0"}  {"level":"INFO","msg":"redirect","status":302,"duration_ms":4}
2026-01-01T10:00:02Z {detected_level="info", pod="linkly-1"}  {"level":"INFO","msg":"redirect","status":302,"duration_ms":6}
2026-01-01T10:00:05Z {detected_level="error", pod="linkly-0"} {"level":"ERROR","msg":"create link failed","status":500,"duration_ms":2003}
2026-01-01T10:00:07Z {detected_level="warn", pod="linkly-1"}  {"level":"WARN","msg":"slow redirect","status":302,"duration_ms":480}
2026-01-01T10:00:09Z {detected_level="error", pod="linkly-1"} {"level":"ERROR","msg":"create link failed","status":500,"duration_ms":2001}
2026-01-01T10:00:11Z {detected_level="info", pod="linkly-0"}  {"level":"INFO","msg":"link created","status":201,"duration_ms":15}
  • Timestamps in the push API are nanoseconds since the epoch, as strings. 1767261601 is 2026-01-01 10:00:01 UTC.
  • service_name appeared without being pushed: Loki 3 derives it from common labels (service, app, container, …) so every stream has one; Grafana's Logs Drilldown app groups by it.
  • logcli query prints the timestamp, the labels that are not common to all results, and the line. Lines from both streams are merged by time.
  • detected_level was not pushed either: Loki detected each line's level from its level field and stored it as structured metadata (not a stream label, so it doesn't create streams). Grafana colours lines by it.

Storage, retention and modes

Topic What to know
storage chunks and the TSDB index go to object storage (S3, GCS, Azure Blob) in production; local disk only for single-node setups
retention limits_config.retention_period (and per-stream overrides), enforced by the compactor with retention_enabled: true
deployment monolithic (one binary, small), simple scalable (read/write/backend targets), or microservices (each component separately) via the official Helm chart
limits ingestion_rate_mb, max_streams_per_user, max_line_size, max_query_series, … per tenant; errors like 429 Maximum active stream limit exceeded mean a label is too cardinal
multi-tenancy auth_enabled: true and the X-Scope-OrgID header per request

Grafana Cloud Logs is Loki as a service; the query language and push API are the same.

Test yourself

What does Loki index?

  1. Every word of every log line
  2. Only the stream labels; line content is scanned at query time
  3. Nothing
  4. Only error lines

Where should a request's trace_id go in Loki?

  1. A stream label
  2. In the log line (JSON field) or structured metadata
  3. In the tenant ID
  4. Nowhere

What unit are timestamps in Loki's push API?

  1. Seconds
  2. Milliseconds
  3. Nanoseconds since the epoch, as strings
  4. RFC 3339 strings