Documentation
¶
Index ¶
- Variables
- type CPUProfileSession
- type CPUProfiler
- type ConfigProvider
- type GoroutineProfileSession
- type GoroutineProfiler
- type HTTPContext
- type HeapProfileSession
- type HeapProfiler
- type Logger
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) GetStats() ProfilingStats
- func (m *Manager) GetTasks() map[string]ProfilingTask
- func (m *Manager) IsEnabled() bool
- func (m *Manager) RegisterProfiler(profiler Profiler)
- func (m *Manager) ShouldProfile(path, method string) (ProfilingTask, bool)
- func (m *Manager) StartProfiling(ctx context.Context, path string, task ProfilingTask) (ProfileSession, error)
- func (m *Manager) StopProfiling(ctx context.Context, path, method string, task ProfilingTask, ...) (*ProfilingResult, error)
- type Options
- type PathMatcher
- type ProfileSession
- type Profiler
- type ProfilingResult
- type ProfilingStats
- type ProfilingTask
- type Storage
Constants ¶
This section is empty.
Variables ¶
var CommonMethods = []string{"GET", "POST", "PUT", "DELETE"}
CommonMethods 定义了"*"通配符支持的方法
Functions ¶
This section is empty.
Types ¶
type CPUProfileSession ¶
type CPUProfileSession struct {
// contains filtered or unexported fields
}
CPUProfileSession represents a CPU profiling session
func (*CPUProfileSession) GetStartTime ¶
func (s *CPUProfileSession) GetStartTime() time.Time
GetStartTime returns when the session started
func (*CPUProfileSession) IsRunning ¶
func (s *CPUProfileSession) IsRunning() bool
IsRunning returns true if the session is still active
func (*CPUProfileSession) Stop ¶
func (s *CPUProfileSession) Stop() ([]byte, error)
Stop stops the profiling session
type CPUProfiler ¶
type CPUProfiler struct{}
CPUProfiler implements CPU profiling
func (*CPUProfiler) GetProfileType ¶
func (c *CPUProfiler) GetProfileType() string
GetProfileType returns the profiling type
func (*CPUProfiler) StartProfiling ¶
func (c *CPUProfiler) StartProfiling(ctx context.Context, task ProfilingTask) (ProfileSession, error)
StartProfiling starts CPU profiling
type ConfigProvider ¶
type ConfigProvider interface {
// GetTasks 返回当前性能分析任务
GetTasks(ctx context.Context) ([]ProfilingTask, error)
// Subscribe 订阅配置变更
Subscribe(ctx context.Context, callback func([]ProfilingTask)) error
// Close 关闭提供程序并释放资源
Close() error
}
ConfigProvider 抽象性能分析任务的配置源
type GoroutineProfileSession ¶
type GoroutineProfileSession struct {
// contains filtered or unexported fields
}
GoroutineProfileSession represents a goroutine profiling session
func (*GoroutineProfileSession) GetStartTime ¶
func (s *GoroutineProfileSession) GetStartTime() time.Time
GetStartTime returns when the session started
func (*GoroutineProfileSession) IsRunning ¶
func (s *GoroutineProfileSession) IsRunning() bool
IsRunning returns true if the session is still active
func (*GoroutineProfileSession) Stop ¶
func (s *GoroutineProfileSession) Stop() ([]byte, error)
Stop stops the profiling session and captures goroutine profile
type GoroutineProfiler ¶
type GoroutineProfiler struct{}
GoroutineProfiler implements goroutine profiling
func (*GoroutineProfiler) GetProfileType ¶
func (g *GoroutineProfiler) GetProfileType() string
GetProfileType returns the profiling type
func (*GoroutineProfiler) StartProfiling ¶
func (g *GoroutineProfiler) StartProfiling(ctx context.Context, task ProfilingTask) (ProfileSession, error)
StartProfiling starts goroutine profiling
type HTTPContext ¶
type HTTPContext interface {
// GetPath 返回路由模板(例如:"/users/:id")
GetPath() string
// GetMethod 返回HTTP方法
GetMethod() string
// GetHeaders 返回请求头
GetHeaders() map[string]string
// SetContext 在上下文中设置键值对
SetContext(key, value interface{})
// GetContext 通过键从上下文获取值
GetContext(key interface{}) interface{}
// GetRequestPath 返回实际请求路径
GetRequestPath() string
}
HTTPContext 为不同框架抽象HTTP请求上下文
type HeapProfileSession ¶
type HeapProfileSession struct {
// contains filtered or unexported fields
}
HeapProfileSession represents a heap profiling session
func (*HeapProfileSession) GetStartTime ¶
func (s *HeapProfileSession) GetStartTime() time.Time
GetStartTime returns when the session started
func (*HeapProfileSession) IsRunning ¶
func (s *HeapProfileSession) IsRunning() bool
IsRunning returns true if the session is still active
func (*HeapProfileSession) Stop ¶
func (s *HeapProfileSession) Stop() ([]byte, error)
Stop stops the profiling session and captures heap profile
type HeapProfiler ¶
type HeapProfiler struct{}
HeapProfiler implements heap/memory profiling
func (*HeapProfiler) GetProfileType ¶
func (h *HeapProfiler) GetProfileType() string
GetProfileType returns the profiling type
func (*HeapProfiler) StartProfiling ¶
func (h *HeapProfiler) StartProfiling(ctx context.Context, task ProfilingTask) (ProfileSession, error)
StartProfiling starts heap profiling
type Logger ¶
type Logger interface {
// Info 记录信息消息
Info(msg string, fields map[string]interface{})
// Warn 记录警告消息
Warn(msg string, fields map[string]interface{})
// Error 记录错误消息
Error(msg string, fields map[string]interface{})
// Debug 记录调试消息
Debug(msg string, fields map[string]interface{})
}
Logger 抽象日志功能
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 管理性能分析会话和任务
func NewManager ¶
func NewManager(opts Options, configProvider ConfigProvider, storage Storage, logger Logger, pathMatcher PathMatcher) *Manager
NewManager 创建新的性能分析管理器
func (*Manager) RegisterProfiler ¶
RegisterProfiler 为特定类型注册分析器
func (*Manager) ShouldProfile ¶
func (m *Manager) ShouldProfile(path, method string) (ProfilingTask, bool)
ShouldProfile 检查是否应该对请求进行性能分析
func (*Manager) StartProfiling ¶
func (m *Manager) StartProfiling(ctx context.Context, path string, task ProfilingTask) (ProfileSession, error)
StartProfiling 开始性能分析会话
func (*Manager) StopProfiling ¶
func (m *Manager) StopProfiling(ctx context.Context, path, method string, task ProfilingTask, session ProfileSession) (*ProfilingResult, error)
StopProfiling 停止性能分析会话并保存结果
type Options ¶
type Options struct {
// MaxConcurrent is the maximum number of concurrent profiling sessions
MaxConcurrent int `yaml:"max_concurrent" json:"max_concurrent"`
// DefaultDuration is the default profiling duration
DefaultDuration time.Duration `yaml:"default_duration" json:"default_duration"`
// CleanupInterval is the interval for cleaning up old profiles
CleanupInterval time.Duration `yaml:"cleanup_interval" json:"cleanup_interval"`
// MaxFileAge is the maximum age for profile files before cleanup
MaxFileAge time.Duration `yaml:"max_file_age" json:"max_file_age"`
// Enabled controls whether profiling is enabled
Enabled bool `yaml:"enabled" json:"enabled"`
// ProfileDir is the directory to store profile files (for file storage)
ProfileDir string `yaml:"profile_dir" json:"profile_dir"`
// DefaultSampleRate is the default sample rate for profiling
DefaultSampleRate int `yaml:"default_sample_rate" json:"default_sample_rate"`
}
Options represents configuration options for the profiling manager
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns default configuration options
type PathMatcher ¶
type PathMatcher interface {
// Match 检查实际路径是否匹配模板
Match(template, actual string) bool
// ExtractParams 使用模板从实际路径提取参数
ExtractParams(template, actual string) map[string]string
}
PathMatcher 抽象路径匹配逻辑
type ProfileSession ¶
type ProfileSession interface {
// Stop 停止性能分析会话并返回结果
Stop() ([]byte, error)
// GetStartTime 返回会话开始时间
GetStartTime() time.Time
// IsRunning 如果会话仍处于活跃状态则返回true
IsRunning() bool
}
ProfileSession 表示活跃的性能分析会话
func NewCPUProfileSession ¶
func NewCPUProfileSession(ctx context.Context, task ProfilingTask) (ProfileSession, error)
NewCPUProfileSession creates a new CPU profiling session
func NewGoroutineProfileSession ¶
func NewGoroutineProfileSession(ctx context.Context, task ProfilingTask) (ProfileSession, error)
NewGoroutineProfileSession creates a new goroutine profiling session
func NewHeapProfileSession ¶
func NewHeapProfileSession(ctx context.Context, task ProfilingTask) (ProfileSession, error)
NewHeapProfileSession creates a new heap profiling session
type Profiler ¶
type Profiler interface {
// StartProfiling 使用给定配置开始性能分析
StartProfiling(ctx context.Context, task ProfilingTask) (ProfileSession, error)
// GetProfileType 返回此分析器处理的性能分析类型
GetProfileType() string
}
Profiler 抽象不同类型的性能分析
func NewGoroutineProfiler ¶
func NewGoroutineProfiler() Profiler
NewGoroutineProfiler creates a new goroutine profiler
type ProfilingResult ¶
type ProfilingResult struct {
Path string `json:"path"` // 路径
StartTime time.Time `json:"start_time"` // 开始时间
Duration time.Duration `json:"duration"` // 持续时间
Filename string `json:"filename"` // 文件名
FileSize int64 `json:"file_size"` // 文件大小
ProfileType string `json:"profile_type"` // 分析类型
Success bool `json:"success"` // 是否成功
Error string `json:"error,omitempty"` // 错误信息
}
ProfilingResult 表示性能分析会话的结果
type ProfilingStats ¶
type ProfilingStats struct {
TotalRequests int64 `json:"total_requests"` // 总请求数
ProfiledCount int64 `json:"profiled_count"` // 已分析数量
FailedCount int64 `json:"failed_count"` // 失败数量
ActiveProfiles int64 `json:"active_profiles"` // 活跃分析数
LastUpdate time.Time `json:"last_update"` // 最后更新时间
}
ProfilingStats 表示性能分析统计信息
type ProfilingTask ¶
type ProfilingTask struct {
Path string `yaml:"path" json:"path"` // 路径
Methods []string `yaml:"methods" json:"methods"` // HTTP方法数组,支持多个方法或使用"*"表示常用方法
ExpiresAt time.Time `yaml:"expires_at" json:"expires_at"` // 过期时间
Duration int `yaml:"duration" json:"duration"` // 最大分析持续时间(秒)
SampleRate int `yaml:"sample_rate" json:"sample_rate"` // 每N个请求进行采样
ProfileType string `yaml:"profile_type" json:"profile_type"` // cpu, heap, goroutine等
}
ProfilingTask 表示性能分析任务配置
func (*ProfilingTask) GetEffectiveMethods ¶
func (task *ProfilingTask) GetEffectiveMethods() []string
GetEffectiveMethods 返回此任务将匹配的所有方法
func (*ProfilingTask) ShouldMatchMethod ¶
func (task *ProfilingTask) ShouldMatchMethod(requestMethod string) bool
ShouldMatchMethod 检查请求方法是否匹配任务配置
type Storage ¶
type Storage interface {
// Save 将性能分析数据保存到存储
Save(ctx context.Context, filename string, data []byte) error
// List 列出匹配给定模式的文件
List(ctx context.Context, pattern string) ([]string, error)
// Delete 从存储中删除文件
Delete(ctx context.Context, filename string) error
// Clean 删除超过maxAge的文件
Clean(ctx context.Context, maxAge time.Duration) error
}
Storage 抽象性能分析文件的存储后端