Skip to content

Commit

Permalink
feat(golang): add metric multiplier #42
Browse files Browse the repository at this point in the history
  • Loading branch information
jplanckeel authored Aug 17, 2022
2 parents bf33a2e + 1c45ce2 commit 4224c8a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type prescalingCollector struct {
prescaleMetrics *prometheus.Desc
minMetrics *prometheus.Desc
multiplierMetrics *prometheus.Desc
prescaling prescaling.IPrescaling
}

Expand All @@ -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,
}
Expand All @@ -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 {
Expand All @@ -48,23 +56,29 @@ 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)
minMetric := prometheus.MustNewConstMetric(collector.minMetrics, prometheus.GaugeValue, float64(hpa.Replica), hpa.Project, hpa.Deployment, hpa.Namespace)
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
}

0 comments on commit 4224c8a

Please sign in to comment.