跳到主要内容

监控

VEF 内置了一个监控 service,以及一个用于运行时检查的内置资源。

模块输出

监控模块会提供:

输出含义
monitor.Service运行时监控服务
sys/monitor内置 RPC 资源

当需要时,service 会通过生命周期 hook 自动初始化与关闭。

monitor.Service 接口

公开监控 service 暴露的方法如下:

方法返回类型作用
Overview(ctx)*monitor.SystemOverview返回综合概览快照
CPU(ctx)*monitor.CPUInfo返回 CPU 详情与使用率
Memory(ctx)*monitor.MemoryInfo返回虚拟内存与 swap 详情
Disk(ctx)*monitor.DiskInfo返回磁盘分区与 I/O 详情
Network(ctx)*monitor.NetworkInfo返回网络接口与 I/O 详情
Host(ctx)*monitor.HostInfo返回主机静态元数据
Process(ctx)*monitor.ProcessInfo返回当前进程详情
Load(ctx)*monitor.LoadInfo返回系统负载
BuildInfo()*monitor.BuildInfo返回构建元数据

内置资源

monitor 模块会注册:

资源
sys/monitor

当前 action:

Action输入参数输出类型说明
get_overviewmonitor.SystemOverview当部分探针不可用时,overview 仍可能返回部分数据
get_cpumonitor.CPUInfo当 CPU 采样缓存尚未准备好时会返回 monitor not ready
get_memorymonitor.MemoryInfo包含虚拟内存和 swap
get_diskmonitor.DiskInfo包含分区和 I/O 计数
get_networkmonitor.NetworkInfo包含网卡和 I/O 计数
get_hostmonitor.HostInfo返回主机静态信息
get_processmonitor.ProcessInfo当进程采样缓存尚未准备好时会返回 monitor not ready
get_loadmonitor.LoadInfo返回负载均值
get_build_infomonitor.BuildInfo仅返回构建元数据
get_event_streamsmonitor.EventStreamsInfo报告跨进程 event stream 和 consumer group 状态;当没有可用的 event.StreamInspector(redis_stream transport 未启用)时返回 enabled=false 和空的 streams 列表

源码中的实现细节:

  • 每个 action 当前都单独设置了 Max = 60 的限流上限
  • get_cpuget_process 在采样尚未准备好时会返回专门的 monitor-not-ready 业务错误
  • get_event_streams 依赖可选的 event.StreamInspector;inspector 为 nil 时仍返回 200 OK,只是 enabled=false,不会报错

错误 API

API含义
monitor.ErrNotReady / ErrCodeNotReadyCPU 或 process 这类依赖 sample 的数据尚未准备好
monitor.ErrCollectionFailed / ErrCodeCollectionFailed某个 runtime probe 采集数据失败

默认采样配置

当没有显式提供 monitor 配置时,模块默认使用:

配置项默认值
采样间隔10s
采样窗口2s

这些默认值主要影响 CPU 与进程采样行为。

构建信息行为

monitor 模块会对构建信息做装饰,保证 vefVersion 一定存在,即使应用没有提供完整构建元数据对象。

回退行为如下:

字段当应用没有提供构建信息时的回退值
appVersionunknown
buildTimeunknown
gitCommitunknown
vefVersion当前框架版本

数据结构

monitor.SystemOverview

字段类型含义
host*monitor.HostSummary简化主机信息
cpu*monitor.CPUSummary简化 CPU 信息
memory*monitor.MemorySummary简化内存使用情况
disk*monitor.DiskSummary简化磁盘使用情况
network*monitor.NetworkSummary简化网络活动
process*monitor.ProcessSummary简化当前进程指标
load*monitor.LoadInfo负载均值
build*monitor.BuildInfo构建元数据

monitor.HostSummary

字段类型含义
hostnamestring主机名
osstring操作系统
platformstring平台名
platformVersionstring平台版本
kernelVersionstring内核版本
kernelArchstring内核架构
uptimeuint64运行时长(秒)

monitor.HostInfo

字段类型含义
hostnamestring主机名
uptimeuint64运行时长
bootTimeuint64启动时间戳
processesuint64进程数
osstring操作系统
platformstring平台名
platformFamilystring平台族
platformVersionstring平台版本
kernelVersionstring内核版本
kernelArchstring内核架构
virtualizationSystemstring虚拟化系统
virtualizationRolestring虚拟化角色
hostIdstring主机标识

monitor.CPUSummary

