1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
/*
* Copyright (c) 2022. Nydus Developers. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package collector
import (
"context"
"github.com/containerd/containerd/log"
"github.com/containerd/continuity/fs"
"github.com/containerd/nydus-snapshotter/pkg/metrics/data"
"github.com/containerd/nydus-snapshotter/pkg/metrics/tool"
"github.com/prometheus/client_golang/prometheus"
)
type SnapshotterMetricsCollector struct {
ctx context.Context
cacheDir string
pid int
lastStat *tool.Stat
}
type SnapshotMethod string
const (
SnapshotMethodUnknown SnapshotMethod = "UNKNOWN"
SnapshotMethodPrepare SnapshotMethod = "PREPARE"
SnapshotMethodMount SnapshotMethod = "MOUNTS"
SnapshotMethodCleanup SnapshotMethod = "CLEANUP"
SnapshotMethodRemove SnapshotMethod = "REMOVE"
)
func (s *SnapshotterMetricsCollector) CollectCacheUsage() {
du, err := fs.DiskUsage(s.ctx, s.cacheDir)
if err != nil {
log.L.Warnf("Get disk usage failed: %v", err)
} else {
data.CacheUsage.Set(float64(du.Size) / 1024)
}
}
func (s *SnapshotterMetricsCollector) CollectResourceUsage() {
currentStat, err := tool.GetProcessStat(s.pid)
if err != nil {
log.L.Warnf("Can not get current process stat.")
return
}
if s.lastStat == nil {
log.L.Debug("Can not get resource usage information: lastStat is nil")
s.lastStat = currentStat
return
}
cpuSys := (currentStat.Stime - s.lastStat.Stime) / tool.ClkTck
cpuUsr := (currentStat.Utime - s.lastStat.Utime) / tool.ClkTck
cpuPercent, err := tool.CalculateCPUUtilization(s.lastStat, currentStat)
if err != nil {
log.L.WithError(err).Warnf("Failed to calculate CPU utilization")
}
s.lastStat = currentStat
memory := currentStat.Rss * tool.PageSize
runTime := currentStat.Uptime - currentStat.Start/tool.ClkTck
data.CPUSystem.Set(tool.FormatFloat64(cpuSys, 2))
data.CPUUser.Set(tool.FormatFloat64(cpuUsr, 2))
data.CPUUsage.Set(tool.FormatFloat64(cpuPercent, 2))
data.MemoryUsage.Set(tool.FormatFloat64(memory/1024, 2))
data.Fds.Set(currentStat.Fds)
data.RunTime.Set(tool.FormatFloat64(runTime, 2))
data.Thread.Set(currentStat.Thread)
}
func (s *SnapshotterMetricsCollector) Collect() {
s.CollectCacheUsage()
s.CollectResourceUsage()
}
func CollectSnapshotMetricsTimer(h *prometheus.HistogramVec, event SnapshotMethod) *prometheus.Timer {
return prometheus.NewTimer(
prometheus.ObserverFunc(
(func(v float64) {
h.WithLabelValues(string(event)).Observe(tool.FormatFloat64(v*1000, 6))
})))
}
|