System Resource Statistics
5/14/25Less than 1 minutecomponentcpumemory
Overview
stat is a component that collects usage statistics for system and service resources (CPU, memory).
Configuration
In services created by sponge, the resource statistics component is enabled by default.
Set the field enableStat in the yaml configuration file under the configs directory:
app:
enableStat: true # Whether resource statistics is enabled, true: enable, false: disableBy default, statistics are collected once per minute and output to the log. Resource statistics include CPU and memory related data for both the system and the service itself.
Examples of Use
import (
"github.com/go-dev-frame/sponge/pkg/stat"
"github.com/go-dev-frame/sponge/pkg/logger"
)
stat.Init(
stat.WithLog(logger.Get()),
stat.WithPrintField(logger.String("service_name", cfg.App.Name), logger.String("host", cfg.App.Host)), // add custom fields to log
stat.WithPrintInterval(time.Minute), // print statistics once per minute
stat.WithEnableAlarm(stat.WithCPUThreshold(0.9), stat.WithMemoryThreshold(0.85)), // Trigger notifications when CPU or memory usage exceeds thresholds, system signals may not work for Windows systems
)