字段类型含义
physicalCoresint物理核心数(宿主机拓扑)
logicalCoresint逻辑核心数(宿主机拓扑)
usagePercentfloat64聚合 CPU 使用率,按 effectiveCores 归一化
effectiveCoresfloat64归一化使用率所用的算力容量(v0.38):容器内是 cgroup CPU 配额(支持 v1 与 v2),无法一致采样受限用量时回退为 logicalCores

monitor.CPUInfo

字段类型含义
physicalCoresint物理核心数
logicalCoresint逻辑核心数
modelNamestringCPU 型号
mhzfloat64主频
cacheSizeint32缓存大小
usagePercent[]float64每核心使用率
totalPercentfloat64总使用率,由每核心采样推导
vendorIdstringvendor 标识
familystringCPU family
modelstringCPU model
steppingint32stepping
microcodestringmicrocode 版本
effectiveCoresfloat64归一化使用率所用的算力容量;见 CPUSummary.effectiveCores(v0.38)

monitor.MemorySummary

字段类型含义
totaluint64总内存
useduint64已用内存
usedPercentfloat64使用率

从 v0.38 起监控是容器感知的:当进程运行在实际限制内存的 cgroup(v2 或 v1)下时,头部指标(totalusedusedPercent 以及 VirtualMemory 的可用/空闲)反映 cgroup 限额与 cgroup 自身用量,而不是宿主机全量——64 GiB 宿主机上的 512 MiB 容器按 512 MiB 报告。没有限额时仍报告宿主机数据。

monitor.MemoryInfo

字段类型含义
virtual*monitor.VirtualMemory虚拟/物理内存详情
swap*monitor.SwapMemoryswap 详情

monitor.VirtualMemory

字段类型含义
totaluint64总虚拟内存
availableuint64可用内存
useduint64已用内存
usedPercentfloat64使用率
freeuint64空闲内存
activeuint64活跃内存
inactiveuint64非活跃内存
wireduint64wired 内存
laundryuint64laundry 页数
buffersuint64buffer 内存
cacheduint64缓存内存
writeBackuint64write-back 页数
dirtyuint64dirty 页数
writeBackTmpuint64临时 write-back 页数
shareduint64共享内存
slabuint64slab 内存
slabReclaimableuint64可回收 slab
slabUnreclaimableuint64不可回收 slab
pageTablesuint64页表占用
swapCacheduint64swap 缓存
commitLimituint64commit 上限
committedAsuint64committed 内存
highTotaluint64high memory 总量
highFreeuint64high memory 空闲量
lowTotaluint64low memory 总量
lowFreeuint64low memory 空闲量
swapTotaluint64swap 总量
swapFreeuint64swap 空闲量
mappeduint64mapped 内存
vmAllocTotaluint64VM 分配总量
vmAllocUseduint64VM 已用分配量
vmAllocChunkuint64VM 分配块
hugePagesTotaluint64huge page 总量
hugePagesFreeuint64huge page 空闲量
hugePagesReserveduint64huge page 预留量
hugePagesSurplusuint64huge page surplus
hugePageSizeuint64huge page 大小
anonHugePagesuint64匿名 huge page 数量

monitor.SwapMemory

字段类型含义
totaluint64swap 总量
useduint64已用 swap
freeuint64空闲 swap
usedPercentfloat64swap 使用率
swapInuint64swap-in 次数
swapOutuint64swap-out 次数
pageInuint64page-in 次数
pageOutuint64page-out 次数
pageFaultuint64page fault 数
pageMajorFaultuint64major page fault 数

monitor.DiskSummary

字段类型含义
totaluint64根文件系统总大小
useduint64根文件系统已用大小
usedPercentfloat64根文件系统使用率
partitionsint从 v0.38 起恒为 1

从 v0.38 起,overview 的磁盘摘要报告进程根路径所在的文件系统,不再累加 所有挂载分区——远程挂载、磁盘镜像和并列卷不会再虚增宿主机容量, vef.monitor.excluded_mounts 配置也随累加逻辑一起移除。完整挂载清单仍可 通过 DiskInfo.partitions 获取。

monitor.DiskInfo

字段类型含义
partitions[]*monitor.PartitionInfo分区详情
ioCountersmap[string]*monitor.IOCounter每设备 I/O 计数

monitor.PartitionInfo

