If you want to know how your Go application is using cpu, memory and goroutine you can use runtime component. It is always good to know how the system resources are used so that you can either scale up or down your application. It also helps debugging potential bugs in the application. However, I would suggest you not to use this feature in production because it will obviously use system resources as well. If you have to, you are better off shipping the data to some kind of message queue so that it is not a blocking operation. Also, you could store and visualise the data for investigation.


Example


package main

import (
"time"

"internal/monitor"
)

func main() {
go monitor.System()

// Simulate never ending client requests.
for {
// Pretend like each request task (goroutine) takes 10 seconds.
go func() {
time.Sleep(10 * time.Second)
}()

// Wait 1 second before creating a new request.
time.Sleep(time.Second)
}
}

package monitor

import (
"log"
"runtime"
"time"
)

// Print system resource usage every 2 seconds.
func System() {
mem := &runtime.MemStats{}

for {
cpu := runtime.NumCPU()
log.Println("CPU:", cpu)

rot := runtime.NumGoroutine()
log.Println("Goroutine:", rot)

// Byte
runtime.ReadMemStats(mem)
log.Println("Memory:", mem.Alloc)

time.Sleep(2 * time.Second)
log.Println("-------")
}
}

Test


2020/04/25 21:32:44 CPU: 4
2020/04/25 21:32:44 Goroutine: 3
2020/04/25 21:32:44 Memory: 87096
2020/04/25 21:32:46 -------
2020/04/25 21:32:46 CPU: 4
2020/04/25 21:32:46 Goroutine: 4
2020/04/25 21:32:46 Memory: 88272
2020/04/25 21:32:48 -------
2020/04/25 21:32:48 CPU: 4
2020/04/25 21:32:48 Goroutine: 6
2020/04/25 21:32:48 Memory: 91688
2020/04/25 21:32:50 -------
2020/04/25 21:32:50 CPU: 4
2020/04/25 21:32:50 Goroutine: 8
2020/04/25 21:32:50 Memory: 92712
2020/04/25 21:32:52 -------
2020/04/25 21:32:52 CPU: 4
2020/04/25 21:32:52 Goroutine: 10
2020/04/25 21:32:52 Memory: 93944
2020/04/25 21:32:54 -------
2020/04/25 21:32:54 CPU: 4
2020/04/25 21:32:54 Goroutine: 11
2020/04/25 21:32:54 Memory: 94984
2020/04/25 21:32:56 -------
2020/04/25 21:32:56 CPU: 4
2020/04/25 21:32:56 Goroutine: 11
2020/04/25 21:32:56 Memory: 95736
2020/04/25 21:32:58 -------
2020/04/25 21:32:58 CPU: 4
2020/04/25 21:32:58 Goroutine: 11
2020/04/25 21:32:58 Memory: 96344
2020/04/25 21:33:00 -------
2020/04/25 21:33:00 CPU: 4
2020/04/25 21:33:00 Goroutine: 11
2020/04/25 21:33:00 Memory: 96552
2020/04/25 21:33:02 -------
2020/04/25 21:33:02 CPU: 4
2020/04/25 21:33:02 Goroutine: 11
2020/04/25 21:33:02 Memory: 96760
2020/04/25 21:33:04 -------
2020/04/25 21:33:04 CPU: 4
2020/04/25 21:33:04 Goroutine: 11
2020/04/25 21:33:04 Memory: 96968
2020/04/25 21:33:06 -------
2020/04/25 21:33:06 CPU: 4
2020/04/25 21:33:06 Goroutine: 12
2020/04/25 21:33:06 Memory: 97176
2020/04/25 21:33:08 -------
2020/04/25 21:33:08 CPU: 4
2020/04/25 21:33:08 Goroutine: 12
2020/04/25 21:33:08 Memory: 97384
2020/04/25 21:33:10 -------
2020/04/25 21:33:10 CPU: 4
2020/04/25 21:33:10 Goroutine: 12
2020/04/25 21:33:10 Memory: 97592
2020/04/25 21:33:12 -------
2020/04/25 21:33:12 CPU: 4
2020/04/25 21:33:12 Goroutine: 12
2020/04/25 21:33:12 Memory: 97800
2020/04/25 21:33:14 -------
2020/04/25 21:33:14 CPU: 4
2020/04/25 21:33:14 Goroutine: 12
2020/04/25 21:33:14 Memory: 98776
2020/04/25 21:33:16 -------
2020/04/25 21:33:16 CPU: 4
2020/04/25 21:33:16 Goroutine: 12
2020/04/25 21:33:16 Memory: 99000
2020/04/25 21:33:19 -------
2020/04/25 21:33:19 CPU: 4
2020/04/25 21:33:19 Goroutine: 12
2020/04/25 21:33:19 Memory: 99208
2020/04/25 21:33:21 -------
2020/04/25 21:33:21 CPU: 4
2020/04/25 21:33:21 Goroutine: 12
2020/04/25 21:33:21 Memory: 99432
2020/04/25 21:33:23 -------
2020/04/25 21:33:23 CPU: 4
2020/04/25 21:33:23 Goroutine: 12
2020/04/25 21:33:23 Memory: 99640
2020/04/25 21:33:25 -------
2020/04/25 21:33:25 CPU: 4
2020/04/25 21:33:25 Goroutine: 12
2020/04/25 21:33:25 Memory: 99864
2020/04/25 21:33:27 -------
2020/04/25 21:33:27 CPU: 4
2020/04/25 21:33:27 Goroutine: 12
2020/04/25 21:33:27 Memory: 100072
2020/04/25 21:33:29 -------
2020/04/25 21:33:29 CPU: 4
2020/04/25 21:33:29 Goroutine: 12
2020/04/25 21:33:29 Memory: 100280
2020/04/25 21:33:31 -------

...