mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-05 10:06:35 +00:00
system: refactor collector (#1730)
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
@@ -109,7 +109,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
|
||||
var err error
|
||||
|
||||
c.perfDataCollector, err = perfdata.NewCollector(perfdata.V1, "Processor Information", perfdata.AllInstances, counters)
|
||||
c.perfDataCollector, err = perfdata.NewCollector(perfdata.V2, "Processor Information", perfdata.AllInstances, counters)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Processor Information collector: %w", err)
|
||||
}
|
||||
|
||||
11
internal/collector/system/const.go
Normal file
11
internal/collector/system/const.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package system
|
||||
|
||||
const (
|
||||
ContextSwitchesPersec = "Context Switches/sec"
|
||||
ExceptionDispatchesPersec = "Exception Dispatches/sec"
|
||||
ProcessorQueueLength = "Processor Queue Length"
|
||||
SystemCallsPersec = "System Calls/sec"
|
||||
SystemUpTime = "System Up Time"
|
||||
Processes = "Processes"
|
||||
Threads = "Threads"
|
||||
)
|
||||
@@ -4,11 +4,13 @@ package system
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
v1 "github.com/prometheus-community/windows_exporter/internal/perfdata/v1"
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata/perftypes"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
@@ -23,6 +25,8 @@ var ConfigDefaults = Config{}
|
||||
type Collector struct {
|
||||
config Config
|
||||
|
||||
perfDataCollector perfdata.Collector
|
||||
|
||||
contextSwitchesTotal *prometheus.Desc
|
||||
exceptionDispatchesTotal *prometheus.Desc
|
||||
processorQueueLength *prometheus.Desc
|
||||
@@ -54,7 +58,7 @@ func (c *Collector) GetName() string {
|
||||
}
|
||||
|
||||
func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) {
|
||||
return []string{"System"}, nil
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func (c *Collector) Close(_ *slog.Logger) error {
|
||||
@@ -62,6 +66,23 @@ func (c *Collector) Close(_ *slog.Logger) error {
|
||||
}
|
||||
|
||||
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
counters := []string{
|
||||
ContextSwitchesPersec,
|
||||
ExceptionDispatchesPersec,
|
||||
ProcessorQueueLength,
|
||||
SystemCallsPersec,
|
||||
SystemUpTime,
|
||||
Processes,
|
||||
Threads,
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
c.perfDataCollector, err = perfdata.NewCollector(perfdata.V2, "System", nil, counters)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create System collector: %w", err)
|
||||
}
|
||||
|
||||
c.contextSwitchesTotal = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "context_switches_total"),
|
||||
"Total number of context switches (WMI source: PerfOS_System.ContextSwitchesPersec)",
|
||||
@@ -117,78 +138,59 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) Collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = logger.With(slog.String("collector", Name))
|
||||
if err := c.collect(ctx, logger, ch); err != nil {
|
||||
logger.Error("failed collecting system metrics",
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
return err
|
||||
func (c *Collector) Collect(_ *types.ScrapeContext, _ *slog.Logger, ch chan<- prometheus.Metric) error {
|
||||
if err := c.collect(ch); err != nil {
|
||||
return fmt.Errorf("failed collecting system metrics: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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
|
||||
type system struct {
|
||||
ContextSwitchesPersec float64 `perflib:"Context Switches/sec"`
|
||||
ExceptionDispatchesPersec float64 `perflib:"Exception Dispatches/sec"`
|
||||
ProcessorQueueLength float64 `perflib:"Processor Queue Length"`
|
||||
SystemCallsPersec float64 `perflib:"System Calls/sec"`
|
||||
SystemUpTime float64 `perflib:"System Up Time"`
|
||||
Processes float64 `perflib:"Processes"`
|
||||
Threads float64 `perflib:"Threads"`
|
||||
}
|
||||
|
||||
func (c *Collector) collect(ctx *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = logger.With(slog.String("collector", Name))
|
||||
|
||||
var dst []system
|
||||
|
||||
if err := v1.UnmarshalObject(ctx.PerfObjects["System"], &dst, logger); err != nil {
|
||||
return err
|
||||
func (c *Collector) collect(ch chan<- prometheus.Metric) error {
|
||||
perfData, err := c.perfDataCollector.Collect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect System metrics: %w", err)
|
||||
}
|
||||
|
||||
if len(dst) == 0 {
|
||||
return errors.New("no data returned from Performance Counter")
|
||||
data, ok := perfData[perftypes.EmptyInstance]
|
||||
if !ok {
|
||||
return errors.New("query for System returned empty result set")
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.contextSwitchesTotal,
|
||||
prometheus.CounterValue,
|
||||
dst[0].ContextSwitchesPersec,
|
||||
data[ContextSwitchesPersec].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.exceptionDispatchesTotal,
|
||||
prometheus.CounterValue,
|
||||
dst[0].ExceptionDispatchesPersec,
|
||||
data[ExceptionDispatchesPersec].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.processorQueueLength,
|
||||
prometheus.GaugeValue,
|
||||
dst[0].ProcessorQueueLength,
|
||||
data[ProcessorQueueLength].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.processes,
|
||||
prometheus.GaugeValue,
|
||||
dst[0].Processes,
|
||||
data[Processes].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.systemCallsTotal,
|
||||
prometheus.CounterValue,
|
||||
dst[0].SystemCallsPersec,
|
||||
data[SystemCallsPersec].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.systemUpTime,
|
||||
prometheus.GaugeValue,
|
||||
dst[0].SystemUpTime,
|
||||
data[SystemUpTime].FirstValue,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.threads,
|
||||
prometheus.GaugeValue,
|
||||
dst[0].Threads,
|
||||
data[Threads].FirstValue,
|
||||
)
|
||||
|
||||
// Windows has no defined limit, and is based off available resources. This currently isn't calculated by WMI and is set to default value.
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
package terminal_services
|
||||
|
||||
const (
|
||||
HandleCount = "Handle Count"
|
||||
PageFaultsPersec = "Page Faults/sec"
|
||||
PageFileBytes = "Page File Bytes"
|
||||
PageFileBytesPeak = "Page File Bytes Peak"
|
||||
PercentPrivilegedTime = "% Privileged Time"
|
||||
PercentProcessorTime = "% Processor Time"
|
||||
PercentUserTime = "% User Time"
|
||||
PoolNonpagedBytes = "Pool Nonpaged Bytes"
|
||||
PoolPagedBytes = "Pool Paged Bytes"
|
||||
PrivateBytes = "Private Bytes"
|
||||
ThreadCount = "Thread Count"
|
||||
VirtualBytes = "Virtual Bytes"
|
||||
VirtualBytesPeak = "Virtual Bytes Peak"
|
||||
WorkingSet = "Working Set"
|
||||
WorkingSetPeak = "Working Set Peak"
|
||||
handleCount = "Handle Count"
|
||||
pageFaultsPersec = "Page Faults/sec"
|
||||
pageFileBytes = "Page File Bytes"
|
||||
pageFileBytesPeak = "Page File Bytes Peak"
|
||||
percentPrivilegedTime = "% Privileged Time"
|
||||
percentProcessorTime = "% Processor Time"
|
||||
percentUserTime = "% User Time"
|
||||
poolNonpagedBytes = "Pool Nonpaged Bytes"
|
||||
poolPagedBytes = "Pool Paged Bytes"
|
||||
privateBytes = "Private Bytes"
|
||||
threadCount = "Thread Count"
|
||||
virtualBytes = "Virtual Bytes"
|
||||
virtualBytesPeak = "Virtual Bytes Peak"
|
||||
workingSet = "Working Set"
|
||||
workingSetPeak = "Working Set Peak"
|
||||
|
||||
SuccessfulConnections = "Successful Connections"
|
||||
PendingConnections = "Pending Connections"
|
||||
FailedConnections = "Failed Connections"
|
||||
successfulConnections = "Successful Connections"
|
||||
pendingConnections = "Pending Connections"
|
||||
failedConnections = "Failed Connections"
|
||||
)
|
||||
|
||||
@@ -126,21 +126,21 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
|
||||
logger = logger.With(slog.String("collector", Name))
|
||||
|
||||
counters := []string{
|
||||
HandleCount,
|
||||
PageFaultsPersec,
|
||||
PageFileBytes,
|
||||
PageFileBytesPeak,
|
||||
PercentPrivilegedTime,
|
||||
PercentProcessorTime,
|
||||
PercentUserTime,
|
||||
PoolNonpagedBytes,
|
||||
PoolPagedBytes,
|
||||
PrivateBytes,
|
||||
ThreadCount,
|
||||
VirtualBytes,
|
||||
VirtualBytesPeak,
|
||||
WorkingSet,
|
||||
WorkingSetPeak,
|
||||
handleCount,
|
||||
pageFaultsPersec,
|
||||
pageFileBytes,
|
||||
pageFileBytesPeak,
|
||||
percentPrivilegedTime,
|
||||
percentProcessorTime,
|
||||
percentUserTime,
|
||||
poolNonpagedBytes,
|
||||
poolPagedBytes,
|
||||
privateBytes,
|
||||
threadCount,
|
||||
virtualBytes,
|
||||
virtualBytesPeak,
|
||||
workingSet,
|
||||
workingSetPeak,
|
||||
}
|
||||
|
||||
var err error
|
||||
@@ -154,9 +154,9 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error {
|
||||
|
||||
if c.connectionBrokerEnabled {
|
||||
counters = []string{
|
||||
SuccessfulConnections,
|
||||
PendingConnections,
|
||||
FailedConnections,
|
||||
successfulConnections,
|
||||
pendingConnections,
|
||||
failedConnections,
|
||||
}
|
||||
|
||||
var err error
|
||||
@@ -317,94 +317,94 @@ func (c *Collector) collectTSSessionCounters(ch chan<- prometheus.Metric) error
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.handleCount,
|
||||
prometheus.GaugeValue,
|
||||
data[HandleCount].FirstValue,
|
||||
data[handleCount].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.pageFaultsPerSec,
|
||||
prometheus.CounterValue,
|
||||
data[PageFaultsPersec].FirstValue,
|
||||
data[pageFaultsPersec].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.pageFileBytes,
|
||||
prometheus.GaugeValue,
|
||||
data[PageFileBytes].FirstValue,
|
||||
data[pageFileBytes].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.pageFileBytesPeak,
|
||||
prometheus.GaugeValue,
|
||||
data[PageFileBytesPeak].FirstValue,
|
||||
data[pageFileBytesPeak].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.percentCPUTime,
|
||||
prometheus.CounterValue,
|
||||
data[PercentPrivilegedTime].FirstValue,
|
||||
data[percentPrivilegedTime].FirstValue,
|
||||
name,
|
||||
"privileged",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.percentCPUTime,
|
||||
prometheus.CounterValue,
|
||||
data[PercentProcessorTime].FirstValue,
|
||||
data[percentProcessorTime].FirstValue,
|
||||
name,
|
||||
"processor",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.percentCPUTime,
|
||||
prometheus.CounterValue,
|
||||
data[PercentUserTime].FirstValue,
|
||||
data[percentUserTime].FirstValue,
|
||||
name,
|
||||
"user",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.poolNonPagedBytes,
|
||||
prometheus.GaugeValue,
|
||||
data[PoolNonpagedBytes].FirstValue,
|
||||
data[poolNonpagedBytes].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.poolPagedBytes,
|
||||
prometheus.GaugeValue,
|
||||
data[PoolPagedBytes].FirstValue,
|
||||
data[poolPagedBytes].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.privateBytes,
|
||||
prometheus.GaugeValue,
|
||||
data[PrivateBytes].FirstValue,
|
||||
data[privateBytes].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.threadCount,
|
||||
prometheus.GaugeValue,
|
||||
data[ThreadCount].FirstValue,
|
||||
data[threadCount].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.virtualBytes,
|
||||
prometheus.GaugeValue,
|
||||
data[VirtualBytes].FirstValue,
|
||||
data[virtualBytes].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.virtualBytesPeak,
|
||||
prometheus.GaugeValue,
|
||||
data[VirtualBytesPeak].FirstValue,
|
||||
data[virtualBytesPeak].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.workingSet,
|
||||
prometheus.GaugeValue,
|
||||
data[WorkingSet].FirstValue,
|
||||
data[workingSet].FirstValue,
|
||||
name,
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.workingSetPeak,
|
||||
prometheus.GaugeValue,
|
||||
data[WorkingSetPeak].FirstValue,
|
||||
data[workingSetPeak].FirstValue,
|
||||
name,
|
||||
)
|
||||
}
|
||||
@@ -426,21 +426,21 @@ func (c *Collector) collectCollectionBrokerPerformanceCounter(ch chan<- promethe
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionBrokerPerformance,
|
||||
prometheus.CounterValue,
|
||||
data[SuccessfulConnections].FirstValue,
|
||||
data[successfulConnections].FirstValue,
|
||||
"Successful",
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionBrokerPerformance,
|
||||
prometheus.CounterValue,
|
||||
data[PendingConnections].FirstValue,
|
||||
data[pendingConnections].FirstValue,
|
||||
"Pending",
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.connectionBrokerPerformance,
|
||||
prometheus.CounterValue,
|
||||
data[FailedConnections].FirstValue,
|
||||
data[failedConnections].FirstValue,
|
||||
"Failed",
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user