monitor

package
v3.1.34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 9 Imported by: 0

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 DecConns

func DecConns()

DecConns 减少连接计数

func IncConns

func IncConns()

IncConns 增加连接计数(供 gateway/websocket 调用)

func IncRequests

func IncRequests()

IncRequests 增加请求计数(供中间件调用)

func New

func New(conf ...Config) core.HandlerFunc

New 启动后台监控采集(周期性更新 Stats)。

func PrometheusHandler

func PrometheusHandler() core.HandlerFunc

PrometheusHandler 返回 Prometheus 格式的 /metrics handler。

func StatsHandler

func StatsHandler() core.HandlerFunc

StatsHandler 返回 JSON 格式的系统状态 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 服务器运行时统计

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL