Skip to content

Commit

Permalink
middleware:prometheus系统采样修改为覆盖记录的方式且新增采样间隔配置
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Dec 25, 2024
1 parent 138a6a2 commit 6776e34
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
9 changes: 5 additions & 4 deletions examples/pkg/app/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ func StartServer(wg *sync.WaitGroup) {
JsonPath: "examples/pkg/app/user/http/docs/swagger.json",
},
Prometheus: config.PrometheusConf{
Enable: true,
Addr: ":19100",
AccessPath: "/metrics",
DiskPath: "/var",
Enable: true,
Addr: ":19100",
AccessPath: "/metrics",
DiskPath: "/var",
SampleInterval: "10s",
},
WebSocketRoutePath: "go-sail-ws",
},
Expand Down
56 changes: 34 additions & 22 deletions http/middleware/prometheusexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ var (
metricsSummaryGaugeVecNetworkTransfer *prometheus.GaugeVec
)

var diskPath = "/"
var (
diskPath = "/"
sampleInterval = time.Minute
)

// SetDiskPath 设置磁盘监控路径
func SetDiskPath(path string) {
Expand All @@ -73,6 +76,16 @@ func SetDiskPath(path string) {
}
}

// SetSampleInterval 设置采样间隔(频率)
func SetSampleInterval(interval string) {
td, err := time.ParseDuration(interval)
//若小于1ms,则使用默认值1分钟
if err != nil || td.Milliseconds() == 0 {
return
}
sampleInterval = td
}

func init() {
once.Do(func() {
svl := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Expand All @@ -97,25 +110,25 @@ func init() {
cpuMetrics := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "system_cpu_usage",
Help: "[cpu] cpu usage (percent)",
}, []string{"durations", "core"})
}, []string{"core"})
metricsSummaryGaugeVecCPUUsage = cpuMetrics

memMetrics := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "system_memory_usage",
Help: "[memory] memory usage bytes",
}, []string{"durations", "type"})
}, []string{"type"})
metricsSummaryGaugeVecMemoryUsage = memMetrics

diskMetrics := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "system_disk_usage",
Help: "[disk] disk usage bytes",
}, []string{"durations", "device", "type"})
}, []string{"device", "type"})
metricsSummaryGaugeVecDiskUsage = diskMetrics

networkMetrics := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "system_network_transfer",
Help: "[network] network transfer bytes",
}, []string{"durations", "name", "type"})
}, []string{"name", "type"})
metricsSummaryGaugeVecNetworkTransfer = networkMetrics

prometheus.MustRegister(cpuMetrics, memMetrics, diskMetrics, networkMetrics)
Expand All @@ -124,7 +137,7 @@ func init() {

// SystemMetricsSample 系统指标采样
//
// 采样周期为每分钟一次
// 采样周期为每分钟一次,覆盖上一次记录值
//
// 采集对象:
//
Expand All @@ -138,49 +151,48 @@ func init() {
//
// - 网卡,多网卡 -> 出入站数值
func SystemMetricsSample() {
ticker := time.NewTicker(time.Minute)
ticker := time.NewTicker(sampleInterval)
for range ticker.C {
point := time.Now().Format("2006-01-02 15:04")
//cpu使用率
cpuUsage, err := cpu.Percent(time.Minute, true)
cpuUsage, err := cpu.Percent(sampleInterval, true)
if err == nil && len(cpuUsage) > 0 {
for index, cu := range cpuUsage {
metricsSummaryGaugeVecCPUUsage.WithLabelValues(point, fmt.Sprintf("%d", index)).Set(cu)
metricsSummaryGaugeVecCPUUsage.WithLabelValues(fmt.Sprintf("%d", index)).Set(cu)
}
}
//内存使用率
memStat, err := mem.VirtualMemory()
if err == nil && memStat != nil {
metricsSummaryGaugeVecMemoryUsage.WithLabelValues(point, "used").Set(float64(memStat.Used))
metricsSummaryGaugeVecMemoryUsage.WithLabelValues(point, "percent").Set(memStat.UsedPercent)
metricsSummaryGaugeVecMemoryUsage.WithLabelValues("used").Set(float64(memStat.Used))
metricsSummaryGaugeVecMemoryUsage.WithLabelValues("percent").Set(memStat.UsedPercent)
}
//硬盘使用率
diskStat, err := disk.Usage(diskPath)
if err == nil && diskStat != nil {
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, diskPath, "used").Set(float64(diskStat.Used))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, diskPath, "percent").Set(diskStat.UsedPercent)
metricsSummaryGaugeVecDiskUsage.WithLabelValues(diskPath, "used").Set(float64(diskStat.Used))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(diskPath, "percent").Set(diskStat.UsedPercent)
}
//硬盘io
diskIoStat, err := disk.IOCounters()
if err == nil && diskIoStat != nil {
for device, stat := range diskIoStat {
//读
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "readBytes").Set(float64(stat.ReadBytes))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "readTime").Set(float64(stat.ReadTime))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "readCount").Set(float64(stat.ReadCount))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "readBytes").Set(float64(stat.ReadBytes))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "readTime").Set(float64(stat.ReadTime))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "readCount").Set(float64(stat.ReadCount))

