2020-08-27 09:37:32 +02:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
"github.com/shirou/gopsutil/mem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Max number of metrics we want to keep.
|
|
|
|
const maxCollectionValues = 500
|
|
|
|
|
|
|
|
func collectCPUUtilization() {
|
|
|
|
if len(Metrics.CPUUtilizations) > maxCollectionValues {
|
|
|
|
Metrics.CPUUtilizations = Metrics.CPUUtilizations[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := cpu.Percent(0, false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:06:14 +02:00
|
|
|
metricValue := timestampedValue{time.Now(), int(v[0])}
|
2020-08-27 09:37:32 +02:00
|
|
|
Metrics.CPUUtilizations = append(Metrics.CPUUtilizations, metricValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectRAMUtilization() {
|
|
|
|
if len(Metrics.RAMUtilizations) > maxCollectionValues {
|
|
|
|
Metrics.RAMUtilizations = Metrics.RAMUtilizations[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
memoryUsage, _ := mem.VirtualMemory()
|
2020-10-02 09:06:14 +02:00
|
|
|
metricValue := timestampedValue{time.Now(), int(memoryUsage.UsedPercent)}
|
2020-08-27 09:37:32 +02:00
|
|
|
Metrics.RAMUtilizations = append(Metrics.RAMUtilizations, metricValue)
|
|
|
|
}
|