跳到主要内容

监控

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

模块输出

监控模块会提供:

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

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

monitor.Service 接口

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

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

内置资源

monitor 模块注册 sys/monitor RPC 资源,挂载在 /api 下,使用标准请求 envelope(resourceactionversionparamsmeta)。没有任何 操作是公开的,也没有声明专门的权限点:每个 action 都继承 API 引擎默认的 Bearer 认证。

每个 action 都单独设置了 Max: 60 的限流上限。窗口长度未覆写,因此继承 vef.api.rate_limit.period(默认 5m);限流按「操作 + 客户端 IP + principal」计数,每个节点在进程内存中独立执行。

这些 action 都没有定义框架级入参:params 会被忽略,可以完全省略。

Action访问限流入参出参
get_overviewBearer 认证Max: 60monitor.SystemOverview
get_cpuBearer 认证Max: 60monitor.CPUInfo
get_memoryBearer 认证Max: 60monitor.MemoryInfo
get_diskBearer 认证Max: 60monitor.DiskInfo
get_networkBearer 认证Max: 60monitor.NetworkInfo
get_hostBearer 认证Max: 60monitor.HostInfo
get_processBearer 认证Max: 60monitor.ProcessInfo
get_loadBearer 认证Max: 60monitor.LoadInfo
get_build_infoBearer 认证Max: 60monitor.BuildInfo
get_event_streamsBearer 认证Max: 60monitor.EventStreamsInfo
get_integration_statsBearer 认证Max: 60monitor.IntegrationStatsInfo

源码中可见的行为语义:

  • get_overview 是尽力而为的,整体从不失败:某个子探针出错时只记日志, 对应的 overview 字段留为 null,单个损坏的采集器不会掩盖其余数据。
  • get_cpuget_process 从后台采样缓存读取,在第一次采样落地前返回 monitor-not-ready 业务错误(monitor.ErrNotReady)。
  • get_memoryget_diskget_networkget_hostget_load 实时读取 探针;探针失败映射为 monitor.ErrCollectionFailed
  • get_build_info 不会失败:service 始终持有非 nil 的构建信息对象(见 构建信息行为)。
  • get_event_streams 依赖可选的 event.StreamInspector。inspector 为 nil (redis_stream transport 未启用)时仍返回 200 OK,只是 enabled: falsestreams 为空列表,不会报错;inspector 读取出错则映射为 monitor.ErrCollectionFailed
  • get_integration_stats 对可选的 integration.StatsInspector(集成模块 未启用时为 nil)采用相同的降级模式:enabled: falsestats 为空 列表。读取进程内快照本身不会失败。
  • 业务错误使用标准 result envelope:HTTP 状态保持 200,失败通过 body 的 code 传递。

错误 API

API含义
monitor.ErrNotReady / ErrCodeNotReady2100CPU 或 process 这类依赖采样的数据尚未准备好
monitor.ErrCollectionFailed / ErrCodeCollectionFailed2101某个 runtime 探针采集数据失败

默认采样配置

默认值按未设置字段生效(部分配置只会覆盖它设置的字段):

配置项默认值
vef.monitor.sample_interval10s
vef.monitor.sample_duration2s

这些配置驱动 get_cpuget_process 背后的后台采样器:启动时立即采样 一次,之后每个采样间隔采样一次,每次采样在一个采样窗口内测量使用率。在 第一次采样完成前(大约是启动后的第一个窗口),这两个 action 都会返回 monitor.ErrNotReady

构建信息行为

service 构造函数会对构建信息做归一化,保证 vefVersion 一定存在,即使应用没有提供完整构建元数据对象。

回退行为如下:

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

按 Action 划分的响应结构

以下字段名即 JSON wire 名称(Go struct 的 json tag)。字节量都是纯字节 数(无单位换算),百分比范围 0100,计数器除特别说明外都是自启动以来 的累计值。平台不提供的字段报告为 0 或空。

