perf

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package perf 提供性能监控:后台定时采样主机、Gately 进程与各站点的指标,保留 24 小时分钟级历史, 并按告警规则(指标 + 阈值 + 持续时长)在条件持续满足时发布性能告警事件,恢复时发布恢复事件, 事件经通知中心送达各渠道。

历史只保存在内存中,重启后清空;告警规则保存在配置里,运行中的计时与触发状态重启后重新开始。

Index

Constants

View Source
const (
	ScopeHost     = "host"
	ScopeProcess  = "process"
	ScopeSite     = "site"
	ScopeUpstream = "upstream"
)

指标对象范围。

View Source
const (
	MetricHostCPU    = "host.cpu"
	MetricHostMem    = "host.mem"
	MetricHostDisk   = "host.disk"
	MetricProcCPU    = "proc.cpu"
	MetricProcRSS    = "proc.rss"
	MetricSiteActive = "site.active"
	MetricSiteQPS    = "site.qps"
	MetricSite5xx    = "site.err5xx"
	MetricSiteP95    = "site.p95"
	MetricUpstreamC  = "upstream.conns"
)

指标标识。

View Source
const (
	UnitPercent = "%"
	UnitMB      = "MB"
	UnitQPS     = "req/s"
	UnitMs      = "ms"
	UnitCount   = ""
)

指标单位。

View Source
const (
	// MaxRules 为告警规则数量上限
	MaxRules = 200
	// MaxRuleSites 为一条规则可指定的站点数量上限
	MaxRuleSites = 200
	// MaxDurationMinutes 为持续时长上限(24 小时)
	MaxDurationMinutes = 1440
	// DefaultMinRequests 为窗口类指标的最小请求数默认值
	DefaultMinRequests = 20
	// MaxMinRequests 为最小请求数上限
	MaxMinRequests = 1000000
)

规则字段的取值范围。

View Source
const (
	OpGT = ">"
	OpGE = ">="
	OpLT = "<"
	OpLE = "<="
)

比较符。

View Source
const ID = "perf"

ID 是模块标识。

View Source
const (
	// SampleInterval 为后台采样与告警评估的间隔
	SampleInterval = 5 * time.Second
)

Variables

View Source
var Metrics = []MetricDef{
	{MetricHostCPU, "主机 CPU 使用率", ScopeHost, UnitPercent, false, 100},
	{MetricHostMem, "主机内存使用率", ScopeHost, UnitPercent, false, 100},
	{MetricHostDisk, "主机磁盘使用率(配置目录所在分区)", ScopeHost, UnitPercent, false, 100},
	{MetricProcCPU, "Gately 进程 CPU 使用率", ScopeProcess, UnitPercent, false, 100},
	{MetricProcRSS, "Gately 进程常驻内存", ScopeProcess, UnitMB, false, 1048576},
	{MetricSiteActive, "站点进行中的请求数", ScopeSite, UnitCount, false, 10000000},
	{MetricSiteQPS, "站点每秒请求数", ScopeSite, UnitQPS, true, 10000000},
	{MetricSite5xx, "站点 5xx 响应比例", ScopeSite, UnitPercent, true, 100},
	{MetricSiteP95, "站点首字节延迟 P95", ScopeSite, UnitMs, true, 3600000},
	{MetricUpstreamC, "后端节点当前连接数", ScopeUpstream, UnitCount, false, 10000000},
}

Metrics 是全部可用指标,顺序即界面展示顺序。

Functions

This section is empty.

Types

type AlertState

type AlertState struct {
	Rule    string `json:"rule"`
	Name    string `json:"name"`
	Subject string `json:"subject"`
	// Target 为告警对象的名称(本机、站点名等)
	Target string `json:"target"`
	// State 为 pending(条件满足、计时中)或 firing(已触发)
	State string `json:"state"`
	// Since 为条件开始满足的时间(Unix 秒),FiredAt 为触发时间,pending 时为 0
	Since   int64   `json:"since"`
	FiredAt int64   `json:"firedAt"`
	Value   float64 `json:"value"`
	Level   string  `json:"level"`
}

AlertState 是一个正在进行的告警实例(条件满足中或已触发)。

type MetricDef

type MetricDef struct {
	ID    string `json:"id"`
	Label string `json:"label"`
	Scope string `json:"scope"`
	Unit  string `json:"unit"`
	// Window 表示该指标按最近约 60 秒的窗口计算,需要窗口内有足够的请求才有结果(见规则的 MinRequests)
	Window bool `json:"window"`
	// Max 为阈值上限,防止填入无意义的巨大数值
	Max float64 `json:"max"`
}

MetricDef 描述一个可用于告警的指标。

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module 是性能监控模块。

func New

func New(web *webservice.Module) *Module

New 创建模块,web 为 Web 服务模块,用于取得各站点的性能快照;可为 nil(此时只监控主机与进程)。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) Init

func (m *Module) Init(env module.Env) error

Init 实现 module.Module。

func (*Module) Metrics

func (m *Module) Metrics() []module.Sample

Metrics 实现 module.Metricer:导出最近一次采样的主机与进程指标,以及各规则当前触发的告警数。

func (*Module) Routes

func (m *Module) Routes(r module.Router)

Routes 实现 module.Module。

func (*Module) Start

