Skip to content

Commit

Permalink
feat(dfdaemon): add disk usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 committed Dec 3, 2023
1 parent 84cdd1e commit 241ac78
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
37 changes: 36 additions & 1 deletion client/daemon/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var (
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "piece_task_total",
Help: "Counter of the total failed piece tasks.",
Help: "Counter of the total piece tasks.",
})

PieceTaskFailedCount = promauto.NewCounter(prometheus.CounterOpts{
Expand Down Expand Up @@ -171,6 +171,41 @@ var (
Name: "version",
Help: "Version info of the service.",
}, []string{"major", "minor", "git_version", "git_commit", "platform", "build_time", "go_version", "go_tags", "go_gcflags"})

DataDiskUsage = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "data_disk_usage_total",
Help: "Gauger of the disk usage of data directory.",
})

DataDiskCapacity = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "data_disk_capacity_total",
Help: "Gauger of disk capacity of data directory.",
})

DataUnReclaimedUsage = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "data_unreclaimed_usage_total",
Help: "Gauger of unreclaimed data usage of data directory.",
})

DataDiskGCThreshold = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "data_disk_gc_threshold_total",
Help: "Gauger of disk gc threshold of data directory.",
})

DataDiskGCThresholdPercent = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: types.MetricsNamespace,
Subsystem: types.DfdaemonMetricsName,
Name: "data_disk_gc_threshold_percent",
Help: "Gauger of disk gc threshold percent of data directory.",
})
)

func New(addr string) *http.Server {
Expand Down
32 changes: 27 additions & 5 deletions client/daemon/storage/storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package storage

import (
"context"
"d7y.io/dragonfly/v2/client/daemon/metrics"

Check failure on line 23 in client/daemon/storage/storage_manager.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard,default,prefix(d7y.io/api),prefix(d7y.io/dragonfly/v2) (gci)
"encoding/base64"
"encoding/json"
"errors"
Expand Down Expand Up @@ -806,10 +807,24 @@ func (s *storageManager) TryGC() (bool, error) {
}
return true
})
diskUsage := s.diskUsage()
if diskUsage != nil {

Check failure on line 811 in client/daemon/storage/storage_manager.go

View workflow job for this annotation

GitHub Actions / Lint

SA9003: empty branch (staticcheck)

}
quotaBytesExceed := totalNotMarkedSize - int64(s.storeOption.DiskGCThreshold)
quotaExceed := s.storeOption.DiskGCThreshold > 0 && quotaBytesExceed > 0
usageExceed, usageBytesExceed := s.diskUsageExceed()

metrics.DataUnReclaimedUsage.Set(float64(totalNotMarkedSize))
metrics.DataDiskGCThreshold.Set(float64(s.storeOption.DiskGCThreshold))
metrics.DataDiskGCThresholdPercent.Set(s.storeOption.DiskGCThresholdPercent)

usage := s.diskUsage()
if usage != nil {
metrics.DataDiskUsage.Set(float64(usage.Used))
metrics.DataDiskCapacity.Set(float64(usage.Total))
}

usageExceed, usageBytesExceed := s.diskUsageExceed(usage)

if quotaExceed || usageExceed {
var bytesExceed int64
Expand Down Expand Up @@ -941,13 +956,20 @@ func (s *storageManager) forceGC() (bool, error) {
return true, nil
}

func (s *storageManager) diskUsageExceed() (exceed bool, bytes int64) {
if s.storeOption.DiskGCThresholdPercent <= 0 {
return false, 0
}
func (s *storageManager) diskUsage() *disk.UsageStat {
usage, err := disk.Usage(s.storeOption.DataPath)
if err != nil {
logger.Warnf("get %s disk usage error: %s", s.storeOption.DataPath, err)
return nil
}
return usage
}

func (s *storageManager) diskUsageExceed(usage *disk.UsageStat) (exceed bool, bytes int64) {
if s.storeOption.DiskGCThresholdPercent <= 0 {
return false, 0
}
if usage == nil {
return false, 0
}
logger.Debugf("disk usage: %+v", usage)
Expand Down

0 comments on commit 241ac78

Please sign in to comment.