字段类型含义
devicestring设备名
mountPointstring挂载点
fsTypestring文件系统类型
options[]string挂载选项
totaluint64总大小
freeuint64空闲大小
useduint64已用大小
usedPercentfloat64使用率
iNodesTotaluint64inode 总量
iNodesUseduint64已用 inode
iNodesFreeuint64空闲 inode
iNodesUsedPercentfloat64inode 使用率

monitor.IOCounter

字段类型含义
readCountuint64读操作次数
mergedReadCountuint64合并读次数
writeCountuint64写操作次数
mergedWriteCountuint64合并写次数
readBytesuint64读取字节数
writeBytesuint64写入字节数
readTimeuint64读耗时
writeTimeuint64写耗时
iopsInProgressuint64正在进行的 I/O 数
ioTimeuint64I/O 总耗时
weightedIouint64加权 I/O 时间
namestring设备名
serialNumberstring设备序列号
labelstring设备标签

monitor.NetworkSummary

字段类型含义
interfacesint网卡数量
bytesSentuint64发送字节总量
bytesRecvuint64接收字节总量
packetsSentuint64发送包总量
packetsRecvuint64接收包总量

monitor.NetworkInfo

字段类型含义
interfaces[]*monitor.InterfaceInfo网卡元数据
ioCountersmap[string]*monitor.NetIOCounter每网卡 I/O 计数

monitor.InterfaceInfo

字段类型含义
indexint接口索引
mtuintMTU
namestring接口名
hardwareAddrstringMAC 地址
flags[]string接口 flags
addrs[]string绑定地址

monitor.NetIOCounter

字段类型含义
namestring接口名
bytesSentuint64发送字节数
bytesRecvuint64接收字节数
packetsSentuint64发送包数
packetsRecvuint64接收包数
errorsInuint64入站错误数
errorsOutuint64出站错误数
droppedInuint64入站丢包数
droppedOutuint64出站丢包数
fifoInuint64入站 FIFO 计数
fifoOutuint64出站 FIFO 计数

monitor.ProcessSummary

字段类型含义
pidint32进程 ID
namestring进程名
cpuPercentfloat64CPU 使用率
memoryPercentfloat32内存使用率

monitor.ProcessInfo

字段类型含义
pidint32进程 ID
parentPidint32父进程 ID
namestring进程名
exestring可执行文件路径
commandLinestring完整命令行
cwdstring当前工作目录
statusstring进程状态
usernamestring所属用户名
createTimeint64创建时间戳
numThreadsint32线程数
numFdsint32打开文件描述符数
cpuPercentfloat64CPU 使用率
memoryPercentfloat32内存使用率
memoryRssuint64RSS 内存
memoryVmsuint64虚拟内存大小
memorySwapuint64swap 使用量

monitor.LoadInfo

字段类型含义
load1float641 分钟负载均值
load5float645 分钟负载均值
load15float6415 分钟负载均值

monitor.BuildInfo

字段类型含义
vefVersionstring框架版本
appVersionstring应用版本
buildTimestring构建时间
gitCommitstringGit 提交号

monitor.EventStreamsInfo

字段类型含义
enabledbool是否有可用的 event.StreamInspector(redis_stream transport 已启用)
streams[]event.StreamInfo每个 transport stream 对应一条记录;enabledfalse 时为空

event.StreamInfo

字段类型含义
streamstring完整 transport 级 stream key(prefix + event type)
lengthint64stream 当前条目数(trim 之后)
groups[]event.StreamGroupInfo挂在该 stream 上的 consumer group

event.StreamGroupInfo

字段类型含义
namestringconsumer group 名称(订阅方 WithGroup 的值,或其派生默认值)
consumersint64该 group 内已注册的 consumer 记录数,包括已重启进程留下的历史 consumer
pendingint64已投递但未 ack 的条目数
lagint64尚未投递给该 group 的 stream 条目数(trim 后为近似值;部分 Redis server 版本不上报 lag 时为 0)
lastDeliveredIdstring该 group 最后一次收到投递的 stream ID

lag 持续增长而 consumer 都处于空闲状态的 group,很可能是一个已下线或改名、却没有清理 consumer group 的订阅者遗留下来的孤儿。transport 层细节见 事件总线

最小请求示例

{
"resource": "sys/monitor",
"action": "get_overview",
"version": "v1"
}

典型用途

  • 运维或后台监控面板
  • 健康检查与诊断界面
  • 内部开发者工具
  • 构建元信息暴露

下一步

继续阅读 CLI 工具,如果你想用 generate-build-info 提供更丰富的构建信息,就会接到那里。