get_overviewmonitor.SystemOverview

由全部探针组装的综合快照。某个探针失败时对应字段为 nullbuild 始终存在。

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

monitor.HostSummary

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

monitor.CPUSummary

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

monitor.MemorySummary

字段类型说明
totaluint64总内存(字节)
useduint64已用内存(字节)
usedPercentfloat64内存使用率

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

monitor.DiskSummary

字段类型说明
totaluint64根文件系统总大小(字节)
useduint64根文件系统已用大小(字节)
usedPercentfloat64根文件系统使用率
partitionsint恒为 1(摘要只覆盖单个文件系统)

overview 的磁盘摘要报告进程根路径所在的文件系统,而不是累加 所有挂载分区——远程挂载、磁盘镜像和并列卷不会虚增宿主机容量,也不存在 vef.monitor.excluded_mounts 配置(本就不做累加,无需排除)。完整挂载 清单仍可通过 DiskInfo.partitions 获取。

monitor.NetworkSummary

字段类型说明
interfacesint网卡数量
bytesSentuint64发送字节总量(跨网卡累加)
bytesRecvuint64接收字节总量(跨网卡累加)
packetsSentuint64发送包总量(跨网卡累加)
packetsRecvuint64接收包总量(跨网卡累加)

monitor.ProcessSummary

字段类型说明
pidint32进程 ID
namestring进程名
cpuPercentfloat64最近一个采样窗口的进程 CPU 使用率;以单核为基准,多核机器上可以超过 100
memoryPercentfloat32进程占宿主机总内存的百分比

get_cpumonitor.CPUInfo

从后台采样缓存读取:每个采样间隔(默认 10s)刷新一次,每次刷新在一个 采样窗口(默认 2s)内测量。清单类字段(modelNamevendorIdfamilymodelsteppingmicrocodemhzcacheSize)描述第一 颗 CPU。

字段类型说明
physicalCoresint物理核心数(宿主机拓扑)
logicalCoresint逻辑核心数(宿主机拓扑)
modelNamestringCPU 型号
mhzfloat64标称主频(MHz)
cacheSizeint32缓存大小(KB)
usagePercent[]float64采样窗口内的每核心使用率,每个逻辑核心一项;在 CPU 受限的容器内为 null(cgroup 测量取代每核心采样)
totalPercentfloat64聚合使用率:每核心采样的均值;在 CPU 受限的容器内则是窗口内消耗的 cgroup 算力份额,上限 100
vendorIdstringvendor 标识
familystringCPU family
modelstringCPU model
steppingint32stepping
microcodestringmicrocode 版本
effectiveCoresfloat64归一化使用率所用的算力容量;见 CPUSummary.effectiveCores

get_memorymonitor.MemoryInfo

每次调用实时读取。MemorySummary 描述的容器感知头部指标行为同样适用于 virtual

字段类型说明
virtual*monitor.VirtualMemory虚拟/物理内存详情
swap*monitor.SwapMemoryswap 详情;swap 探针失败时为 null

monitor.VirtualMemory

usedPercent(百分比)和 huge page 计数器外,所有字段都是字节量: hugePagesTotalhugePagesFreehugePagesReservedhugePagesSurplus 是页数,hugePageSizeanonHugePages 是字节。即使在内存受限的容器 内,明细字段仍保持宿主机含义。

字段类型说明
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

totalusedfree 是字节。swapInswapOutpageInpageOut 是由内核页计数换算的累计字节量;pageFaultpageMajorFault 是累计事件 次数。

字段类型说明
totaluint64swap 总量
useduint64已用 swap
freeuint64空闲 swap
usedPercentfloat64swap 使用率
swapInuint64swap 换入量
swapOutuint64swap 换出量
pageInuint64page 换入量
pageOutuint64page 换出量
pageFaultuint64page fault 数
pageMajorFaultuint64major page fault 数

get_diskmonitor.DiskInfo

