mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-31 14:56:35 +00:00
mi: make timeout configurable for build functions (#2377)
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
@@ -179,21 +180,21 @@ func (c *Collector) Build(_ *slog.Logger, miSession *mi.Session) error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) error {
|
||||
if len(c.config.CollectorsEnabled) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
errCh := make(chan error, 6)
|
||||
errCh := make(chan error, 7)
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(6)
|
||||
wg.Add(7)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorCluster) {
|
||||
if err := c.collectCluster(ch); err != nil {
|
||||
if err := c.collectCluster(ch, maxScrapeDuration); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect cluster metrics: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -203,7 +204,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorNetwork) {
|
||||
if err := c.collectNetwork(ch); err != nil {
|
||||
if err := c.collectNetwork(ch, maxScrapeDuration); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect network metrics: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -217,7 +218,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorNode) {
|
||||
var err error
|
||||
|
||||
nodeNames, err = c.collectNode(ch)
|
||||
nodeNames, err = c.collectNode(ch, maxScrapeDuration)
|
||||
if err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect node metrics: %w", err)
|
||||
}
|
||||
@@ -227,7 +228,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorResource) {
|
||||
if err := c.collectResource(ch, nodeNames); err != nil {
|
||||
if err := c.collectResource(ch, maxScrapeDuration, nodeNames); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect resource metrics: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -237,7 +238,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorResourceGroup) {
|
||||
if err := c.collectResourceGroup(ch, nodeNames); err != nil {
|
||||
if err := c.collectResourceGroup(ch, maxScrapeDuration, nodeNames); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect resource group metrics: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -248,13 +249,17 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorSharedVolumes) {
|
||||
if err := c.collectSharedVolumes(ch); err != nil {
|
||||
if err := c.collectSharedVolumes(ch, maxScrapeDuration); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect shared_volumes metrics: %w", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
if slices.Contains(c.config.CollectorsEnabled, subCollectorVirtualDisk) {
|
||||
if err := c.collectVirtualDisk(ch); err != nil {
|
||||
if err := c.collectVirtualDisk(ch, maxScrapeDuration); err != nil {
|
||||
errCh <- fmt.Errorf("failed to collect virtualdisk metrics: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/osversion"
|
||||
@@ -673,16 +674,16 @@ func (c *Collector) buildCluster() error {
|
||||
)
|
||||
|
||||
var dst []msClusterCluster
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.clusterMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.clusterMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) collectCluster(ch chan<- prometheus.Metric) error {
|
||||
func (c *Collector) collectCluster(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) error {
|
||||
var dst []msClusterCluster
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.clusterMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.clusterMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
@@ -90,7 +91,7 @@ func (c *Collector) buildNetwork() error {
|
||||
|
||||
var dst []msClusterNetwork
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.networkMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.networkMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
@@ -99,10 +100,10 @@ func (c *Collector) buildNetwork() error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus metric channel.
|
||||
func (c *Collector) collectNetwork(ch chan<- prometheus.Metric) error {
|
||||
func (c *Collector) collectNetwork(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) error {
|
||||
var dst []msClusterNetwork
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.networkMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.networkMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/osversion"
|
||||
@@ -170,7 +171,7 @@ func (c *Collector) buildNode() error {
|
||||
|
||||
var dst []msClusterNode
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.nodeMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.nodeMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
@@ -179,10 +180,10 @@ func (c *Collector) buildNode() error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) collectNode(ch chan<- prometheus.Metric) ([]string, error) {
|
||||
func (c *Collector) collectNode(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) ([]string, error) {
|
||||
var dst []msClusterNode
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.nodeMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.nodeMIQuery, maxScrapeDuration); err != nil {
|
||||
return nil, fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
@@ -194,7 +195,7 @@ func (c *Collector) buildResource() error {
|
||||
|
||||
var dst []msClusterResource
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
@@ -203,10 +204,10 @@ func (c *Collector) buildResource() error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) collectResource(ch chan<- prometheus.Metric, nodeNames []string) error {
|
||||
func (c *Collector) collectResource(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration, nodeNames []string) error {
|
||||
var dst []msClusterResource
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
@@ -168,7 +169,7 @@ func (c *Collector) buildResourceGroup() error {
|
||||
|
||||
var dst []msClusterResourceGroup
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceGroupMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceGroupMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
@@ -177,10 +178,10 @@ func (c *Collector) buildResourceGroup() error {
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *Collector) collectResourceGroup(ch chan<- prometheus.Metric, nodeNames []string) error {
|
||||
func (c *Collector) collectResourceGroup(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration, nodeNames []string) error {
|
||||
var dst []msClusterResourceGroup
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceGroupMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.resourceGroupMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ package mscluster
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
@@ -76,16 +77,16 @@ func (c *Collector) buildSharedVolumes() error {
|
||||
)
|
||||
|
||||
var dst []msClusterDiskPartition
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.sharedVolumesMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.sharedVolumesMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) collectSharedVolumes(ch chan<- prometheus.Metric) error {
|
||||
func (c *Collector) collectSharedVolumes(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) error {
|
||||
var dst []msClusterDiskPartition
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.sharedVolumesMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootMSCluster, c.sharedVolumesMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package mscluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/mi"
|
||||
"github.com/prometheus-community/windows_exporter/internal/types"
|
||||
@@ -92,13 +93,19 @@ func (c *Collector) buildVirtualDisk() error {
|
||||
nil,
|
||||
)
|
||||
|
||||
var dst []msftVirtualDisk
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootStorage, c.virtualDiskMIQuery, 0); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) collectVirtualDisk(ch chan<- prometheus.Metric) error {
|
||||
func (c *Collector) collectVirtualDisk(ch chan<- prometheus.Metric, maxScrapeDuration time.Duration) error {
|
||||
var dst []msftVirtualDisk
|
||||
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootStorage, c.virtualDiskMIQuery); err != nil {
|
||||
if err := c.miSession.Query(&dst, mi.NamespaceRootStorage, c.virtualDiskMIQuery, maxScrapeDuration); err != nil {
|
||||
return fmt.Errorf("WMI query failed: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user