logQL简介
存储在Loki中的日志,是按行存储的,而LogQL就是可以快速按各种条件检索出日志的一种手段(语言)
LogQL:Loki 提供的日志查询语言,类似 Prometheus 的 PromQL,LogQL也是使用标签和运算符进行过滤,它主要分为两个部分:
- log stream selector (日志流选择器)
- filter expression (过滤器表达式)
logQL有两种形式的Query:
- Log queries:按行返回log
{job="portal_log"}|= `ERROR`
- Metric queries:计算统计query的结果,可以使用sum、count_over_time等函数。
count_over_time(({host=~"192.168.*"}|~"error")[2m])
日志告警规则示例:
1、日志中出现大量error
groups:
- name: error-alerting
rules:
- alert: 大量报错日志
expr: count_over_time(({host=~"192.168.*"}|~"error")[2m]) >10
for: 0m
labels:
severity: warnning
instance: "logs"
annotations:
summary: Too many error logs
description: Too many error logs
2、日志中错误率超过5%
groups:
- name: portal_log_error_rate_alerting
rules:
- alert: portal_log_error_rate
expr: sum(rate({job="portal_log"} |= "error" [5m])) by (job) / sum(rate({job="portal_log"}[5m])) by (job) > 0.05
for: 10m
annotations:
summary: "portal_log 日志的错误率大于5%"
description: "portal_log 日志的错误率大于5%"
3、日志中出现“WARN”、“hasAccessAuthorize fail”日志,并输出日志行
groups:
- name: notice_access_authorize_alerting
rules:
- alert: notice_access_authorize_alerting
expr: sum by (host, job, error) (count_over_time({job="notice_log"} |= `WARN` |= `hasAccessAuthorize fail`| regexp "(?P<error>.*)$"[5s])) > 0
for: 0m
annotations:
summary: "notice hasAccessAuthorize fail, host:{{ $labels.host }} \n 异常日志:{{ $labels.error }}"
description: notice hasAccessAuthorize fail