Loki (Log Aggregation) Cheat Sheet
Grafana Loki log aggregation covering LogQL queries, label design, Promtail/Alloy config, and retention tuning.
2 PagesIntermediateFeb 18, 2026
LogQL Basics
Core LogQL syntax for selecting and filtering log streams.
bash
# Stream selector - filter by labels{app="checkout", env="prod"}# Line filter - substring match{app="checkout"} |= "error"# Negative line filter{app="checkout"} != "healthcheck"# Regex line filter{app="checkout"} |~ "timeout|refused"# Parse JSON logs into labels{app="checkout"} | json# Filter on a parsed field{app="checkout"} | json | status_code >= 500# logfmt parsing{app="checkout"} | logfmt | duration > 500ms
Metric Queries (Aggregations)
Turn log streams into time-series metrics for alerting and dashboards.
bash
# Log lines per second matching a filtersum(rate({app="checkout"} |= "error" [5m]))# Rate grouped by labelsum by (pod) (rate({app="checkout"}[5m]))# Count over timecount_over_time({app="checkout"} |= "panic" [1h])# Extract a numeric field and compute quantile latencyquantile_over_time(0.99, {app="checkout"} | json | unwrap duration [5m]) by (route)# Bytes ingested per streamsum(bytes_rate({app="checkout"}[5m])) by (pod)
Promtail / Grafana Alloy Scrape Config
Minimal Promtail config to ship container logs to Loki with pipeline stages.
yaml
server: http_listen_port: 9080clients: - url: http://loki:3100/loki/api/v1/pushscrape_configs: - job_name: kubernetes-pods kubernetes_sd_configs: - role: pod pipeline_stages: - docker: {} - json: expressions: level: level msg: message - labels: level: relabel_configs: - source_labels: [__meta_kubernetes_pod_label_app] target_label: app - source_labels: [__meta_kubernetes_namespace] target_label: namespace
Key Loki Server Config Options
Common limits_config and storage settings you'll tune in production.
- retention_period- how long chunks are kept before compactor deletes them, set in limits_config
- ingestion_rate_mb- per-tenant ingestion rate limit; raise for high-volume tenants
- max_query_series- caps how many streams a single query can touch, protects against runaway queries
- split_queries_by_interval- breaks large time-range queries into parallel chunks for the query-frontend
- chunk_encoding- compression codec for chunks (snappy, gzip, zstd); zstd gives best ratio
- boltdb-shipper / tsdb- index store types; tsdb is the modern recommended index for Loki 2.9+
Pro Tip
Never put high-cardinality values (user IDs, request IDs, trace IDs) in Loki labels — put them in the log line and query with `| json | field="x"` instead, since every unique label combination creates a separate stream and blows up your index.
Was this cheat sheet helpful?
Explore Topics
#LokiLogAggregation#LokiLogAggregationCheatSheet#DevOps#Intermediate#LogQLBasics#MetricQueriesAggregations#Promtail#Grafana#Databases#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance