From 1c45ce26870e005b14219830bc87c589831d22fe Mon Sep 17 00:00:00 2001 From: Jeremy PLANCKEEL Date: Fri, 12 Aug 2022 09:58:49 +0200 Subject: [PATCH] feat(golang): add metric multiplier --- pkg/exporter/exporter.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index f331210..8b64dcf 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -11,6 +11,7 @@ import ( type prescalingCollector struct { prescaleMetrics *prometheus.Desc minMetrics *prometheus.Desc + multiplierMetrics *prometheus.Desc prescaling prescaling.IPrescaling } @@ -26,6 +27,11 @@ func NewPrescalingCollector(p prescaling.IPrescaling) prometheus.Collector { "Number of pod desired for prescale", []string{"project", "deployment", "namespace"}, nil, + ), multiplierMetrics: prometheus.NewDesc( + "prescaling_multiplier", + "Multiplying factor of min replica", + []string{}, + nil, ), prescaling: p, } @@ -34,9 +40,11 @@ func NewPrescalingCollector(p prescaling.IPrescaling) prometheus.Collector { func (collector *prescalingCollector) Describe(ch chan<- *prometheus.Desc) { ch <- collector.prescaleMetrics ch <- collector.minMetrics + ch <- collector.multiplierMetrics } func (collector *prescalingCollector) Collect(ch chan<- prometheus.Metric) { + var multiplier int = 1 log.Info("Collect") hpaList := collector.prescaling.GetHpa() if len(hpaList) == 0 { @@ -48,19 +56,20 @@ func (collector *prescalingCollector) Collect(ch chan<- prometheus.Metric) { now := collector.prescaling.GetEventService().GetClock().Now() for _, hpa := range hpaList { - multiplier := 1 if err == nil && currentPrescalingEvent.StartTime != "" && currentPrescalingEvent.EndTime != "" { hpa.Start, _ = utils.SetTime(currentPrescalingEvent.StartTime, now) hpa.End, _ = utils.SetTime(currentPrescalingEvent.EndTime, now) multiplier = currentPrescalingEvent.Multiplier } - collector.addDataToMetrics(ch, multiplier, hpa) + collector.addHpaDataToMetrics(ch, multiplier, hpa) } + collector.addDataToMetrics(ch, multiplier) + } -func (collector *prescalingCollector) addDataToMetrics(ch chan<- prometheus.Metric, multiplier int, hpa prescaling.Hpa) { +func (collector *prescalingCollector) addHpaDataToMetrics(ch chan<- prometheus.Metric, multiplier int, hpa prescaling.Hpa) { eventInRangeTime := utils.InRangeTime(hpa.Start, hpa.End, collector.prescaling.GetEventService().GetClock().Now()) desiredScalingType := prescaling.DesiredScaling(eventInRangeTime, multiplier, hpa.Replica, hpa.CurrentReplicas) prescaleMetric := prometheus.MustNewConstMetric(collector.prescaleMetrics, prometheus.GaugeValue, float64(desiredScalingType), hpa.Project, hpa.Deployment, hpa.Namespace) @@ -68,3 +77,8 @@ func (collector *prescalingCollector) addDataToMetrics(ch chan<- prometheus.Metr ch <- prescaleMetric ch <- minMetric } + +func (collector *prescalingCollector) addDataToMetrics(ch chan<- prometheus.Metric, multiplier int) { + multiplierMetric := prometheus.MustNewConstMetric(collector.multiplierMetrics, prometheus.GaugeValue, float64(multiplier)) + ch <- multiplierMetric +}