Documentation
¶
Overview ¶
Package monitor 提供服务器运行时监控插件,暴露 CPU、内存、文件句柄、连接数等指标。
使用方式:
// 方式 1: 仅暴露 Prometheus /metrics 端点
app.Get("/metrics", monitor.PrometheusHandler())
// 方式 2: 启动后台周期性采集 + JSON endpoint
app.Use(monitor.New(monitor.Config{Interval: 5 * time.Second}))
app.Get("/sys/stats", monitor.StatsHandler())
零开销 — 后台 goroutine 每 Interval 采集一次,不影响请求处理。
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultConfig = Config{ Interval: 5 * time.Second, }
Functions ¶
func PrometheusHandler ¶
func PrometheusHandler() core.HandlerFunc
PrometheusHandler 返回 Prometheus 格式的 /metrics handler。
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector 运行时数据采集器
type Config ¶
type Config struct {
// Interval 采集间隔,默认 5s
Interval time.Duration
// Skip 可选跳过函数
Skip func(c core.Ctx) bool
}
Config 监控配置
type Stats ¶
type Stats struct {
// CPU 相关
NumGoroutine int `json:"num_goroutine"`
NumCPU int `json:"num_cpu"`
GoMaxProcs int `json:"gomaxprocs"`
NumCgoCall int64 `json:"num_cgo_call"`
CPUPercent float64 `json:"cpu_percent"` // 近似值
// 内存相关 (bytes)
Alloc uint64 `json:"alloc"`
TotalAlloc uint64 `json:"total_alloc"`
Sys uint64 `json:"sys"`
HeapAlloc uint64 `json:"heap_alloc"`
HeapSys uint64 `json:"heap_sys"`
HeapInuse uint64 `json:"heap_inuse"`
HeapIdle uint64 `json:"heap_idle"`
StackInuse uint64 `json:"stack_inuse"`
// GC 相关
NumGC uint32 `json:"num_gc"`
GCPauseMs float64 `json:"gc_pause_ms"`
// 系统资源
OpenFDs int64 `json:"open_fds"` // 当前进程打开的文件描述符数
MaxFDs int64 `json:"max_fds"` // 系统允许的最大文件描述符
NumConns int64 `json:"num_conns"` // 框架追踪的网络连接数
NumRequests int64 `json:"num_requests"` // 累计请求数(通过中间件计数)
// contains filtered or unexported fields
}
Stats 服务器运行时统计
Click to show internal directories.
Click to hide internal directories.