2023-04-17 09:37:13 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2024-09-27 09:09:17 +00:00
|
|
|
"context"
|
2023-04-17 09:37:13 +00:00
|
|
|
"fmt"
|
2024-09-10 22:34:10 +00:00
|
|
|
"log/slog"
|
2024-09-28 13:57:56 +00:00
|
|
|
"runtime/debug"
|
2023-04-17 09:37:13 +00:00
|
|
|
"sync"
|
2024-09-10 22:34:10 +00:00
|
|
|
"sync/atomic"
|
2023-04-17 09:37:13 +00:00
|
|
|
"time"
|
|
|
|
|
2024-10-03 18:34:45 +00:00
|
|
|
"github.com/prometheus-community/windows_exporter/internal/types"
|
2023-04-17 09:37:13 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Interface guard.
|
|
|
|
var _ prometheus.Collector = (*Prometheus)(nil)
|
|
|
|
|
|
|
|
// Prometheus implements prometheus.Collector for a set of Windows MetricCollectors.
|
2023-04-17 09:37:13 +00:00
|
|
|
type Prometheus struct {
|
|
|
|
maxScrapeDuration time.Duration
|
2024-09-10 22:34:10 +00:00
|
|
|
logger *slog.Logger
|
|
|
|
metricCollectors *MetricCollectors
|
2023-11-04 19:51:35 +00:00
|
|
|
|
|
|
|
// Base metrics returned by Prometheus
|
2024-09-10 22:34:10 +00:00
|
|
|
scrapeDurationDesc *prometheus.Desc
|
|
|
|
collectorScrapeDurationDesc *prometheus.Desc
|
|
|
|
collectorScrapeSuccessDesc *prometheus.Desc
|
|
|
|
collectorScrapeTimeoutDesc *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
type collectorStatus struct {
|
|
|
|
name string
|
|
|
|
statusCode collectorStatusCode
|
2023-04-17 09:37:13 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
type collectorStatusCode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
pending collectorStatusCode = iota
|
|
|
|
success
|
|
|
|
failed
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewPrometheusCollector returns a new Prometheus where the set of MetricCollectors must
|
2023-04-17 09:37:13 +00:00
|
|
|
// return metrics within the given timeout.
|
2024-09-10 22:34:10 +00:00
|
|
|
func (c *MetricCollectors) NewPrometheusCollector(timeout time.Duration, logger *slog.Logger) *Prometheus {
|
2023-04-17 09:37:13 +00:00
|
|
|
return &Prometheus{
|
|
|
|
maxScrapeDuration: timeout,
|
2024-09-10 22:34:10 +00:00
|
|
|
metricCollectors: c,
|
2023-04-22 10:17:51 +00:00
|
|
|
logger: logger,
|
2023-11-04 19:51:35 +00:00
|
|
|
scrapeDurationDesc: prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, "exporter", "scrape_duration_seconds"),
|
2024-09-10 22:34:10 +00:00
|
|
|
"windows_exporter: Total scrape duration.",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
collectorScrapeDurationDesc: prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, "exporter", "collector_duration_seconds"),
|
2023-11-04 19:51:35 +00:00
|
|
|
"windows_exporter: Duration of a collection.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
),
|
2024-09-10 22:34:10 +00:00
|
|
|
collectorScrapeSuccessDesc: prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, "exporter", "collector_success"),
|
2023-11-04 19:51:35 +00:00
|
|
|
"windows_exporter: Whether the collector was successful.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
),
|
2024-09-10 22:34:10 +00:00
|
|
|
collectorScrapeTimeoutDesc: prometheus.NewDesc(
|
2024-10-03 18:34:45 +00:00
|
|
|
prometheus.BuildFQName(types.Namespace, "exporter", "collector_timeout"),
|
2023-11-04 19:51:35 +00:00
|
|
|
"windows_exporter: Whether the collector timed out.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
),
|
2023-04-17 09:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
func (p *Prometheus) Describe(_ chan<- *prometheus.Desc) {}
|
2023-04-17 09:37:13 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Collect sends the collected metrics from each of the MetricCollectors to
|
2023-04-17 09:37:13 +00:00
|
|
|
// prometheus.
|
2024-09-10 22:34:10 +00:00
|
|
|
func (p *Prometheus) Collect(ch chan<- prometheus.Metric) {
|
2024-11-22 20:10:00 +00:00
|
|
|
collectorStartTime := time.Now()
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
// WaitGroup to wait for all collectors to finish
|
2023-04-17 09:37:13 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2024-09-10 22:34:10 +00:00
|
|
|
wg.Add(len(p.metricCollectors.Collectors))
|
2023-04-17 09:37:13 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Using a channel to collect the status of each collector
|
|
|
|
// A channel is safe to use concurrently while a map is not
|
|
|
|
collectorStatusCh := make(chan collectorStatus, len(p.metricCollectors.Collectors))
|
2023-04-17 09:37:13 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Execute all collectors concurrently
|
|
|
|
// timeout handling is done in the execute function
|
|
|
|
for name, metricsCollector := range p.metricCollectors.Collectors {
|
|
|
|
go func(name string, metricsCollector Collector) {
|
2023-04-17 09:37:13 +00:00
|
|
|
defer wg.Done()
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
collectorStatusCh <- collectorStatus{
|
|
|
|
name: name,
|
2024-11-17 20:51:12 +00:00
|
|
|
statusCode: p.execute(name, metricsCollector, ch),
|
2023-04-17 09:37:13 +00:00
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
}(name, metricsCollector)
|
2023-04-17 09:37:13 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Wait for all collectors to finish
|
|
|
|
wg.Wait()
|
2023-04-17 09:37:13 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Close the channel since we are done writing to it
|
|
|
|
close(collectorStatusCh)
|
2023-04-17 09:37:13 +00:00
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
for status := range collectorStatusCh {
|
2023-04-17 09:37:13 +00:00
|
|
|
var successValue, timeoutValue float64
|
2024-09-10 22:34:10 +00:00
|
|
|
if status.statusCode == pending {
|
2023-04-17 09:37:13 +00:00
|
|
|
timeoutValue = 1.0
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
if status.statusCode == success {
|
2023-04-17 09:37:13 +00:00
|
|
|
successValue = 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2024-09-10 22:34:10 +00:00
|
|
|
p.collectorScrapeSuccessDesc,
|
2023-04-17 09:37:13 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
successValue,
|
2024-09-10 22:34:10 +00:00
|
|
|
status.name,
|
2023-04-17 09:37:13 +00:00
|
|
|
)
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-04-17 09:37:13 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2024-09-10 22:34:10 +00:00
|
|
|
p.collectorScrapeTimeoutDesc,
|
2023-04-17 09:37:13 +00:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
timeoutValue,
|
2024-09-10 22:34:10 +00:00
|
|
|
status.name,
|
2023-04-17 09:37:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2024-09-10 22:34:10 +00:00
|
|
|
p.scrapeDurationDesc,
|
2023-04-17 09:37:13 +00:00
|
|
|
prometheus.GaugeValue,
|
2024-11-22 20:10:00 +00:00
|
|
|
time.Since(collectorStartTime).Seconds(),
|
2023-04-17 09:37:13 +00:00
|
|
|
)
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
func (p *Prometheus) execute(name string, c Collector, ch chan<- prometheus.Metric) collectorStatusCode {
|
2024-09-10 22:34:10 +00:00
|
|
|
var (
|
2024-09-27 09:09:17 +00:00
|
|
|
err error
|
|
|
|
numMetrics int
|
|
|
|
duration time.Duration
|
|
|
|
timeout atomic.Bool
|
2024-09-10 22:34:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// bufCh is a buffer channel to store the metrics
|
|
|
|
// This is needed because once timeout is reached, the prometheus registry channel is closed.
|
2024-09-27 09:09:17 +00:00
|
|
|
bufCh := make(chan prometheus.Metric, 1000)
|
2024-09-10 22:34:10 +00:00
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
2024-09-27 09:09:17 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), p.maxScrapeDuration)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
// Execute the collector
|
|
|
|
go func() {
|
2024-09-27 09:09:17 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2024-10-03 12:31:44 +00:00
|
|
|
errCh <- fmt.Errorf("panic in collector %s: %v. stack: %s", name, r,
|
|
|
|
string(debug.Stack()),
|
2024-09-27 09:09:17 +00:00
|
|
|
)
|
|
|
|
}
|
2024-10-03 12:31:44 +00:00
|
|
|
|
|
|
|
close(bufCh)
|
2024-09-27 09:09:17 +00:00
|
|
|
}()
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
errCh <- c.Collect(bufCh)
|
2024-09-10 22:34:10 +00:00
|
|
|
}()
|
|
|
|
|
2024-09-27 09:09:17 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
// This prevents a panic from race-condition when closing the ch channel too early.
|
|
|
|
_ = recover()
|
2024-09-27 09:09:17 +00:00
|
|
|
|
|
|
|
wg.Done()
|
2024-09-10 22:34:10 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Pass metrics to the prometheus registry
|
|
|
|
// If timeout is reached, the channel is closed.
|
|
|
|
// This will cause a panic if we try to write to it.
|
2024-09-27 09:09:17 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case m, ok := <-bufCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !timeout.Load() {
|
|
|
|
ch <- m
|
|
|
|
|
|
|
|
numMetrics++
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
// Wait for the collector to finish or timeout
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
2024-09-27 09:09:17 +00:00
|
|
|
wg.Wait() // Wait for the buffer channel to be closed and empty
|
|
|
|
|
2024-09-10 22:34:10 +00:00
|
|
|
duration = time.Since(t)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
p.collectorScrapeDurationDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
duration.Seconds(),
|
|
|
|
name,
|
|
|
|
)
|
2024-09-27 09:09:17 +00:00
|
|
|
case <-ctx.Done():
|
2024-09-10 22:34:10 +00:00
|
|
|
timeout.Store(true)
|
|
|
|
|
|
|
|
duration = time.Since(t)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
p.collectorScrapeDurationDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
duration.Seconds(),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
|
2024-09-27 09:09:17 +00:00
|
|
|
p.logger.Warn(fmt.Sprintf("collector %s timeouted after %s, resulting in %d metrics", name, p.maxScrapeDuration, numMetrics))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Drain channel in case of premature return to not leak a goroutine.
|
|
|
|
//nolint:revive
|
|
|
|
for range bufCh {
|
|
|
|
}
|
|
|
|
}()
|
2024-09-10 22:34:10 +00:00
|
|
|
|
|
|
|
return pending
|
|
|
|
}
|
2023-04-17 09:37:13 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-09-27 09:09:17 +00:00
|
|
|
p.logger.Error(fmt.Sprintf("collector %s failed after %s, resulting in %d metrics", name, duration, numMetrics),
|
2024-09-10 22:34:10 +00:00
|
|
|
slog.Any("err", err),
|
|
|
|
)
|
|
|
|
|
2023-04-17 09:37:13 +00:00
|
|
|
return failed
|
|
|
|
}
|
2024-08-29 20:03:05 +00:00
|
|
|
|
2024-09-27 10:01:30 +00:00
|
|
|
p.logger.Debug(fmt.Sprintf("collector %s succeeded after %s, resulting in %d metrics", name, duration, numMetrics))
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-04-17 09:37:13 +00:00
|
|
|
return success
|
|
|
|
}
|