func (m *Module) Start(ctx context.Context) error

Start 实现 module.Module:启动后台采样与告警评估。

func (*Module) Stop

func (m *Module) Stop() error

Stop 实现 module.Module。

func (*Module) Summary

func (m *Module) Summary() []module.SummaryItem

Summary 实现 module.Summarizer:总览页展示告警规则数与触发中的告警数。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type Overview

type Overview struct {
	// Range 为 60s、1h 或 24h;Step 为相邻两点的间隔秒数(5 秒、60 秒或 300 秒)
	Range  string  `json:"range"`
	Step   int     `json:"step"`
	Series []Point `json:"series"`
	// Latest 为最近一次采样,尚未采样时为 null
	Latest *Point `json:"latest"`
}

Overview 是主机与进程历史接口的输出。

type Point

type Point struct {
	Time       int64    `json:"t"`
	CPU        *float64 `json:"cpu"`
	CPUMax     *float64 `json:"cpuMax"`
	Mem        *float64 `json:"mem"`
	Disk       *float64 `json:"disk"`
	Load1      *float64 `json:"load1"`
	ProcCPU    *float64 `json:"procCpu"`
	ProcCPUMax *float64 `json:"procCpuMax"`
	// ProcRSS 为常驻内存(MB)
	ProcRSS    *float64 `json:"procRss"`
	ProcFDs    *float64 `json:"procFds"`
	Goroutines *float64 `json:"goroutines"`
	// ChildCPU、ChildCPUMax、ChildRSS 为全部托管进程的 CPU 与常驻内存(MB)合计,没有托管进程时为 null
	ChildCPU    *float64 `json:"childCpu"`
	ChildCPUMax *float64 `json:"childCpuMax"`
	ChildRSS    *float64 `json:"childRss"`
}

Point 是主机与进程历史中的一个点,没有数据的字段为 null。

type ProcSample added in v0.6.5

type ProcSample struct {
	Time int64    `json:"t"`
	CPU  *float64 `json:"cpu"`
	// RSS 为常驻内存(MB)
	RSS *float64 `json:"rss"`
}

ProcSample 是托管进程的一次采样,取不到的值为 null。

type ProcessView added in v0.6.5

type ProcessView struct {
	proc.Managed
	// CPU 为按整机核数归一化的百分比,RSS 为常驻内存(字节)
	CPU     *float64     `json:"cpu"`
	RSS     uint64       `json:"rss"`
	Threads int          `json:"threads"`
	Samples []ProcSample `json:"samples"`
}

ProcessView 是一个托管进程的当前资源占用与最近约 60 秒的采样。

type Rule

type Rule struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	// Group 为列表中的分组名称,只影响管理界面的展示
	Group  string `json:"group"`
	Enable bool   `json:"enable"`
	Metric string `json:"metric"`
	Op     string `json:"op"`
	// Threshold 的单位由指标决定
	Threshold float64 `json:"threshold"`
	// Duration 为条件需要持续满足的分钟数,中途回落则计时清零
	Duration int `json:"duration"`
	// Sites 为站点与后端类指标生效的站点(站点标识,见 /api/perf/sites),留空为全部站点;对主机与进程指标无意义,保存时清空
	Sites []string `json:"sites"`
	// MinRequests 为窗口类指标(QPS 之外的 5xx 比例、延迟)的最小请求数:窗口内请求不足时不判定(视为条件不满足),避免低流量时误报
	MinRequests int `json:"minRequests"`
	// Level 为告警级别 warn / error,留空为 warn
	Level string `json:"level"`
	// Remark 为备注
	Remark string `json:"remark"`
}

Rule 是一条告警规则:指标满足 “比较符 阈值” 的状态持续 Duration 分钟后触发告警,条件解除后发恢复通知。

func (*Rule) Validate

func (r *Rule) Validate() error

Validate 校验并规范化规则。

type SiteView

type SiteView struct {
	Key    string `json:"key"`
	Name   string `json:"name"`
	Active int64  `json:"active"`
	// QPS、Err5xx、P95 按最近约 60 秒计算:每秒请求数、5xx 占已完成响应的百分比、首字节延迟 P95(毫秒)
	QPS    *float64 `json:"qps"`
	Err5xx *float64 `json:"err5xx"`
	P95    *float64 `json:"p95"`
	// Requests 为窗口内已完成的响应数,Latencies 为窗口内计入延迟统计的请求数
	Requests  uint64         `json:"requests"`
	Latencies uint64         `json:"latencies"`
	Upstreams []UpstreamView `json:"upstreams,omitempty"`
}

SiteView 是一个站点最近一次采样后的指标,窗口类指标数据不足时为 nil。

type Sources

type Sources struct {
	// Host 采集主机与进程信息
	Host func() sysstat.Snapshot
	// Sites 返回各运行中站点的性能快照,可为 nil
	Sites func() []webservice.PerfSite
}

Sources 是采样的数据来源,测试中替换为假数据。

type UpstreamView

type UpstreamView struct {
	Key     string `json:"key"`
	URL     string `json:"url"`
	Route   string `json:"route,omitempty"`
	Healthy bool   `json:"healthy"`
	Conns   int64  `json:"conns"`
}

UpstreamView 是后端节点的瞬时状态。

Jump to

Keyboard shortcuts

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