夜莺-Nightingale
夜莺V7
项目介绍 功能概览
部署升级 部署升级
数据接入 数据接入
告警管理 告警管理
数据查看 数据查看
功能介绍 功能介绍
数据库表结构 数据库表结构
API FAQ
夜莺V6
项目介绍 架构介绍
快速开始 快速开始
黄埔营
安装部署 安装部署
升级
采集器 采集器
使用手册 使用手册
API API
数据库表结构 数据库表结构
FAQ FAQ
开源生态
Prometheus
版权声明
第1章:天降奇兵 第1章:天降奇兵
第2章:探索PromQL 第2章:探索PromQL
第3章:Prometheus告警处理 第3章:Prometheus告警处理
第4章:Exporter详解 第4章:Exporter详解
第5章:数据与可视化 第5章:数据与可视化
第6章:集群与高可用 第6章:集群与高可用
第7章:Prometheus服务发现 第7章:Prometheus服务发现
第8章:监控Kubernetes 第8章:监控Kubernetes
第9章:Prometheus Operator 第9章:Prometheus Operator
参考资料

VictoriaLogs 查询

VictoriaLogs 日志告警通过 LogsQL 进行日志统计分析,及时发现异常并触发告警。

配置说明

查询统计

查询条件

在查询条件输入框中输入 LogsQL 统计查询语句,查询结果会赋值给变量 $A。

查询语句格式:<过滤条件> | stats <统计函数>

点击「数据预览」按钮可以查看查询结果,验证查询语句是否正确。

阈值判断

基于查询统计的结果设置告警阈值,例如 $A > 10 表示统计值大于 10 时触发告警。

请确保所有变量均有数据,且它们的标签一致,否则无法进行阈值判断。

数据缺失

配置当查询无数据时的处理策略。

LogsQL 查询语法

过滤条件

关键字查询

error                           # 查询包含 "error" 的日志
"connection timeout"            # 查询包含完整短语的日志

字段过滤

_stream:{host="server1"}        # 匹配 stream 标签
level:=error                    # 精确匹配字段值
level:error                     # 字段包含某值
status:>=500                    # 数值比较

逻辑运算符

error AND timeout               # 同时包含
error OR warning                # 包含任一
error NOT debug                 # 排除

通配符和正则

err*                            # 前缀匹配
/error|warning/i                # 正则匹配(i 表示忽略大小写)

时间过滤

使用 _time 字段筛选时间范围,这在告警场景中非常常用:

_time:1m                        # 最近 1 分钟
_time:5m                        # 最近 5 分钟
_time:10m                       # 最近 10 分钟
_time:30m                       # 最近 30 分钟
_time:1h                        # 最近 1 小时
_time:1d                        # 最近 1 天

时间范围查询:

_time:[now-5m, now]             # 最近 5 分钟(等同于 _time:5m)
_time:[2024-01-01, 2024-01-02]  # 指定日期范围
_time:["2024-01-01 10:00", "2024-01-01 12:00"]  # 指定时间范围

组合使用示例:

_time:5m AND level:=error       # 最近 5 分钟的错误日志
_time:10m AND status:>=500      # 最近 10 分钟的 5xx 错误

统计管道(stats)

查询语句必须使用 | stats 管道进行统计,常用统计函数:

函数 说明 示例
count() 统计日志数量 stats count() as total
count_uniq(field) 统计字段唯一值数量 stats count_uniq(user_id) as users
sum(field) 求和 stats sum(bytes) as total_bytes
avg(field) 平均值 stats avg(response_time) as avg_rt
min(field) 最小值 stats min(latency) as min_latency
max(field) 最大值 stats max(latency) as max_latency

分组统计(by)

使用 by (field) 按字段分组,分组后每个分组会独立产生告警:

error | stats by (host) count() as count

详细的 LogsQL 语法请参考 VictoriaLogs 官方文档

使用样例

示例1:错误日志数量监控(5分钟)

查询条件:

_time:5m AND level:=error AND _stream:{service="payment"} | stats count() as count

阈值判断: $A > 10

示例2:按主机分组监控错误(5分钟)

查询条件:

_time:5m AND level:=error | stats by (host) count() as count

阈值判断: $A > 50

每个主机独立告警。

示例3:5xx 状态码监控(10分钟)

查询条件:

_time:10m AND status:>=500 AND status:<600 | stats count() as count

阈值判断: $A > 100

示例4:按服务和状态码分组统计(10分钟)

查询条件:

_time:10m AND status:>=400 | stats by (service, status) count() as count

阈值判断: $A > 50

示例5:平均响应时间监控(5分钟)

查询条件:

_time:5m AND _stream:{app="api-gateway"} | stats avg(response_time) as avg_rt

阈值判断: $A > 1000

示例6:关键字告警(10分钟)

查询条件:

_time:10m AND ("timeout" OR "connection refused" OR "out of memory") | stats count() as count

阈值判断: $A > 5

示例7:正则表达式匹配(5分钟)

查询条件:

_time:5m AND /exception|panic|fatal/i AND _stream:{env="production"} | stats by (service) count() as count

阈值判断: $A > 20

示例8:唯一用户数监控(30分钟)

查询条件:

_time:30m AND level:=error | stats count_uniq(user_id) as affected_users

阈值判断: $A > 100

快猫星云 联系方式 快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云
OpenSource
开源版
Flashcat
Flashcat
Flashduty
Flashduty