//写
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "writeBytes").Set(float64(stat.WriteBytes))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "writeTime").Set(float64(stat.WriteTime))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(point, device, "writeCount").Set(float64(stat.WriteCount))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "writeBytes").Set(float64(stat.WriteBytes))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "writeTime").Set(float64(stat.WriteTime))
metricsSummaryGaugeVecDiskUsage.WithLabelValues(device, "writeCount").Set(float64(stat.WriteCount))
}
}
//网卡使用率
netStats, err := net.IOCounters(false)
if err == nil && len(netStats) > 0 {
for _, netStat := range netStats {
metricsSummaryGaugeVecNetworkTransfer.WithLabelValues(point, netStat.Name, "bytesReceived").Set(float64(netStat.BytesRecv))
metricsSummaryGaugeVecNetworkTransfer.WithLabelValues(point, netStat.Name, "bytesSent").Set(float64(netStat.BytesSent))
metricsSummaryGaugeVecNetworkTransfer.WithLabelValues(netStat.Name, "bytesReceived").Set(float64(netStat.BytesRecv))
metricsSummaryGaugeVecNetworkTransfer.WithLabelValues(netStat.Name, "bytesSent").Set(float64(netStat.BytesSent))
}
}
}
Expand Down
1 change: 1 addition & 0 deletions sail/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type PrometheusConf struct {
AccessPath string `yaml:"access_path" toml:"access_path" json:"access_path" default:"/metrics"` //路由地址
DisableSystemSample bool `yaml:"disable_system_sample" toml:"disable_system_sample" json:"disable_system_sample" default:"false"` //禁止系统采样(默认会采样)
DiskPath string `yaml:"disk_path" toml:"disk_path" json:"disk_path" default:"/"` //检测硬盘使用率指定的监控路径
SampleInterval string `yaml:"sample_interval" toml:"sample_interval" json:"sample_interval" default:"1m"` //采样间隔(默认1分钟,最小1ms),格式:一分钟=1m,30秒=30s,500毫秒=500ms
}

type KafkaExtraConf struct {
Expand Down
1 change: 1 addition & 0 deletions sail/httpserver/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func InitGinEngine(conf config.HttpServerConf) *gin.Engine {
r.Use(middleware.PrometheusExporter())
if !conf.Prometheus.DisableSystemSample {
middleware.SetDiskPath(conf.Prometheus.DiskPath)
middleware.SetSampleInterval(conf.Prometheus.SampleInterval)
go middleware.SystemMetricsSample()
}
}
Expand Down

0 comments on commit 6776e34

Please sign in to comment.