Merge pull request #402 from breed808/perf_mem

Use perflib for memory collector
This commit is contained in:
Calle Pettersson
2019-10-09 21:16:46 +02:00
committed by GitHub
2 changed files with 73 additions and 74 deletions

View File

@@ -6,7 +6,6 @@
package collector package collector
import ( import (
"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"
) )
@@ -15,7 +14,7 @@ func init() {
Factories["memory"] = NewMemoryCollector Factories["memory"] = NewMemoryCollector
} }
// A MemoryCollector is a Prometheus collector for WMI Win32_PerfRawData_PerfOS_Memory metrics // A MemoryCollector is a Prometheus collector for perflib Memory metrics
type MemoryCollector struct { type MemoryCollector struct {
AvailableBytes *prometheus.Desc AvailableBytes *prometheus.Desc
CacheBytes *prometheus.Desc CacheBytes *prometheus.Desc
@@ -257,247 +256,246 @@ func NewMemoryCollector() (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 *MemoryCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error { func (c *MemoryCollector) 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 memory metrics:", desc, err) log.Error("failed collecting memory metrics:", desc, err)
return err return err
} }
return nil return nil
} }
type Win32_PerfRawData_PerfOS_Memory struct { type memory struct {
AvailableBytes uint64 AvailableBytes float64 `perflib:"Available Bytes"`
AvailableKBytes uint64 AvailableKBytes float64 `perflib:"Available KBytes"`
AvailableMBytes uint64 AvailableMBytes float64 `perflib:"Available MBytes"`
CacheBytes uint64 CacheBytes float64 `perflib:"Cache Bytes"`
CacheBytesPeak uint64 CacheBytesPeak float64 `perflib:"Cache Bytes Peak"`
CacheFaultsPersec uint32 CacheFaultsPersec float64 `perflib:"Cache Faults/sec"`
CommitLimit uint64 CommitLimit float64 `perflib:"Commit Limit"`
CommittedBytes uint64 CommittedBytes float64 `perflib:"Committed Bytes"`
DemandZeroFaultsPersec uint32 DemandZeroFaultsPersec float64 `perflib:"Demand Zero Faults/sec"`
FreeAndZeroPageListBytes uint64 FreeAndZeroPageListBytes float64 `perflib:"Free & Zero Page List Bytes"`
FreeSystemPageTableEntries uint32 FreeSystemPageTableEntries float64 `perflib:"Free System Page Table Entries"`
ModifiedPageListBytes uint64 ModifiedPageListBytes float64 `perflib:"Modified Page List Bytes"`
PageFaultsPersec uint32 PageFaultsPersec float64 `perflib:"Page Faults/sec"`
PageReadsPersec uint32 PageReadsPersec float64 `perflib:"Page Reads/sec"`
PagesInputPersec uint32 PagesInputPersec float64 `perflib:"Pages Input/sec"`
PagesOutputPersec uint32 PagesOutputPersec float64 `perflib:"Pages Output/sec"`
PagesPersec uint32 PagesPersec float64 `perflib:"Pages/sec"`
PageWritesPersec uint32 PageWritesPersec float64 `perflib:"Page Writes/sec"`
PoolNonpagedAllocs uint32 PoolNonpagedAllocs float64 `perflib:"Pool Nonpaged Allocs"`
PoolNonpagedBytes uint64 PoolNonpagedBytes float64 `perflib:"Pool Nonpaged Bytes"`
PoolPagedAllocs uint32 PoolPagedAllocs float64 `perflib:"Pool Paged Allocs"`
PoolPagedBytes uint64 PoolPagedBytes float64 `perflib:"Pool Paged Bytes"`
PoolPagedResidentBytes uint64 PoolPagedResidentBytes float64 `perflib:"Pool Paged Resident Bytes"`
StandbyCacheCoreBytes uint64 StandbyCacheCoreBytes float64 `perflib:"Standby Cache Core Bytes"`
StandbyCacheNormalPriorityBytes uint64 StandbyCacheNormalPriorityBytes float64 `perflib:"Standby Cache Normal Priority Bytes"`
StandbyCacheReserveBytes uint64 StandbyCacheReserveBytes float64 `perflib:"Standby Cache Reserve Bytes"`
SystemCacheResidentBytes uint64 SystemCacheResidentBytes float64 `perflib:"System Cache Resident Bytes"`
SystemCodeResidentBytes uint64 SystemCodeResidentBytes float64 `perflib:"System Code Resident Bytes"`
SystemCodeTotalBytes uint64 SystemCodeTotalBytes float64 `perflib:"System Code Total Bytes"`
SystemDriverResidentBytes uint64 SystemDriverResidentBytes float64 `perflib:"System Driver Resident Bytes"`
SystemDriverTotalBytes uint64 SystemDriverTotalBytes float64 `perflib:"System Driver Total Bytes"`
TransitionFaultsPersec uint32 TransitionFaultsPersec float64 `perflib:"Transition Faults/sec"`
TransitionPagesRePurposedPersec uint32 TransitionPagesRePurposedPersec float64 `perflib:"Transition Pages RePurposed/sec"`
WriteCopiesPersec uint32 WriteCopiesPersec float64 `perflib:"Write Copies/sec"`
} }
func (c *MemoryCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { func (c *MemoryCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_PerfRawData_PerfOS_Memory var dst []memory
q := queryAll(&dst) if err := unmarshalObject(ctx.perfObjects["Memory"], &dst); err != nil {
if err := wmi.Query(q, &dst); err != nil {
return nil, err return nil, err
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.AvailableBytes, c.AvailableBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].AvailableBytes), dst[0].AvailableBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CacheBytes, c.CacheBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].CacheBytes), dst[0].CacheBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CacheBytesPeak, c.CacheBytesPeak,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].CacheBytesPeak), dst[0].CacheBytesPeak,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CacheFaultsTotal, c.CacheFaultsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].CacheFaultsPersec), dst[0].CacheFaultsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CommitLimit, c.CommitLimit,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].CommitLimit), dst[0].CommitLimit,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.CommittedBytes, c.CommittedBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].CommittedBytes), dst[0].CommittedBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.DemandZeroFaultsTotal, c.DemandZeroFaultsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].DemandZeroFaultsPersec), dst[0].DemandZeroFaultsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.FreeAndZeroPageListBytes, c.FreeAndZeroPageListBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].FreeAndZeroPageListBytes), dst[0].FreeAndZeroPageListBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.FreeSystemPageTableEntries, c.FreeSystemPageTableEntries,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].FreeSystemPageTableEntries), dst[0].FreeSystemPageTableEntries,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ModifiedPageListBytes, c.ModifiedPageListBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].ModifiedPageListBytes), dst[0].ModifiedPageListBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PageFaultsTotal, c.PageFaultsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PageFaultsPersec), dst[0].PageFaultsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SwapPageReadsTotal, c.SwapPageReadsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PageReadsPersec), dst[0].PageReadsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SwapPagesReadTotal, c.SwapPagesReadTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PagesInputPersec), dst[0].PagesInputPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SwapPagesWrittenTotal, c.SwapPagesWrittenTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PagesOutputPersec), dst[0].PagesOutputPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SwapPageOperationsTotal, c.SwapPageOperationsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PagesPersec), dst[0].PagesPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SwapPageWritesTotal, c.SwapPageWritesTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PageWritesPersec), dst[0].PageWritesPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PoolNonpagedAllocsTotal, c.PoolNonpagedAllocsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PoolNonpagedAllocs), dst[0].PoolNonpagedAllocs,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PoolNonpagedBytes, c.PoolNonpagedBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PoolNonpagedBytes), dst[0].PoolNonpagedBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PoolPagedAllocsTotal, c.PoolPagedAllocsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PoolPagedAllocs), dst[0].PoolPagedAllocs,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PoolPagedBytes, c.PoolPagedBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PoolPagedBytes), dst[0].PoolPagedBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PoolPagedResidentBytes, c.PoolPagedResidentBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].PoolPagedResidentBytes), dst[0].PoolPagedResidentBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.StandbyCacheCoreBytes, c.StandbyCacheCoreBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].StandbyCacheCoreBytes), dst[0].StandbyCacheCoreBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.StandbyCacheNormalPriorityBytes, c.StandbyCacheNormalPriorityBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].StandbyCacheNormalPriorityBytes), dst[0].StandbyCacheNormalPriorityBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.StandbyCacheReserveBytes, c.StandbyCacheReserveBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].StandbyCacheReserveBytes), dst[0].StandbyCacheReserveBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemCacheResidentBytes, c.SystemCacheResidentBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SystemCacheResidentBytes), dst[0].SystemCacheResidentBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemCodeResidentBytes, c.SystemCodeResidentBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SystemCodeResidentBytes), dst[0].SystemCodeResidentBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemCodeTotalBytes, c.SystemCodeTotalBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SystemCodeTotalBytes), dst[0].SystemCodeTotalBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemDriverResidentBytes, c.SystemDriverResidentBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SystemDriverResidentBytes), dst[0].SystemDriverResidentBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.SystemDriverTotalBytes, c.SystemDriverTotalBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].SystemDriverTotalBytes), dst[0].SystemDriverTotalBytes,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TransitionFaultsTotal, c.TransitionFaultsTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].TransitionFaultsPersec), dst[0].TransitionFaultsPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.TransitionPagesRepurposedTotal, c.TransitionPagesRepurposedTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].TransitionPagesRePurposedPersec), dst[0].TransitionPagesRePurposedPersec,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.WriteCopiesTotal, c.WriteCopiesTotal,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(dst[0].WriteCopiesPersec), dst[0].WriteCopiesPersec,
) )
return nil, nil return nil, nil

View File

@@ -5,6 +5,7 @@ The memory collector exposes metrics about system memory usage
||| |||
-|- -|-
Metric name prefix | `memory` Metric name prefix | `memory`
Data source | Perflib
Classes | `Win32_PerfRawData_PerfOS_Memory` Classes | `Win32_PerfRawData_PerfOS_Memory`
Enabled by default? | Yes Enabled by default? | Yes