stat

package
v1.15.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 9 Imported by: 0

README

stat

Statistics on system and process cpu and memory information, alarm notification and custom handler support.


Example of use

    import "github.com/go-dev-frame/sponge/pkg/stat"

    l, _ := zap.NewDevelopment()
    stat.Init(
        stat.WithLog(l),
        stat.WithPrintInterval(time.Minute),
        stat.WithPrintField(logger.String("service_name", cfg.App.Name), logger.String("host", cfg.App.Host)), // add custom fields to log
        stat.WithEnableAlarm(stat.WithCPUThreshold(0.85), stat.WithMemoryThreshold(0.85)), // enable alarm and trigger collect profile data, invalid if it is windows
        //stat.WithCustomHandler(func(ctx context.Context, sd *stat.StatData) error { // it will be replace default print handler
        //    //push stat data to remote server (prometheus, influxdb, etc.) or do something else
        //    return nil
        //}),
    )

Documentation

Overview

Package stat provides for counting system and process cpu and memory information, alarm notification support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(opts ...Option)

Init initialize statistical information

Types

type AlarmOption

type AlarmOption func(*alarmOptions)

AlarmOption set the alarm options field.

func WithCPUThreshold

func WithCPUThreshold(threshold float64) AlarmOption

WithCPUThreshold set cpu threshold, range 0 to 1

func WithMemoryThreshold

func WithMemoryThreshold(threshold float64) AlarmOption

WithMemoryThreshold set memory threshold, range 0 to 1

type Option

type Option func(*options)

Option set the stat options field.

func WithAlarm

func WithAlarm(opts ...AlarmOption) Option

WithAlarm enable alarm and notify, except windows

func WithCustomHandler added in v1.14.5

func WithCustomHandler(handler func(ctx context.Context, sd *StatData) error) Option

WithCustomHandler set custom handler and interval, will replace default print stat data handler

func WithLog

func WithLog(l *zap.Logger) Option

WithLog set zapLog

func WithPrintField

func WithPrintField(fields ...zap.Field) Option

WithPrintField set print field

func WithPrintInterval

func WithPrintInterval(d time.Duration) Option

WithPrintInterval set print interval

type Process added in v1.14.5

type Process struct {
	CPUUsage   float64 `json:"cpu_usage"`   // process cpu usage, unit(%)
	RSS        uint64  `json:"rss"`         // use of physical memory, unit(M)
	VMS        uint64  `json:"vms"`         // use of virtual memory, unit(M)
	Alloc      uint64  `json:"alloc"`       // allocated memory capacity, unit(M)
	TotalAlloc uint64  `json:"total_alloc"` // cumulative allocated memory capacity, unit(M)
	Sys        uint64  `json:"sys"`         // requesting memory capacity from the system, unit(M)
	NumGc      uint32  `json:"num_gc"`      // number of GC cycles
	Goroutines int     `json:"goroutines"`  // number of goroutines
}

Process information

type StatData added in v1.14.5

type StatData struct {
	Sys  System
	Proc Process
}

StatData statistical data

type System added in v1.14.5

type System struct {
	CPUUsage float64 `json:"cpu_usage"` // system cpu usage, unit(%)
	MemUsage float64 `json:"mem_usage"` // system memory usage, unit(%)
	CPUCores int32   `json:"cpu_cores"` // cpu cores, multiple cpu accumulation
	MemTotal uint64  `json:"mem_total"` // system total physical memory, unit(M)
	MemFree  uint64  `json:"mem_free"`  // system free physical memory, unit(M)
}

System information

Directories

Path Synopsis
Package cpu is a library that counts system and process cpu usage.
Package cpu is a library that counts system and process cpu usage.
Package mem is a library that counts system and process memory usage.
Package mem is a library that counts system and process memory usage.

Jump to

Keyboard shortcuts

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