每次调用实时读取。用量探针失败的分区会被跳过、不出现在 partitions 中;I/O 计数探针失败时 ioCountersnull

字段类型说明
partitions[]*monitor.PartitionInfo每个挂载点的分区详情
ioCountersmap[string]*monitor.IOCounter每设备 I/O 计数,key 为设备名

monitor.PartitionInfo

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

monitor.IOCounter

计数器为自启动以来的累计值;readTimewriteTimeioTimeweightedIo 单位为毫秒。

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

get_networkmonitor.NetworkInfo

每次调用实时读取。

字段类型说明
interfaces[]*monitor.InterfaceInfo网卡元数据
ioCountersmap[string]*monitor.NetIOCounter每网卡 I/O 计数,key 为网卡名

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 计数

get_hostmonitor.HostInfo

主机静态元数据,每次调用实时读取。

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

get_processmonitor.ProcessInfo

描述应用自身进程。与 get_cpu 相同节奏,从后台采样缓存读取。

字段类型说明
pidint32进程 ID
parentPidint32父进程 ID
namestring进程名
exestring可执行文件路径
commandLinestring完整命令行
cwdstring当前工作目录
statusstring进程状态
usernamestring所属用户名
createTimeint64进程创建时间,自 Unix 纪元以来的毫秒数(UTC)
numThreadsint32线程数
numFdsint32打开文件描述符数
cpuPercentfloat64采样窗口内的进程 CPU 使用率;以单核为基准,多核机器上可以超过 100
memoryPercentfloat32进程占宿主机总内存的百分比
memoryRssuint64常驻内存 RSS(字节)
memoryVmsuint64虚拟内存大小(字节)
memorySwapuint64swap 使用量(字节)

get_loadmonitor.LoadInfo

每次调用实时读取。

字段类型说明
load1float641 分钟负载均值
load5float645 分钟负载均值
load15float6415 分钟负载均值

get_build_infomonitor.BuildInfo

仅返回构建元数据;回退值见构建信息行为

字段类型说明
vefVersionstring框架版本,模块始终会盖章写入
appVersionstring应用版本
buildTimestring构建时间
gitCommitstringGit 提交号

get_event_streamsmonitor.EventStreamsInfo

通过可选的 event.StreamInspector(由 redis_stream transport 提供)报告 跨进程 event stream 与 consumer group 状态。

字段类型说明
enabledbool是否有可用的 event.StreamInspector(redis_stream transport 已启用);false 表示这是一次空降级返回,不是错误
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 层细节见 事件总线

get_integration_statsmonitor.IntegrationStatsInfo

通过可选的 integration.StatsInspector 报告本节点的集成调用统计。 数字是进程启动以来的内存计数——持久记录以调用日志为准。

字段类型说明
enabledbool是否有可用的 integration.StatsInspector(集成模块已启用);false 表示这是一次空降级返回,不是错误
stats[]integration.InvocationStats进程启动以来观察到的每个(系统、契约、方向)组合一条记录,按系统、契约、方向排序;enabledfalse 时为空

integration.InvocationStats

字段类型说明
systemstring服务(或拒绝)该调用的系统 code
contractstring被调用的契约 code;被验证拒绝的入站投递聚合在空 contract 下——拒绝发生时契约 code 还是未经校验的调用方输入
directionstringoutboundinbound
callsint64观察到的调用总数
successesint64成功完成的调用数
failuresmap[string]int64失败分类input_invalidoutput_invalidupstreamtransporttimeoutcanceledscriptconfigauthhandler)计数的失败数;为空时省略
avgDurationMsint64平均调用耗时(毫秒)
maxDurationMsint64最大调用耗时(毫秒)
lastErrorstring最近一次失败的信息;从未失败时省略
lastErrorAt时间戳最近一次失败发生的时间;从未失败时省略

这些计数如何被记录,见集成引擎

最小请求示例

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

典型用途

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

下一步

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