Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultConfig() config.MonitorConfig
- func NewResource(service monitor.Service) api.Resource
- func NewService(cfg *config.MonitorConfig, buildInfo *monitor.BuildInfo) monitor.Service
- type DefaultService
- func (s *DefaultService) BuildInfo() *monitor.BuildInfo
- func (s *DefaultService) Close() error
- func (s *DefaultService) Cpu(_ context.Context) (*monitor.CpuInfo, error)
- func (s *DefaultService) Disk(ctx context.Context) (*monitor.DiskInfo, error)
- func (s *DefaultService) Host(ctx context.Context) (*monitor.HostInfo, error)
- func (s *DefaultService) Init(context.Context) error
- func (s *DefaultService) Load(ctx context.Context) (*monitor.LoadInfo, error)
- func (s *DefaultService) Memory(ctx context.Context) (*monitor.MemoryInfo, error)
- func (s *DefaultService) Network(ctx context.Context) (*monitor.NetworkInfo, error)
- func (s *DefaultService) Overview(ctx context.Context) (*monitor.SystemOverview, error)
- func (s *DefaultService) Process(_ context.Context) (*monitor.ProcessInfo, error)
- type Resource
- func (r *Resource) GetBuildInfo(ctx fiber.Ctx) error
- func (r *Resource) GetCpu(ctx fiber.Ctx) error
- func (r *Resource) GetDisk(ctx fiber.Ctx) error
- func (r *Resource) GetHost(ctx fiber.Ctx) error
- func (r *Resource) GetLoad(ctx fiber.Ctx) error
- func (r *Resource) GetMemory(ctx fiber.Ctx) error
- func (r *Resource) GetNetwork(ctx fiber.Ctx) error
- func (r *Resource) GetOverview(ctx fiber.Ctx) error
- func (r *Resource) GetProcess(ctx fiber.Ctx) error
Constants ¶
const ( // DefaultSampleInterval is the default interval between CPU and process sampling. // Sample every 10 seconds provides a good balance between accuracy and overhead. DefaultSampleInterval = 10 * time.Second // DefaultSampleDuration is the default sampling window duration for CPU and process metrics. // A 2-second window smooths short-term fluctuations while providing responsive metrics. DefaultSampleDuration = 2 * time.Second )
Variables ¶
var ( // ErrCpuInfoNotReady indicates CPU information is not yet available from background sampling. ErrCpuInfoNotReady = errors.New("cpu info not ready") // ErrProcessInfoNotReady indicates process information is not yet available from background sampling. ErrProcessInfoNotReady = errors.New("process info not ready") )
var Module = fx.Module( "vef:monitor", fx.Decorate(func(cfg *config.MonitorConfig) *config.MonitorConfig { cfgToUse := DefaultConfig() if cfg != nil { if cfg.SampleInterval > 0 { cfgToUse.SampleInterval = cfg.SampleInterval } if cfg.SampleDuration > 0 { cfgToUse.SampleDuration = cfg.SampleDuration } } return &cfgToUse }), fx.Decorate( fx.Annotate( func(buildInfo *monitor.BuildInfo) *monitor.BuildInfo { if buildInfo != nil { buildInfo.VEFVersion = constants.VEFVersion } else { buildInfo = &monitor.BuildInfo{ VEFVersion: constants.VEFVersion, AppVersion: "v0.0.0", BuildTime: "2022-08-08 01:00:00", GitCommit: "-", } } return buildInfo }, fx.ParamTags(`optional:"true"`), ), ), fx.Provide( fx.Annotate( NewService, fx.ParamTags(``, `optional:"true"`), fx.OnStart(func(ctx context.Context, svc monitor.Service) error { if initializer, ok := svc.(contract.Initializer); ok { if err := initializer.Init(ctx); err != nil { return fmt.Errorf("failed to initialize monitor service: %w", err) } } return nil }), fx.OnStop(func(svc monitor.Service) error { if closer, ok := svc.(io.Closer); ok { if err := closer.Close(); err != nil { return fmt.Errorf("failed to close monitor service: %w", err) } } return nil }), ), fx.Annotate( NewResource, fx.ResultTags(`group:"vef:api:resources"`), ), ), )
Module is the FX module for system monitoring functionality.
Functions ¶
func DefaultConfig ¶
func DefaultConfig() config.MonitorConfig
DefaultConfig returns the default monitor configuration. This configuration provides reasonable defaults for most use cases: - 10 second sampling interval (20% duty cycle with 2s window) - 2 second sampling window (smooths CPU spikes).
func NewResource ¶
NewResource creates a new monitor resource with the provided service.
func NewService ¶
NewService creates a new monitor.Service implementation.
Types ¶
type DefaultService ¶
type DefaultService struct {
// contains filtered or unexported fields
}
func (*DefaultService) BuildInfo ¶
func (s *DefaultService) BuildInfo() *monitor.BuildInfo
BuildInfo returns application build information if available. Returns default "unknown" values if no build info was provided during service creation.
func (*DefaultService) Close ¶
func (s *DefaultService) Close() error
Close gracefully stops the background sampling goroutines.
func (*DefaultService) Cpu ¶
Cpu returns detailed cpu information including usage percentages. This method returns cached data from background sampling, ensuring fast response.
func (*DefaultService) Init ¶
func (s *DefaultService) Init(context.Context) error
Init starts background goroutines to periodically sample cpu and process metrics.
func (*DefaultService) Memory ¶
func (s *DefaultService) Memory(ctx context.Context) (*monitor.MemoryInfo, error)
Memory returns memory usage information.
func (*DefaultService) Network ¶
func (s *DefaultService) Network(ctx context.Context) (*monitor.NetworkInfo, error)
Network returns network interface and I/O statistics.
func (*DefaultService) Overview ¶
func (s *DefaultService) Overview(ctx context.Context) (*monitor.SystemOverview, error)
Overview returns a comprehensive system overview by fetching all metrics.
func (*DefaultService) Process ¶
func (s *DefaultService) Process(_ context.Context) (*monitor.ProcessInfo, error)
Process returns information about the current process. This method returns cached data from background sampling, ensuring fast response.
type Resource ¶
Resource handles system monitoring-related API endpoints.
func (*Resource) GetBuildInfo ¶
GetBuildInfo returns application build information.
func (*Resource) GetNetwork ¶
GetNetwork returns network interface and I/O statistics.
func (*Resource) GetOverview ¶
GetOverview returns a comprehensive system overview.