From 8d7c0c5a06d166ff0249d85926276c2eeaf2d8eb Mon Sep 17 00:00:00 2001 From: yomek33 Date: Sat, 20 Jul 2024 07:51:49 +0900 Subject: [PATCH] Update metrics handling logic Signed-off-by: yomek33 --- metrics/serve.go | 69 ++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/metrics/serve.go b/metrics/serve.go index 967af4f..49702e1 100644 --- a/metrics/serve.go +++ b/metrics/serve.go @@ -160,55 +160,46 @@ func changeSeriesDoubleHalve(currentSeriesCount *int, seriesIncrease *bool) { func handleDoubleHalveMode(metricCount, metricLength, metricCycle, seriesCycle int, labelKeys, labelValues []string, seriesCount int, changeSeriesTick *time.Ticker, updateNotify chan struct{}) { currentSeriesCount := seriesCount seriesIncrease := true + for tick := range changeSeriesTick.C { + metricsMux.Lock() + unregisterMetrics() + registerMetrics(metricCount, metricLength, metricCycle, labelKeys) + cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) + metricsMux.Unlock() - registerMetrics(metricCount, metricLength, metricCycle, labelKeys) - cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) - - go func() { - for tick := range changeSeriesTick.C { - metricsMux.Lock() - unregisterMetrics() - registerMetrics(metricCount, metricLength, metricCycle, labelKeys) - cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) - metricsMux.Unlock() - - changeSeriesDoubleHalve(¤tSeriesCount, &seriesIncrease) + changeSeriesDoubleHalve(¤tSeriesCount, &seriesIncrease) - fmt.Printf("%v: Adjusting series count. New count: %d\n", tick, currentSeriesCount) + fmt.Printf("%v: Adjusting series count. New count: %d\n", tick, currentSeriesCount) - select { - case updateNotify <- struct{}{}: - default: - } + select { + case updateNotify <- struct{}{}: + default: } - }() + } + } func handleGradualChangeMode(metricCount, metricLength, metricCycle, seriesCycle int, labelKeys, labelValues []string, seriesChangeRate, maxSeriesCount, minSeriesCount int, changeSeriesTick *time.Ticker, updateNotify chan struct{}) { currentSeriesCount := minSeriesCount seriesIncrease := true - registerMetrics(metricCount, metricLength, metricCycle, labelKeys) - cycleValues(labelKeys, labelValues, minSeriesCount, seriesCycle) - - go func() { - for tick := range changeSeriesTick.C { - metricsMux.Lock() - unregisterMetrics() - registerMetrics(metricCount, metricLength, metricCycle, labelKeys) - cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) - metricsMux.Unlock() + for tick := range changeSeriesTick.C { + metricsMux.Lock() + unregisterMetrics() + registerMetrics(metricCount, metricLength, metricCycle, labelKeys) + cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) + metricsMux.Unlock() - changeSeriesGradual(&seriesChangeRate, &maxSeriesCount, &minSeriesCount, ¤tSeriesCount, &seriesIncrease) + changeSeriesGradual(&seriesChangeRate, &maxSeriesCount, &minSeriesCount, ¤tSeriesCount, &seriesIncrease) - fmt.Printf("%v: Adjusting series count. New count: %d\n", tick, currentSeriesCount) + fmt.Printf("%v: Adjusting series count. New count: %d\n", tick, currentSeriesCount) - select { - case updateNotify <- struct{}{}: - default: - } + select { + case updateNotify <- struct{}{}: + default: } - }() + } + } // RunMetrics creates a set of Prometheus test series that update over time @@ -242,7 +233,9 @@ func RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, maxSerie switch seriesOperationMode { case "double-halve": - handleDoubleHalveMode(metricCount, metricLength, metricCycle, seriesCycle, labelKeys, labelValues, seriesCount, changeSeriesTick, updateNotify) + registerMetrics(metricCount, metricLength, metricCycle, labelKeys) + cycleValues(labelKeys, labelValues, currentSeriesCount, seriesCycle) + go handleDoubleHalveMode(metricCount, metricLength, metricCycle, seriesCycle, labelKeys, labelValues, seriesCount, changeSeriesTick, updateNotify) go handleValueTicks(&labelKeys, &labelValues, ¤tSeriesCount, &seriesCycle, updateNotify, valueTick) go handleSeriesTicks(&labelKeys, &labelValues, ¤tSeriesCount, &seriesCycle, updateNotify, seriesTick) @@ -250,7 +243,9 @@ func RunMetrics(metricCount, labelCount, seriesCount, seriesChangeRate, maxSerie if minSeriesCount >= maxSeriesCount { return nil, fmt.Errorf("error: minSeriesCount must be less than maxSeriesCount, got %d and %d", minSeriesCount, maxSeriesCount) } - handleGradualChangeMode(metricCount, metricLength, metricCycle, seriesCycle, labelKeys, labelValues, seriesChangeRate, maxSeriesCount, minSeriesCount, changeSeriesTick, updateNotify) + registerMetrics(metricCount, metricLength, metricCycle, labelKeys) + cycleValues(labelKeys, labelValues, minSeriesCount, seriesCycle) + go handleGradualChangeMode(metricCount, metricLength, metricCycle, seriesCycle, labelKeys, labelValues, seriesChangeRate, maxSeriesCount, minSeriesCount, changeSeriesTick, updateNotify) go handleValueTicks(&labelKeys, &labelValues, ¤tSeriesCount, &seriesCycle, updateNotify, valueTick) go handleSeriesTicks(&labelKeys, &labelValues, ¤tSeriesCount, &seriesCycle, updateNotify, seriesTick)