monitor

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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")
)
View Source
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

func NewResource(service monitor.Service) api.Resource

NewResource creates a new monitor resource with the provided service.

func NewService

func NewService(
	cfg *config.MonitorConfig,
	buildInfo *monitor.BuildInfo,
) monitor.Service

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) Disk

Disk returns disk usage and partition information.

func (*DefaultService) Host

Host returns host information.

func (*DefaultService) Init

Init starts background goroutines to periodically sample cpu and process metrics.

func (*DefaultService) Load

Load returns system load averages.

func (*DefaultService) Memory

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

Overview returns a comprehensive system overview by fetching all metrics.

func (*DefaultService) Process

Process returns information about the current process. This method returns cached data from background sampling, ensuring fast response.

type Resource

type Resource struct {
	api.Resource
	// contains filtered or unexported fields
}

Resource handles system monitoring-related API endpoints.

func (*Resource) GetBuildInfo

func (r *Resource) GetBuildInfo(ctx fiber.Ctx) error

GetBuildInfo returns application build information.

func (*Resource) GetCpu

func (r *Resource) GetCpu(ctx fiber.Ctx) error

GetCpu returns detailed cpu information.

func (*Resource) GetDisk

func (r *Resource) GetDisk(ctx fiber.Ctx) error

GetDisk returns disk usage and partition information.

func (*Resource) GetHost

func (r *Resource) GetHost(ctx fiber.Ctx) error

GetHost returns static host information.

func (*Resource) GetLoad

func (r *Resource) GetLoad(ctx fiber.Ctx) error

GetLoad returns system load averages.

func (*Resource) GetMemory

func (r *Resource) GetMemory(ctx fiber.Ctx) error

GetMemory returns memory usage information.

func (*Resource) GetNetwork

func (r *Resource) GetNetwork(ctx fiber.Ctx) error

GetNetwork returns network interface and I/O statistics.

func (*Resource) GetOverview

func (r *Resource) GetOverview(ctx fiber.Ctx) error

GetOverview returns a comprehensive system overview.

func (*Resource) GetProcess

func (r *Resource) GetProcess(ctx fiber.Ctx) error

GetProcess returns information about the current process.

Jump to

Keyboard shortcuts

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