Using Grafana Variables to Create Dynamic Dashboards
Count Logs Matching a Specific Pattern
Count all logs containing the word "error":
count_over_time({host="$host"} |= "error" [1m])
Rate of Logs Matching a Pattern
Calculate the rate of logs containing "timeout" over a 5-minute window:
rate({host="$host"} |= "timeout" [5m])
Histogram of Log Message Lengths
Create a histogram of log message sizes:
histogram_over_time({host="$host"} | line_format "{{.message}}" [10m])
Error Rate by Service
Calculate the error rate for each service using the level label:
rate({level="error"}[1m])
Top Hosts with High Log Volume
Find the hosts with the highest log volume over a 5-minute interval:
topk(5, count_over_time({host="$host"}[5m]))
Log Patterns Over Time
Visualize the frequency of a log message pattern:
count_over_time({host="$host"} |= "error" [1m])
Log Rate Grouped by Label
Calculate the log rate per namespace:
sum by (detected_level) (rate({host="$host"}[5m]))
Compare Log Levels
Compare the rates of info and error logs:
rate({level="info"}[5m]) / rate({level="error"}[5m])
Custom Parsing and Aggregation
Parse log messages to extract a custom field and sum values:
sum by (service) (
rate({job="api"} | json | unwrap request_time [5m])
)