Skip to content

Commit f667e68

Browse files
committed
add cpu time information to the metrics endpoint
1 parent c0602e9 commit f667e68

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

zipserver/metrics.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"reflect"
99
"strings"
1010
"sync/atomic"
11+
"syscall"
1112
)
1213

1314
var globalMetrics = &MetricsCounter{}
@@ -44,9 +45,24 @@ func (m *MetricsCounter) RenderMetrics(config *Config) string {
4445

4546
}
4647

48+
userCPU, systemCPU := getCPUTime()
49+
metrics.WriteString(fmt.Sprintf("zipserver_cpu_user_seconds_total{host=\"%s\"} %f\n", hostname, userCPU))
50+
metrics.WriteString(fmt.Sprintf("zipserver_cpu_system_seconds_total{host=\"%s\"} %f\n", hostname, systemCPU))
51+
4752
return metrics.String()
4853
}
4954

55+
// getCPUTime returns the cumulative user and system CPU time for this process
56+
func getCPUTime() (userSeconds, systemSeconds float64) {
57+
var rusage syscall.Rusage
58+
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil {
59+
return 0, 0
60+
}
61+
userSeconds = float64(rusage.Utime.Sec) + float64(rusage.Utime.Usec)/1e6
62+
systemSeconds = float64(rusage.Stime.Sec) + float64(rusage.Stime.Usec)/1e6
63+
return
64+
}
65+
5066
// wrap a reader to count bytes read into the counter
5167
func metricsReader(reader io.Reader, counter *atomic.Int64) readerClosure {
5268
return func(p []byte) (int, error) {

zipserver/metrics_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ func Test_Metrics(t *testing.T) {
3232
MetricsHost: "localhost",
3333
}
3434

35-
expectedMetrics := `zipserver_requests_total{host="localhost"} 1
36-
zipserver_errors_total{host="localhost"} 0
37-
zipserver_extracted_files_total{host="localhost"} 1
38-
zipserver_copied_files_total{host="localhost"} 0
39-
zipserver_deleted_files_total{host="localhost"} 0
40-
zipserver_downloaded_bytes_total{host="localhost"} 7
41-
zipserver_uploaded_bytes_total{host="localhost"} 0
42-
`
43-
assert.Equal(t, expectedMetrics, metrics.RenderMetrics(config))
35+
rendered := metrics.RenderMetrics(config)
36+
37+
// Check counter metrics (exact values)
38+
assert.Contains(t, rendered, `zipserver_requests_total{host="localhost"} 1`)
39+
assert.Contains(t, rendered, `zipserver_errors_total{host="localhost"} 0`)
40+
assert.Contains(t, rendered, `zipserver_extracted_files_total{host="localhost"} 1`)
41+
assert.Contains(t, rendered, `zipserver_copied_files_total{host="localhost"} 0`)
42+
assert.Contains(t, rendered, `zipserver_deleted_files_total{host="localhost"} 0`)
43+
assert.Contains(t, rendered, `zipserver_downloaded_bytes_total{host="localhost"} 7`)
44+
assert.Contains(t, rendered, `zipserver_uploaded_bytes_total{host="localhost"} 0`)
45+
46+
// Check CPU metrics are present (values vary per run)
47+
assert.Contains(t, rendered, `zipserver_cpu_user_seconds_total{host="localhost"}`)
48+
assert.Contains(t, rendered, `zipserver_cpu_system_seconds_total{host="localhost"}`)
4449
}

0 commit comments

Comments
 (0)