loki
Loki
LogQL Syntax Gotchas
Stream Selector (required first)
{app="nginx", namespace=~"prod|staging"}
- At least one label matcher required
=~is regex, not glob (use.*not*)
Filter Order Matters
{app="api"} |= "error" | json | level="error" | line_format "{{.message}}"
Filters apply left-to-right. Put cheap filters (string match) before expensive ones (json parse).
Parser Output
After | json or | logfmt, extracted fields become labels for filtering:
{app="api"} | json | status_code >= 400 | duration > 1s
Line vs Label Filters
|=!=|~!~filter on log line content (before parsing)- Label matchers after parser filter on extracted fields
Metric Queries
# Logs per second
rate({app="nginx"}[5m])
# Error rate percentage
sum(rate({app="api"} |= "error" [5m])) / sum(rate({app="api"}[5m]))
# Extract numeric value for aggregation
quantile_over_time(0.95, {app="api"} | json | unwrap duration [5m]) by (endpoint)
unwrap Gotchas
- Requires parsed numeric field
- Add
| __error__=""to filter parse failures - Supports unit conversion:
unwrap duration(latency),unwrap bytes(size)
Promtail Pipeline
pipeline_stages:
- cri: {} # Parse container runtime format first
- json:
expressions:
level: level
msg: message
- labels:
level: # Promote extracted field to label
- timestamp:
source: time
format: RFC3339Nano
- output:
source: msg # Replace log line with extracted field
Stage Order
criordocker(parse container format)multiline(if needed)regex/json/logfmt(extract fields)labels(promote to index)timestamp(set log timestamp)output(modify final line)
Cardinality Warning
Labels are indexed. High-cardinality labels (user IDs, trace IDs) cause:
- Index bloat
- Query performance degradation
- Ingestion rate limits
Keep extracted fields as line content unless you need to filter by them.
Common Patterns
# Errors with context
{namespace="prod"} |= "error" | json | line_format "{{.timestamp}} [{{.service}}] {{.message}}"
# Logs missing (for alerting)
absent_over_time({app="critical"}[5m])
# Top error messages
topk(10, sum by (message) (count_over_time({app="api"} | json | level="error" [1h])))
More from kontrolplane/skills
kyverno
Kyverno Kubernetes policy engine for validation, mutation, and generation. Use when writing ClusterPolicies to enforce security standards, auto-mutate resources with defaults, generate companion resources, or verify container image signatures.
12prometheus
Prometheus metrics and PromQL queries. Use when writing PromQL queries, creating recording or alerting rules, debugging metric scraping issues, or understanding counter/gauge/histogram behavior.
4argocd
ArgoCD GitOps continuous delivery for Kubernetes. Use when creating or debugging ArgoCD Application/ApplicationSet manifests, configuring sync policies, troubleshooting OutOfSync or degraded states, or integrating Helm/Kustomize sources.
3grafana
Grafana dashboard JSON configuration and alerting. Use when creating or editing dashboard JSON, configuring panels programmatically, setting up Grafana alerting rules, or troubleshooting visualization issues.
3kubernetes
Kubernetes resource configuration and troubleshooting. Use when debugging pod failures, configuring probes and resource limits, setting up RBAC or NetworkPolicies, or resolving common Kubernetes errors like CrashLoopBackOff or ImagePullBackOff.
3terraform
Terraform infrastructure as code with HCL. Use when writing Terraform configurations, debugging state issues, understanding count vs for_each behavior, managing modules, or troubleshooting plan/apply errors.
3