|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +//go:build !nodiskstats |
| 15 | +// +build !nodiskstats |
| 16 | + |
| 17 | +package collector |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "log/slog" |
| 22 | + |
| 23 | + "github.com/power-devops/perfstat" |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | +) |
| 26 | + |
| 27 | +const diskstatsDefaultIgnoredDevices = "" |
| 28 | + |
| 29 | +type diskstatsCollector struct { |
| 30 | + rbytes typedDesc |
| 31 | + wbytes typedDesc |
| 32 | + time typedDesc |
| 33 | + |
| 34 | + deviceFilter deviceFilter |
| 35 | + logger *slog.Logger |
| 36 | + |
| 37 | + tickPerSecond int64 |
| 38 | +} |
| 39 | + |
| 40 | +func init() { |
| 41 | + registerCollector("diskstats", defaultEnabled, NewDiskstatsCollector) |
| 42 | +} |
| 43 | + |
| 44 | +// NewDiskstatsCollector returns a new Collector exposing disk device stats. |
| 45 | +func NewDiskstatsCollector(logger *slog.Logger) (Collector, error) { |
| 46 | + ticks, err := tickPerSecond() |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + deviceFilter, err := newDiskstatsDeviceFilter(logger) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to parse device filter flags: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + return &diskstatsCollector{ |
| 56 | + rbytes: typedDesc{readBytesDesc, prometheus.CounterValue}, |
| 57 | + wbytes: typedDesc{writtenBytesDesc, prometheus.CounterValue}, |
| 58 | + time: typedDesc{ioTimeSecondsDesc, prometheus.CounterValue}, |
| 59 | + |
| 60 | + deviceFilter: deviceFilter, |
| 61 | + logger: logger, |
| 62 | + |
| 63 | + tickPerSecond: ticks, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { |
| 68 | + stats, err := perfstat.DiskStat() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + for _, stat := range stats { |
| 74 | + if c.deviceFilter.ignored(stat.Name) { |
| 75 | + continue |
| 76 | + } |
| 77 | + ch <- c.rbytes.mustNewConstMetric(float64(stat.Rblks*512), stat.Name) |
| 78 | + ch <- c.wbytes.mustNewConstMetric(float64(stat.Wblks*512), stat.Name) |
| 79 | + ch <- c.time.mustNewConstMetric(float64(stat.Time/c.tickPerSecond), stat.Name) |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
0 commit comments