service: parallelize api calls (#1744)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2024-11-18 02:07:03 +01:00
committed by GitHub
parent e6a15d4ec4
commit 9f29fc8a9c
2 changed files with 156 additions and 75 deletions

View File

@@ -5,6 +5,7 @@ package cpu
import (
"fmt"
"log/slog"
"sync"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/mi"
@@ -25,6 +26,8 @@ type Collector struct {
perfDataCollector *perfdata.Collector
mu sync.Mutex
processorRTCValues map[string]utils.Counter
processorMPerfValues map[string]utils.Counter
@@ -73,6 +76,8 @@ func (c *Collector) Close() error {
func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
var err error
c.mu = sync.Mutex{}
c.perfDataCollector, err = perfdata.NewCollector("Processor Information", perfdata.InstanceAll, []string{
c1TimeSeconds,
c2TimeSeconds,
@@ -219,6 +224,9 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
c.mu.Lock() // Lock is needed to prevent concurrent map access to c.processorRTCValues
defer c.mu.Unlock()
data, err := c.perfDataCollector.Collect()
if err != nil {
return fmt.Errorf("failed to collect Processor Information metrics: %w", err)