Use perflib for system collector

This commit is contained in:
Ben Reedy
2019-09-25 04:06:31 +00:00
parent 2688847c2e
commit 2aafa9ebf3
2 changed files with 18 additions and 26 deletions

View File

@@ -3,8 +3,6 @@
package collector package collector
import ( import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log" "github.com/prometheus/common/log"
) )
@@ -70,7 +68,7 @@ func NewSystemCollector() (Collector, error) {
// Collect sends the metric values for each metric // Collect sends the metric values for each metric
// to the provided prometheus Metric channel. // to the provided prometheus Metric channel.
func (c *SystemCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error { func (c *SystemCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil { if desc, err := c.collect(ctx, ch); err != nil {
log.Error("failed collecting system metrics:", desc, err) log.Error("failed collecting system metrics:", desc, err)
return err return err
} }
@@ -79,57 +77,50 @@ func (c *SystemCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metri
// Win32_PerfRawData_PerfOS_System docs: // Win32_PerfRawData_PerfOS_System docs:
// - https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp // - https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp
type Win32_PerfRawData_PerfOS_System struct { type system struct {
ContextSwitchesPersec uint32 ContextSwitchesPersec float64 `perflib:"Context Switches/sec"`
ExceptionDispatchesPersec uint32 ExceptionDispatchesPersec float64 `perflib:"Exception Dispatches/sec"`
Frequency_Object uint64 ProcessorQueueLength float64 `perflib:"Processor Queue Length"`
ProcessorQueueLength uint32 SystemCallsPersec float64 `perflib:"System Calls/sec"`
SystemCallsPersec uint32 SystemUpTime float64 `perflib:"System Up Time"`
SystemUpTime uint64 Threads float64 `perflib:"Threads"`
Threads uint32
Timestamp_Object uint64
} }
func (c *SystemCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { func (c *SystemCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_PerfRawData_PerfOS_System var dst []system
q := queryAll(&dst) if err := unmarshalObject(ctx.perfObjects["System"], &dst); err != nil {
if err := wmi.Query(q, &dst); err != nil {
return nil, err return nil, err
} }
if len(dst) == 0 {
return nil, errors.New("WMI query returned empty result set")
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ContextSwitchesTotal, c.ContextSwitchesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst[0].ContextSwitchesPersec), dst[0].ContextSwitchesPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ExceptionDispatchesTotal, c.ExceptionDispatchesTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst[0].ExceptionDispatchesPersec), dst[0].ExceptionDispatchesPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ProcessorQueueLength, c.ProcessorQueueLength,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].ProcessorQueueLength), dst[0].ProcessorQueueLength,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemCallsTotal, c.SystemCallsTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(dst[0].SystemCallsPersec), dst[0].SystemCallsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemUpTime, c.SystemUpTime,
prometheus.GaugeValue, prometheus.GaugeValue,
// convert from Windows timestamp (1 jan 1601) to unix timestamp (1 jan 1970) dst[0].SystemUpTime,
float64(dst[0].SystemUpTime-116444736000000000)/float64(dst[0].Frequency_Object),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.Threads, c.Threads,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].Threads), dst[0].Threads,
) )
return nil, nil return nil, nil
} }

View File

@@ -5,6 +5,7 @@ The system collector exposes metrics about ...
||| |||
-|- -|-
Metric name prefix | `system` Metric name prefix | `system`
Data source | Perflib
Classes | [`Win32_PerfRawData_PerfOS_System`](https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp) Classes | [`Win32_PerfRawData_PerfOS_System`](https://web.archive.org/web/20050830140516/http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_perfrawdata_perfos_system.asp)
Enabled by default? | Yes Enabled by default? | Yes