Documentation
¶
Overview ¶
Package actuator 提供应用监控端点 Starter,用于 enhance 框架。
该模块提供多种运维监控端点,包括健康检查、指标收集、环境信息等。 支持多种 HTTP 框架集成,如标准库 http、Gin、Hertz 等。
架构设计 ¶
- Actuator: 运维端点管理器
- health.Aggregator: 健康检查聚合器
- metrics.MeterRegistry: 指标注册表
- Sanitizer: 敏感信息检测器
支持的端点 ¶
- /health: 健康检查
- /info: 应用信息
- /metrics: 指标暴露
- /env: 环境配置查看
- /beans: Bean 列表
- /admin: 管理端点
使用方式 ¶
在 main.go 中引入:
import _ "github.com/xudefa/enhance/actuator"
配置属性 ¶
- actuator.enabled: 是否启用监控端点(默认 true)
- actuator.path: 端点路径前缀(默认 /actuator)
- actuator.health.enabled: 是否启用健康检查(默认 true)
- actuator.metrics.enabled: 是否启用指标收集(默认 true)
配置示例 ¶
环境变量:
export ACTUATOR_ENABLED=true export ACTUATOR_PATH=/actuator
配置文件(application.json):
{
"actuator": {
"enabled": true,
"path": "/actuator",
"health": {
"enabled": true
},
"metrics": {
"enabled": true
}
}
}
Index ¶
- Constants
- func EnsureLeadingSlash(path string) string
- func JoinPath(base, path string) string
- func NewDatabaseHealthIndicator(checkFunc func(context.Context) error) health.Indicator
- func NewRedisHealthIndicator(checkFunc func(context.Context) error) health.Indicator
- type Actuator
- func (a *Actuator) BeansHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) EnvHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) HealthHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) InfoHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) MetricsHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) MetricsRegistry() metrics.MeterRegistry
- func (a *Actuator) PprofHandlers() map[string]http.HandlerFunc
- func (a *Actuator) PrometheusHandler(w http.ResponseWriter, r *http.Request)
- func (a *Actuator) RegisterDebugRoutes(registrar RouteRegistrar)
- func (a *Actuator) RegisterRoutes(registrar RouteRegistrar, config RouteConfig)
- func (a *Actuator) SetHealthAggregator(agg *health.Aggregator)
- func (a *Actuator) SetMetricsRegistry(reg metrics.MeterRegistry)
- type ActuatorAutoConfiguration
- type ActuatorHttpStarter
- func (s *ActuatorHttpStarter) Configure(ctx boot.ApplicationContext) error
- func (s *ActuatorHttpStarter) Dependencies() []string
- func (s *ActuatorHttpStarter) GetCondition() condition.Condition
- func (s *ActuatorHttpStarter) Name() string
- func (s *ActuatorHttpStarter) Start(ctx boot.ApplicationContext) error
- func (s *ActuatorHttpStarter) Stop(ctx boot.ApplicationContext) error
- type AppContext
- type DiskSpaceHealthIndicator
- type EndpointConfig
- type HttpEndpointRegistry
- type HttpEndpointRegistryAdapter
- type HttpHandlerRegistry
- type MemoryHealthIndicator
- type PathNormalizer
- type ProcessHealthIndicator
- type RouteConfig
- type RouteRegistrar
- type SanitizeStrategy
- type Sanitizer
- type StdHttpHandlerRegistry
- type StdRouteRegistrar
Constants ¶
const ( // 应用配置 AppName = "app.name" AppVersion = "app.version" // Actuator 配置 ActuatorEnabled = "actuator.enabled" )
const ( // 应用默认值 DefaultAppName = "enhance-app" DefaultAppVersion = "1.0.0" // 条件值常量 ConditionTrue = "true" )
Variables ¶
This section is empty.
Functions ¶
func NewDatabaseHealthIndicator ¶
NewDatabaseHealthIndicator 创建数据库健康指示器
Types ¶
type Actuator ¶
type Actuator struct {
// contains filtered or unexported fields
}
Actuator 运维端点管理器
提供多种运维端点,包括健康检查、指标收集、环境信息、Bean 列表等。 支持多种 HTTP 框架集成,如标准库 http、Gin、Hertz 等。
func (*Actuator) BeansHandler ¶
func (a *Actuator) BeansHandler(w http.ResponseWriter, r *http.Request)
BeansHandler Bean 列表 HTTP 处理器
func (*Actuator) EnvHandler ¶
func (a *Actuator) EnvHandler(w http.ResponseWriter, r *http.Request)
EnvHandler 环境信息 HTTP 处理器
func (*Actuator) HealthHandler ¶
func (a *Actuator) HealthHandler(w http.ResponseWriter, r *http.Request)
HealthHandler 健康检查 HTTP 处理器
返回聚合后的健康状态信息,包含所有健康指标的详细状态。 响应格式:
{
"status": "UP",
"details": {
"database": {
"status": "UP",
"detail": {}
}
},
"timestamp": "2024-01-01T00:00:00Z"
}
func (*Actuator) InfoHandler ¶
func (a *Actuator) InfoHandler(w http.ResponseWriter, r *http.Request)
InfoHandler 应用信息 HTTP 处理器
func (*Actuator) MetricsHandler ¶
func (a *Actuator) MetricsHandler(w http.ResponseWriter, r *http.Request)
MetricsHandler 指标 HTTP 处理器
func (*Actuator) MetricsRegistry ¶
func (a *Actuator) MetricsRegistry() metrics.MeterRegistry
MetricsRegistry 获取指标注册表
func (*Actuator) PprofHandlers ¶
func (a *Actuator) PprofHandlers() map[string]http.HandlerFunc
PprofHandlers returns handlers for pprof endpoints
func (*Actuator) PrometheusHandler ¶
func (a *Actuator) PrometheusHandler(w http.ResponseWriter, r *http.Request)
PrometheusHandler Prometheus 指标 HTTP 处理器
func (*Actuator) RegisterDebugRoutes ¶
func (a *Actuator) RegisterDebugRoutes(registrar RouteRegistrar)
RegisterDebugRoutes 注册调试路由
func (*Actuator) RegisterRoutes ¶
func (a *Actuator) RegisterRoutes(registrar RouteRegistrar, config RouteConfig)
RegisterRoutes 注册 Actuator 路由
使用 RouteRegistrar 接口解耦路由注册逻辑, 支持不同的 HTTP 框架实现。
func (*Actuator) SetHealthAggregator ¶
func (a *Actuator) SetHealthAggregator(agg *health.Aggregator)
SetHealthAggregator 设置健康检查聚合器
func (*Actuator) SetMetricsRegistry ¶
func (a *Actuator) SetMetricsRegistry(reg metrics.MeterRegistry)
SetMetricsRegistry 设置指标注册表
type ActuatorAutoConfiguration ¶
type ActuatorAutoConfiguration struct{}
ActuatorAutoConfiguration Actuator 自动配置
func (*ActuatorAutoConfiguration) Configure ¶
func (a *ActuatorAutoConfiguration) Configure(ctx boot.ApplicationContext) error
Configure 创建 Actuator 实例并注册为 Bean
type ActuatorHttpStarter ¶
type ActuatorHttpStarter struct {
// contains filtered or unexported fields
}
ActuatorHttpStarter Actuator HTTP 启动器
负责将 Actuator 端点自动挂载到现有的 HTTP 服务器上。 通过 HttpEndpointRegistry 接口实现框架无关的端点注册, 支持任意 HTTP 框架(Gin、Fiber、Echo、Chi 等)。
挂载策略(按优先级): 1. HttpEndpointRegistry 接口(推荐,框架无关) 2. HttpHandlerRegistry 接口(简化版) 3. RouteRegistrar 接口(向后兼容) 4. 独立 HTTP 服务器(降级方案)
通过配置项控制各端点的暴露:
- actuator.expose.health: 健康检查端点(默认 true)
- actuator.expose.metrics: 指标端点(默认 true)
- actuator.expose.env: 环境信息端点(默认 true)
- actuator.expose.beans: Bean 列表端点(默认 true)
- actuator.expose.info: 应用信息端点(默认 true)
- actuator.expose.prometheus: Prometheus 端点(默认 true)
func (*ActuatorHttpStarter) Configure ¶
func (s *ActuatorHttpStarter) Configure(ctx boot.ApplicationContext) error
Configure 配置阶段:从容器中获取 Actuator 实例
func (*ActuatorHttpStarter) Dependencies ¶
func (s *ActuatorHttpStarter) Dependencies() []string
Dependencies 返回依赖的其他启动器名称
func (*ActuatorHttpStarter) GetCondition ¶
func (s *ActuatorHttpStarter) GetCondition() condition.Condition
GetCondition 返回启动条件
func (*ActuatorHttpStarter) Start ¶
func (s *ActuatorHttpStarter) Start(ctx boot.ApplicationContext) error
Start 启动阶段:将 Actuator 端点挂载到 HTTP 服务器
func (*ActuatorHttpStarter) Stop ¶
func (s *ActuatorHttpStarter) Stop(ctx boot.ApplicationContext) error
Stop 停止阶段:无需特殊处理
type AppContext ¶
type AppContext interface {
Container() core.Container
Environment() *environment.Environment
}
AppContext 应用上下文接口。
type DiskSpaceHealthIndicator ¶
type DiskSpaceHealthIndicator struct {
// contains filtered or unexported fields
}
DiskSpaceHealthIndicator 磁盘空间健康指标
检查磁盘使用率是否超过阈值,当使用率过高时返回降级状态。
func NewDiskSpaceHealthIndicator ¶
func NewDiskSpaceHealthIndicator(path string, threshold float64) *DiskSpaceHealthIndicator
NewDiskSpaceHealthIndicator 创建磁盘空间健康指标
参数:
- path: 检查的磁盘路径
- threshold: 使用率阈值(0.0-1.0),超过此比例返回降级状态
func (*DiskSpaceHealthIndicator) Health ¶
func (d *DiskSpaceHealthIndicator) Health(ctx context.Context) health.Health
Health 执行磁盘空间健康检查
func (*DiskSpaceHealthIndicator) Name ¶
func (d *DiskSpaceHealthIndicator) Name() string
Name 返回健康指标名称
type EndpointConfig ¶
type EndpointConfig struct {
// Method HTTP 方法,空字符串表示所有方法
Method string
// Path 路由路径
Path string
// Handler HTTP 处理器
Handler http.Handler
// Description 端点描述(可选,用于日志和文档)
Description string
}
EndpointConfig 端点配置
type HttpEndpointRegistry ¶
type HttpEndpointRegistry interface {
// RegisterEndpoint 注册单个端点
// method: HTTP 方法(GET, POST 等),空字符串表示所有方法
// path: 路由路径
// handler: HTTP 处理器
RegisterEndpoint(method, path string, handler http.Handler)
// RegisterEndpoints 批量注册端点
// endpoints: 端点配置列表
RegisterEndpoints(endpoints []EndpointConfig)
// HasEndpoint 检查是否已注册指定路径的端点
HasEndpoint(path string) bool
}
HttpEndpointRegistry HTTP 端点注册表接口
该接口作为 Web 框架和 Actuator 之间的桥梁,允许 Actuator 将端点 挂载到任意 HTTP 框架,而无需关心框架的具体实现细节。
Web 框架(如 Gin、Fiber、默认 Router 等)应在启动时向容器注册 此接口的实现,Actuator 通过查找此接口来自动挂载端点。
使用示例(Gin 框架):
registry := &GinEndpointRegistry{engine: ginEngine}
ctx.Container().RegisterInstance(registry, reflect.TypeFor[actuator.HttpEndpointRegistry]())
使用示例(Fiber 框架):
registry := &FiberEndpointRegistry{app: fiberApp}
ctx.Container().RegisterInstance(registry, reflect.TypeFor[actuator.HttpEndpointRegistry]())
type HttpEndpointRegistryAdapter ¶
type HttpEndpointRegistryAdapter struct {
// contains filtered or unexported fields
}
HttpEndpointRegistryAdapter HttpEndpointRegistry 的基础实现
该适配器将 HttpEndpointRegistry 接口委托给底层的 HttpHandlerRegistry, 为不同 HTTP 框架提供统一的注册方式。
框架集成者可以实现 HttpHandlerRegistry 接口,然后使用此适配器 快速获得 HttpEndpointRegistry 的完整功能。
func NewHttpEndpointRegistryAdapter ¶
func NewHttpEndpointRegistryAdapter(registry HttpHandlerRegistry) *HttpEndpointRegistryAdapter
NewHttpEndpointRegistryAdapter 创建 HttpEndpointRegistry 适配器
func (*HttpEndpointRegistryAdapter) HasEndpoint ¶
func (a *HttpEndpointRegistryAdapter) HasEndpoint(path string) bool
HasEndpoint 检查是否已注册指定路径的端点
func (*HttpEndpointRegistryAdapter) RegisterEndpoint ¶
func (a *HttpEndpointRegistryAdapter) RegisterEndpoint(method, path string, handler http.Handler)
RegisterEndpoint 注册单个端点
func (*HttpEndpointRegistryAdapter) RegisterEndpoints ¶
func (a *HttpEndpointRegistryAdapter) RegisterEndpoints(endpoints []EndpointConfig)
RegisterEndpoints 批量注册端点
type HttpHandlerRegistry ¶
type HttpHandlerRegistry interface {
// Handle 注册路由处理器
// pattern: 路由模式,如 "/actuator/health"
// handler: HTTP 处理器
Handle(pattern string, handler http.Handler)
}
HttpHandlerRegistry HTTP Handler 注册表
这是 HttpEndpointRegistry 的简化版本,仅支持注册 http.Handler。 适用于只需要基本路由注册功能的场景。
type MemoryHealthIndicator ¶
type MemoryHealthIndicator struct {
// contains filtered or unexported fields
}
MemoryHealthIndicator 内存使用健康指标
检查内存使用情况,当使用率过高时返回降级状态。
func NewMemoryHealthIndicator ¶
func NewMemoryHealthIndicator(threshold float64) *MemoryHealthIndicator
NewMemoryHealthIndicator 创建内存健康指标
参数:
- threshold: 堆内存使用率阈值(0.0-1.0),超过此比例返回降级状态
type PathNormalizer ¶
type PathNormalizer struct{}
PathNormalizer 路径标准化工具
func (PathNormalizer) NormalizePath ¶
func (PathNormalizer) NormalizePath(path string) string
NormalizePath 标准化路径,确保路径格式正确
type ProcessHealthIndicator ¶
type ProcessHealthIndicator struct {
// contains filtered or unexported fields
}
ProcessHealthIndicator 进程健康指标
检查进程状态信息,如goroutine数量等。
func NewProcessHealthIndicator ¶
func NewProcessHealthIndicator(goroutineThreshold int) *ProcessHealthIndicator
NewProcessHealthIndicator 创建进程健康指标
参数:
- goroutineThreshold: goroutine 数量阈值,超过此值返回降级状态
type RouteConfig ¶
RouteConfig 路由配置
type RouteRegistrar ¶
RouteRegistrar 路由注册器接口。
type SanitizeStrategy ¶
SanitizeStrategy 敏感信息检测策略接口。
type Sanitizer ¶
type Sanitizer struct {
// contains filtered or unexported fields
}
Sanitizer 敏感信息检测器
使用策略模式管理多种敏感信息检测规则, 支持自定义检测策略。 使用 atomic.Value 优化高频读取场景的性能。
func (*Sanitizer) AddStrategy ¶
func (s *Sanitizer) AddStrategy(strategy SanitizeStrategy)
AddStrategy 添加自定义检测策略
type StdHttpHandlerRegistry ¶
type StdHttpHandlerRegistry struct {
Mux interface {
Handle(pattern string, handler http.Handler)
}
}
StdHttpHandlerRegistry 标准库 http.Handler 注册表实现
该实现包装 http.ServeMux 或其他实现了 Handle 方法的类型, 提供 HttpHandlerRegistry 接口的功能。
type StdRouteRegistrar ¶
StdRouteRegistrar 标准库 HTTP 路由注册器
该实现包装 http.ServeMux,提供 RouteRegistrar 接口的功能。