Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(diskstats): add metric for bare disk capacity #3068

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions collector/diskstats_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ var (
diskLabelNames,
nil,
)

diskCapacityDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "capacity_bytes"),
"Capacity of the disk in bytes.",
diskLabelNames, nil,
)
)

func newDiskstatsDeviceFilter(logger log.Logger) (deviceFilter, error) {
Expand Down
25 changes: 25 additions & 0 deletions collector/diskstats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
nil,
), valueType: prometheus.CounterValue,
},
{
desc: diskCapacityDesc, valueType: prometheus.GaugeValue,
},
},
filesystemInfoDesc: typedFactorDesc{
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "filesystem_info"),
Expand Down Expand Up @@ -366,6 +369,28 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
}
}
}

sizePath := fmt.Sprintf("/sys/block/%s/size", dev)
fs185143 marked this conversation as resolved.
Show resolved Hide resolved
sizeBytes, err := os.ReadFile(sizePath)
if err != nil {
level.Error(c.logger).Log("msg", "Failed to read disk size", "path", sizePath, "err", err)
continue
}

size, err := strconv.ParseUint(strings.TrimSpace(string(sizeBytes)), 10, 64)
if err != nil {
level.Error(c.logger).Log("msg", "Failed to parse disk size", "err", err)
continue
}

sizeInBytes := size * 512 // convert size to bytes (size is in 512-byte sectors)

ch <- prometheus.MustNewConstMetric(
diskCapacityDesc,
prometheus.GaugeValue,
float64(sizeInBytes),
dev,
)
}
return nil
}
Expand Down