From b810d92f44e55b0a4633c20baa51eba125ae75d9 Mon Sep 17 00:00:00 2001 From: kvisidisi Date: Wed, 16 Jul 2025 21:24:52 +0300 Subject: [PATCH 1/3] feat(collector): concurrent metrics collection --- internal/collector/collector.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/collector/collector.go b/internal/collector/collector.go index 4d4f31a..0c5533e 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -6,6 +6,7 @@ import ( "log/slog" "pg-bash-exporter/internal/cache" "pg-bash-exporter/internal/config" + "sync" "time" ) @@ -67,13 +68,22 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) { func (c *Collector) Collect(ch chan<- prometheus.Metric) { c.logger.Info("Metrics collection started") + wg := sync.WaitGroup{} + for _, metricConfig := range c.config.Metrics { - if len(metricConfig.SubMetrics) == 0 { - c.collectSimpleMetric(ch, metricConfig) - } else { - c.collectComplicatedMetric(ch, metricConfig) - } + wg.Add(1) + + go func(mc config.Metric) { + defer wg.Done() + if len(mc.SubMetrics) == 0 { + c.collectSimpleMetric(ch, mc) + } else { + c.collectComplicatedMetric(ch, mc) + } + }(metricConfig) } + wg.Wait() + c.logger.Info("Metrics collection finished") } From 50c6d5baa223be244fea3b84f83951878d868ec7 Mon Sep 17 00:00:00 2001 From: kvisidisi Date: Wed, 16 Jul 2025 21:30:27 +0300 Subject: [PATCH 2/3] feat(collector): add max goroutines from max concurrent config value --- internal/collector/collector.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/collector/collector.go b/internal/collector/collector.go index 0c5533e..8b38c39 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -70,11 +70,19 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { wg := sync.WaitGroup{} + smph := make(chan struct{}, c.config.Global.MaxConcurrent) + for _, metricConfig := range c.config.Metrics { wg.Add(1) go func(mc config.Metric) { defer wg.Done() + + smph <- struct{}{} + defer func() { + <-smph + }() + if len(mc.SubMetrics) == 0 { c.collectSimpleMetric(ch, mc) } else { From 84770aaa55a44bc8b9070fd228c728aab537c7c5 Mon Sep 17 00:00:00 2001 From: kvisidisi Date: Wed, 16 Jul 2025 21:36:46 +0300 Subject: [PATCH 3/3] fix(collector): some tests does not use config.Load so default values of max concurrent was 0. --- internal/collector/collector.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/collector/collector.go b/internal/collector/collector.go index 8b38c39..34dee2b 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -70,7 +70,11 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { wg := sync.WaitGroup{} - smph := make(chan struct{}, c.config.Global.MaxConcurrent) + maxConcurrent := c.config.Global.MaxConcurrent + if maxConcurrent <= 0 { + maxConcurrent = config.DefaultMaxConcurrent + } + smph := make(chan struct{}, maxConcurrent) for _, metricConfig := range c.config.Metrics { wg.Add(1)