Loki Data Source
Loki is Grafana’s horizontally-scalable, highly-available log aggregation system inspired by Prometheus. Query logs using LogQL, a powerful query language designed for log exploration.Overview
The Loki data source provides:- LogQL query language for log filtering and parsing
- Log stream exploration with label browser
- Metrics extraction from logs
- Live tailing of log streams
- Log context and correlation
Source:
public/app/plugins/datasource/loki/Configuration
Connection Settings
Data Source Options
Maximum number of log lines to retrieve. Higher values may impact performance.
Extract fields from logs and link to other data sources:
matcherRegex: Regular expression to extract valuename: Field display namedatasourceUid: Target data source UID for navigationurl: Custom URL template (alternative to datasourceUid)
Query Editor
The Loki query editor provides:- Label Browser
- Code Editor
Visual interface for selecting log streams:
- Click Label browser
- Select labels (job, namespace, pod, etc.)
- Choose label values
- Build stream selector automatically
{job="mysql", namespace="prod"}LogQL Query Language
Log Stream Selectors
Select log streams using label matchers:Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:13Multiple Label Matchers
Combine labels with AND logic:Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:129Log Pipeline Operators
Line Filters
Exact match:Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:134-141Parser Expressions
Extract labels from log lines: logfmt parser:level=info msg="Server started" port=8080
JSON parser:
Label Filters
Filter on parsed labels:- Targets the MySQL job
- Keeps logs containing “metrics”
- Parses with logfmt
- Filters where duration > 10 seconds
Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:18-23Metric Queries
Extract metrics from logs:Count Over Time
Count log lines in time windows:Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:25-28Rate of Errors
Calculate per-second rate of filtered logs:Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:30-34Aggregations
Sum, count, and group metrics:level label (extracted from logs).
Source:
public/app/plugins/datasource/loki/components/LokiCheatSheet.tsx:36-39Unwrap
Extract numeric values from logs:bytes field over 5-minute windows.
Advanced Queries
Multiple Filters
Label Formatting
Query Options
Query Type
range: Return logs over time range (default)instant: Return logs at a single point in timestream: Live tail logs (real-time)
Line Limit
Maximum number of log lines to return per query.
Resolution
Step size for metric queries (e.g.,
1, 1/2).Live Tailing
Tail logs in real-time:Log Context
View surrounding log lines for context:- Click on any log line in the results
- Select Show context
- Grafana retrieves logs before and after the selected line
Template Variables
Label Names
List all available labels:Label Values
Get values for a specific label:Using Variables
Derived Fields
Extract structured data from logs and link to other data sources:Trace ID Extraction
Link logs to distributed traces:Custom URL Links
Create links to external systems:Performance Optimization
Use Specific Labels
Always filter with labels first:More efficient than:
Limit Time Range
Query shorter time ranges for faster results. Use the time picker or:
Reduce Line Limit
Lower Increase only when needed.
maxLines for exploratory queries:Avoid Regex When Possible
Use exact match for better performance:Instead of:
Troubleshooting
No log lines returned
No log lines returned
- Verify label selector matches ingested logs
- Check time range includes log data
- Confirm Loki is receiving logs (check
/metrics) - Review
maxLinessetting - may need to increase
Query timeout
Query timeout
- Reduce time range
- Add more specific label filters
- Lower
maxLinesparameter - Check Loki query timeout settings
Missing labels in autocomplete
Missing labels in autocomplete
- Verify labels exist in selected time range
- Check Loki label API:
/loki/api/v1/labels - Ensure data source URL is correct
Parser not extracting fields
Parser not extracting fields
- Verify log format matches parser (logfmt, JSON, etc.)
- Test regex patterns at regex101.com
- Check for escaped characters in log lines
Best Practices
- Always start with labels:
{job="api"}before line filters - Use structured logging: JSON or logfmt for easier parsing
- Index high-cardinality fields: Configure indexed labels in Loki
- Aggregate in Loki: Use metric queries instead of downloading all logs
- Set appropriate retention: Configure log retention based on use case