mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-02 08:36:36 +00:00
feat: Tolerate collector failures (#1769)
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
@@ -25,7 +25,9 @@ import (
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -52,6 +54,7 @@ type Config struct {
|
||||
CollectorsEnabled []string `yaml:"collectors_enabled"`
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var ConfigDefaults = Config{
|
||||
CollectorsEnabled: []string{
|
||||
subCollectorDataStore,
|
||||
@@ -154,15 +157,19 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
version := windows.RtlGetVersion()
|
||||
|
||||
subCollectors := map[string]struct {
|
||||
build func() error
|
||||
collect func(ch chan<- prometheus.Metric) error
|
||||
close func()
|
||||
build func() error
|
||||
collect func(ch chan<- prometheus.Metric) error
|
||||
close func()
|
||||
minBuildNumber uint32
|
||||
}{
|
||||
subCollectorDataStore: {
|
||||
build: c.buildDataStore,
|
||||
collect: c.collectDataStore,
|
||||
close: c.perfDataCollectorDataStore.Close,
|
||||
build: c.buildDataStore,
|
||||
collect: c.collectDataStore,
|
||||
close: c.perfDataCollectorDataStore.Close,
|
||||
minBuildNumber: types.BuildNumberWindowsServer2022,
|
||||
},
|
||||
subCollectorDynamicMemoryBalancer: {
|
||||
build: c.buildDynamicMemoryBalancer,
|
||||
@@ -239,20 +246,30 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
|
||||
// Result must order, to prevent test failures.
|
||||
sort.Strings(c.config.CollectorsEnabled)
|
||||
|
||||
errs := make([]error, 0, len(c.config.CollectorsEnabled))
|
||||
|
||||
for _, name := range c.config.CollectorsEnabled {
|
||||
if _, ok := subCollectors[name]; !ok {
|
||||
return fmt.Errorf("unknown collector: %s", name)
|
||||
}
|
||||
|
||||
if version.BuildNumber < subCollectors[name].minBuildNumber {
|
||||
errs = append(errs, fmt.Errorf("collector %s requires Windows Server 2022 or newer", name))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if err := subCollectors[name].build(); err != nil {
|
||||
return fmt.Errorf("failed to build %s collector: %w", name, err)
|
||||
errs = append(errs, fmt.Errorf("failed to build %s collector: %w", name, err))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
c.collectorFns = append(c.collectorFns, subCollectors[name].collect)
|
||||
c.closeFns = append(c.closeFns, subCollectors[name].close)
|
||||
}
|
||||
|
||||
return nil
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -177,7 +176,7 @@ func (c *Collector) buildDataStore() error {
|
||||
dataStoreSetOperationLatencyMicro,
|
||||
dataStoreSetOperationCount,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V DataStore collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -463,7 +462,7 @@ func (c *Collector) buildDataStore() error {
|
||||
|
||||
func (c *Collector) collectDataStore(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorDataStore.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V DataStore metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -69,7 +68,7 @@ func (c *Collector) buildDynamicMemoryVM() error {
|
||||
vmMemoryRemovedMemory,
|
||||
vmMemoryGuestAvailableMemory,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Dynamic Memory VM collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -139,7 +138,7 @@ func (c *Collector) buildDynamicMemoryVM() error {
|
||||
|
||||
func (c *Collector) collectDynamicMemoryVM(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorDynamicMemoryVM.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Dynamic Memory VM metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -58,7 +57,7 @@ func (c *Collector) buildHypervisorVirtualProcessor() error {
|
||||
hypervisorVirtualProcessorRemoteRunTimePercent,
|
||||
hypervisorVirtualProcessorCPUWaitTimePerDispatch,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Hypervisor Virtual Processor collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -86,7 +85,7 @@ func (c *Collector) buildHypervisorVirtualProcessor() error {
|
||||
|
||||
func (c *Collector) collectHypervisorVirtualProcessor(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorHypervisorVirtualProcessor.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Hypervisor Virtual Processor metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -56,7 +55,7 @@ func (c *Collector) buildLegacyNetworkAdapter() error {
|
||||
legacyNetworkAdapterFramesReceived,
|
||||
legacyNetworkAdapterFramesSent,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Legacy Network Adapter collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -102,7 +101,7 @@ func (c *Collector) buildLegacyNetworkAdapter() error {
|
||||
|
||||
func (c *Collector) collectLegacyNetworkAdapter(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorLegacyNetworkAdapter.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Legacy Network Adapter metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -46,7 +45,7 @@ func (c *Collector) buildVirtualMachineVidPartition() error {
|
||||
preferredNUMANodeIndex,
|
||||
remotePhysicalPages,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V VM Vid Partition collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -74,7 +73,7 @@ func (c *Collector) buildVirtualMachineVidPartition() error {
|
||||
|
||||
func (c *Collector) collectVirtualMachineVidPartition(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualMachineVidPartition.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V VM Vid Partition metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -56,7 +55,7 @@ func (c *Collector) buildVirtualNetworkAdapter() error {
|
||||
virtualNetworkAdapterPacketsReceived,
|
||||
virtualNetworkAdapterPacketsSent,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Virtual Network Adapter collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -102,7 +101,7 @@ func (c *Collector) buildVirtualNetworkAdapter() error {
|
||||
|
||||
func (c *Collector) collectVirtualNetworkAdapter(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualNetworkAdapter.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Virtual Network Adapter metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -215,7 +214,7 @@ func (c *Collector) buildVirtualNetworkAdapterDropReasons() error {
|
||||
virtualNetworkAdapterDropReasonsOutgoingUnknown,
|
||||
virtualNetworkAdapterDropReasonsIncomingUnknown,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Virtual Network Adapter Drop Reasons collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -231,7 +230,7 @@ func (c *Collector) buildVirtualNetworkAdapterDropReasons() error {
|
||||
|
||||
func (c *Collector) collectVirtualNetworkAdapterDropReasons(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualNetworkAdapterDropReasons.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Virtual Network Adapter Drop Reasons metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -89,7 +88,7 @@ func (c *Collector) buildVirtualSMB() error {
|
||||
virtualSMBSentBytes,
|
||||
virtualSMBReceivedBytes,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Virtual SMB collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -201,7 +200,7 @@ func (c *Collector) buildVirtualSMB() error {
|
||||
|
||||
func (c *Collector) collectVirtualSMB(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualSMB.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Virtual SMB metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -74,7 +73,7 @@ func (c *Collector) buildVirtualStorageDevice() error {
|
||||
virtualStorageDeviceLowerLatency,
|
||||
virtualStorageDeviceIOQuotaReplenishmentRate,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Virtual Storage Device collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -156,7 +155,7 @@ func (c *Collector) buildVirtualStorageDevice() error {
|
||||
|
||||
func (c *Collector) collectVirtualStorageDevice(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualStorageDevice.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Virtual Storage Device metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
package hyperv
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/perfdata"
|
||||
@@ -100,7 +99,7 @@ func (c *Collector) buildVirtualSwitch() error {
|
||||
virtualSwitchPacketsSent,
|
||||
virtualSwitchPurgedMacAddresses,
|
||||
})
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Hyper-V Virtual Switch collector: %w", err)
|
||||
}
|
||||
|
||||
@@ -236,7 +235,7 @@ func (c *Collector) buildVirtualSwitch() error {
|
||||
|
||||
func (c *Collector) collectVirtualSwitch(ch chan<- prometheus.Metric) error {
|
||||
data, err := c.perfDataCollectorVirtualSwitch.Collect()
|
||||
if err != nil && !errors.Is(err, perfdata.ErrNoData) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to collect Hyper-V Virtual Switch metrics: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user