Skip to content

Commit 73f82ab

Browse files
authored
Add system metrics (#83)
* Add system metrics * fix go mod * Linter
1 parent c06bcdd commit 73f82ab

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/go-redsync/redsync/v4 v4.12.1
1515
github.com/google/uuid v1.6.0
1616
github.com/prometheus/client_golang v1.19.0
17+
github.com/shirou/gopsutil v3.21.11+incompatible
1718
github.com/spf13/cobra v1.8.0
1819
github.com/spf13/viper v1.18.2
1920
github.com/stretchr/testify v1.9.0
@@ -36,6 +37,7 @@ require (
3637
github.com/fsnotify/fsnotify v1.7.0 // indirect
3738
github.com/go-faster/city v1.0.1 // indirect
3839
github.com/go-faster/errors v0.7.1 // indirect
40+
github.com/go-ole/go-ole v1.2.6 // indirect
3941
github.com/hashicorp/errwrap v1.1.0 // indirect
4042
github.com/hashicorp/go-multierror v1.1.1 // indirect
4143
github.com/hashicorp/go-version v1.6.0 // indirect
@@ -67,7 +69,10 @@ require (
6769
github.com/spf13/pflag v1.0.5 // indirect
6870
github.com/stretchr/objx v0.5.2 // indirect
6971
github.com/subosito/gotenv v1.6.0 // indirect
72+
github.com/tklauser/go-sysconf v0.3.12 // indirect
73+
github.com/tklauser/numcpus v0.6.1 // indirect
7074
github.com/yuin/gopher-lua v1.1.0 // indirect
75+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
7176
go.opentelemetry.io/otel v1.24.0 // indirect
7277
go.opentelemetry.io/otel/trace v1.24.0 // indirect
7378
go.uber.org/multierr v1.11.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,7 @@ github.com/go-logr/stdr v1.2.0/go.mod h1:YkVgnZu1ZjjL7xTxrfm/LLZBfkhTqSR1ydtm6jT
10921092
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
10931093
github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4=
10941094
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
1095+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
10951096
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
10961097
github.com/go-openapi/jsonpointer v0.0.0-20160704185906-46af16f9f7b1/go.mod h1:+35s3my2LFTysnkMfxsJBAMHj/DoqoB9knIWoYG/Vk0=
10971098
github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg=
@@ -1711,6 +1712,7 @@ github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
17111712
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
17121713
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
17131714
github.com/shirou/gopsutil v2.19.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
1715+
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
17141716
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
17151717
github.com/shirou/gopsutil/v3 v3.23.8/go.mod h1:7hmCaBn+2ZwaZOr6jmPBZDfawwMGuo1id3C6aM8EDqQ=
17161718
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
@@ -1808,7 +1810,9 @@ github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ
18081810
github.com/tchap/go-patricia/v2 v2.3.1/go.mod h1:VZRHKAb53DLaG+nA9EaYYiaEx6YztwDlLElMsnSHD4k=
18091811
github.com/testcontainers/testcontainers-go v0.25.0/go.mod h1:4sC9SiJyzD1XFi59q8umTQYWxnkweEc5OjVtTUlJzqQ=
18101812
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
1813+
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
18111814
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
1815+
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
18121816
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
18131817
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
18141818
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -1859,6 +1863,7 @@ github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
18591863
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
18601864
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
18611865
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
1866+
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
18621867
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
18631868
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
18641869
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=

pkg/metrics/service_metrics.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package metrics
2+
3+
import (
4+
"context"
5+
"github.com/shirou/gopsutil/cpu"
6+
"github.com/shirou/gopsutil/net"
7+
"github.com/zondax/golem/pkg/logger"
8+
"github.com/zondax/golem/pkg/metrics/collectors"
9+
"runtime"
10+
"runtime/pprof"
11+
"time"
12+
)
13+
14+
const (
15+
memoryUsageBytes = "memory_usage_bytes"
16+
cpuUsagePercent = "cpu_usage_percent"
17+
bytesSent = "bytes_sent"
18+
bytesReceived = "bytes_received"
19+
activeConnections = "system_active_connections"
20+
goroutinesCount = "goroutines_count"
21+
threadsCount = "threads_count"
22+
)
23+
24+
func RegisterSystemMetrics(metricsServer TaskMetrics) []error {
25+
var errs []error
26+
27+
register := func(name, help string, labels []string, handler MetricHandler) {
28+
if err := metricsServer.RegisterMetric(name, help, labels, handler); err != nil {
29+
errs = append(errs, err)
30+
}
31+
}
32+
33+
register(memoryUsageBytes, "Memory usage in bytes.", nil, &collectors.Gauge{})
34+
register(cpuUsagePercent, "CPU usage in percent.", nil, &collectors.Gauge{})
35+
register(bytesSent, "Total bytes sent over network.", nil, &collectors.Counter{})
36+
register(bytesReceived, "Total bytes received over network.", nil, &collectors.Counter{})
37+
register(activeConnections, "Number of active network connections.", nil, &collectors.Gauge{})
38+
register(goroutinesCount, "Number of goroutines.", nil, &collectors.Gauge{})
39+
register(threadsCount, "Number of OS threads.", nil, &collectors.Gauge{})
40+
41+
return errs
42+
}
43+
44+
func UpdateSystemMetrics(metricsServer TaskMetrics, updateInterval time.Duration) {
45+
for {
46+
var m runtime.MemStats
47+
runtime.ReadMemStats(&m)
48+
if err := metricsServer.UpdateMetric(memoryUsageBytes, float64(m.Alloc)); err != nil {
49+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", memoryUsageBytes, err)
50+
}
51+
52+
cpuPercents, _ := cpu.Percent(time.Second, false)
53+
if err := metricsServer.UpdateMetric(cpuUsagePercent, cpuPercents[0]); err != nil {
54+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", cpuUsagePercent, err)
55+
}
56+
57+
netIO, _ := net.IOCounters(false)
58+
if err := metricsServer.UpdateMetric(bytesSent, float64(netIO[0].BytesSent)); err != nil {
59+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", bytesSent, err)
60+
}
61+
if err := metricsServer.UpdateMetric(bytesReceived, float64(netIO[0].BytesRecv)); err != nil {
62+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", bytesReceived, err)
63+
}
64+
65+
conns, _ := net.Connections("all")
66+
if err := metricsServer.UpdateMetric(activeConnections, float64(len(conns))); err != nil {
67+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", activeConnections, err)
68+
}
69+
70+
if err := metricsServer.UpdateMetric(goroutinesCount, float64(runtime.NumGoroutine())); err != nil {
71+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", goroutinesCount, err)
72+
}
73+
74+
threads := pprof.Lookup("threadcreate").Count()
75+
if err := metricsServer.UpdateMetric(threadsCount, float64(threads)); err != nil {
76+
logger.GetLoggerFromContext(context.Background()).Errorf("error updating %v: %v", threadsCount, err)
77+
}
78+
79+
time.Sleep(updateInterval)
80+
}
81+
}

0 commit comments

Comments
 (0)