Convert remaining collectors to use ConstMetrics

This commit is contained in:
Johannes 'fish' Ziemke 2016-12-28 15:21:31 +01:00
commit 8e50b80d12
17 changed files with 455 additions and 614 deletions

View file

@ -40,7 +40,7 @@ var (
type diskstatsCollector struct {
ignoredDevicesPattern *regexp.Regexp
metrics []prometheus.Collector
descs []typedDesc
}
func init() {
@ -55,129 +55,116 @@ func NewDiskstatsCollector() (Collector, error) {
return &diskstatsCollector{
ignoredDevicesPattern: regexp.MustCompile(*ignoredDevices),
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt
metrics: []prometheus.Collector{
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "reads_completed",
Help: "The total number of reads completed successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "reads_merged",
Help: "The number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "sectors_read",
Help: "The total number of sectors read successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "read_time_ms",
Help: "The total number of milliseconds spent by all reads.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "writes_completed",
Help: "The total number of writes completed successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "writes_merged",
Help: "The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "sectors_written",
Help: "The total number of sectors written successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "write_time_ms",
Help: "This is the total number of milliseconds spent by all writes.",
},
diskLabelNames,
),
prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_now",
Help: "The number of I/Os currently in progress.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_time_ms",
Help: "Milliseconds spent doing I/Os.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "io_time_weighted",
Help: "The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "bytes_read",
Help: "The total number of bytes read successfully.",
},
diskLabelNames,
),
prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: diskSubsystem,
Name: "bytes_written",
Help: "The total number of bytes written successfully.",
},
diskLabelNames,
),
descs: []typedDesc{
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "reads_completed"),
"The total number of reads completed successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "reads_merged"),
"The total number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "sectors_read"),
"The total number of sectors read successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "read_time_ms"),
"The total number of milliseconds spent by all reads.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "writes_completed"),
"The total number of writes completed successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "writes_merged"),
"The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "sectors_written"),
"The total number of sectors written successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "write_time_ms"),
"This is the total number of milliseconds spent by all writes.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "io_now"),
"The number of I/Os currently in progress.",
diskLabelNames,
nil,
), valueType: prometheus.GaugeValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "io_time_ms"),
"Total Milliseconds spent doing I/Os.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "io_time_weighted"),
"The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "bytes_read"),
"The total number of bytes read successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
{
desc: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, diskSubsystem, "bytes_written"),
"The total number of bytes written successfully.",
diskLabelNames,
nil,
), valueType: prometheus.CounterValue,
},
},
}, nil
}
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
procDiskStats := procFilePath("diskstats")
diskStats, err := getDiskStats()
if err != nil {
@ -190,29 +177,19 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) (err error) {
continue
}
if len(stats) != len(c.metrics) {
if len(stats) != len(c.descs) {
return fmt.Errorf("invalid line for %s for %s", procDiskStats, dev)
}
for k, value := range stats {
for i, value := range stats {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid value %s in diskstats: %s", value, err)
}
if counter, ok := c.metrics[k].(*prometheus.CounterVec); ok {
counter.WithLabelValues(dev).Set(v)
} else if gauge, ok := c.metrics[k].(*prometheus.GaugeVec); ok {
gauge.WithLabelValues(dev).Set(v)
} else {
return fmt.Errorf("unexpected collector %d", k)
}
ch <- c.descs[i].mustNewConstMetric(v, dev)
}
}
for _, c := range c.metrics {
c.Collect(ch)
}
return err
return nil
}
func getDiskStats() (map[string]map[int]string, error) {