Skip to content

Commit

Permalink
add trigger name as a label in prometheus metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Ying Liu <shatterly@gmail.com>
  • Loading branch information
Ying Liu committed Sep 8, 2022
1 parent 4e72030 commit 531c21e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
16 changes: 8 additions & 8 deletions pkg/metrics/prometheus_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

var (
metricLabels = []string{"namespace", "metric", "scaledObject", "scaler", "scalerIndex"}
metricLabels = []string{"namespace", "metric", "scaledObject", "scaler", "scalerIndex", "scalerName"}
scalerErrorsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "keda_metrics_adapter",
Expand Down Expand Up @@ -100,21 +100,21 @@ func (metricsServer PrometheusMetricServer) NewServer(address string, pattern st
}

// RecordHPAScalerMetric create a measurement of the external metric used by the HPA
func (metricsServer PrometheusMetricServer) RecordHPAScalerMetric(namespace string, scaledObject string, scaler string, scalerIndex int, metric string, value float64) {
scalerMetricsValue.With(getLabels(namespace, scaledObject, scaler, scalerIndex, metric)).Set(value)
func (metricsServer PrometheusMetricServer) RecordHPAScalerMetric(namespace string, scaledObject string, scaler string, scalerIndex int, scalerName string, metric string, value float64) {
scalerMetricsValue.With(getLabels(namespace, scaledObject, scaler, scalerIndex, scalerName, metric)).Set(value)
}

// RecordHPAScalerError counts the number of errors occurred in trying get an external metric used by the HPA
func (metricsServer PrometheusMetricServer) RecordHPAScalerError(namespace string, scaledObject string, scaler string, scalerIndex int, metric string, err error) {
func (metricsServer PrometheusMetricServer) RecordHPAScalerError(namespace string, scaledObject string, scaler string, scalerIndex int, scalerName string, metric string, err error) {
if err != nil {
scalerErrors.With(getLabels(namespace, scaledObject, scaler, scalerIndex, metric)).Inc()
scalerErrors.With(getLabels(namespace, scaledObject, scaler, scalerIndex, scalerName, metric)).Inc()
// scaledObjectErrors.With(prometheus.Labels{"namespace": namespace, "scaledObject": scaledObject}).Inc()
metricsServer.RecordScalerObjectError(namespace, scaledObject, err)
scalerErrorsTotal.With(prometheus.Labels{}).Inc()
return
}
// initialize metric with 0 if not already set
_, errscaler := scalerErrors.GetMetricWith(getLabels(namespace, scaledObject, scaler, scalerIndex, metric))
_, errscaler := scalerErrors.GetMetricWith(getLabels(namespace, scaledObject, scaler, scalerIndex, scalerName, metric))
if errscaler != nil {
log.Fatalf("Unable to write to serve custom metrics: %v", errscaler)
}
Expand All @@ -135,6 +135,6 @@ func (metricsServer PrometheusMetricServer) RecordScalerObjectError(namespace st
}
}

func getLabels(namespace string, scaledObject string, scaler string, scalerIndex int, metric string) prometheus.Labels {
return prometheus.Labels{"namespace": namespace, "scaledObject": scaledObject, "scaler": scaler, "scalerIndex": strconv.Itoa(scalerIndex), "metric": metric}
func getLabels(namespace string, scaledObject string, scaler string, scalerIndex int, scalerName string, metric string) prometheus.Labels {
return prometheus.Labels{"namespace": namespace, "scaledObject": scaledObject, "scaler": scaler, "scalerIndex": strconv.Itoa(scalerIndex), "scalerName": scalerName, "metric": metric}
}
14 changes: 7 additions & 7 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ func (p *KedaProvider) GetExternalMetric(ctx context.Context, namespace string,

scalerError := false

for scalerIndex, scaler := range cache.GetScalers() {
metricSpecs := scaler.GetMetricSpecForScaling(ctx)
scalerName := strings.Replace(fmt.Sprintf("%T", scaler), "*scalers.", "", 1)
for scalerIndex, scalerPair := range cache.GetScalers() {
metricSpecs := scalerPair.Scaler.GetMetricSpecForScaling(ctx)
scalerName := strings.Replace(fmt.Sprintf("%T", scalerPair.Scaler), "*scalers.", "", 1)
triggerName := scalerPair.TriggerName

for _, metricSpec := range metricSpecs {
// skip cpu/memory resource scaler
Expand All @@ -118,18 +119,17 @@ func (p *KedaProvider) GetExternalMetric(ctx context.Context, namespace string,
if strings.EqualFold(metricSpec.External.Metric.Name, info.Metric) {
metrics, err := cache.GetMetricsForScaler(ctx, scalerIndex, info.Metric, metricSelector)
metrics, err = p.getMetricsWithFallback(ctx, metrics, err, info.Metric, scaledObject, metricSpec)

if err != nil {
scalerError = true
logger.Error(err, "error getting metric for scaler", "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name, "scaler", scaler)
logger.Error(err, "error getting metric for scaler", "scaledObject.Namespace", scaledObject.Namespace, "scaledObject.Name", scaledObject.Name, "scaler", scalerPair.Scaler)
} else {
for _, metric := range metrics {
metricValue := metric.Value.AsApproximateFloat64()
metricsServer.RecordHPAScalerMetric(namespace, scaledObject.Name, scalerName, scalerIndex, metric.MetricName, metricValue)
metricsServer.RecordHPAScalerMetric(namespace, scaledObject.Name, scalerName, scalerIndex, triggerName, metric.MetricName, metricValue)
}
matchingMetrics = append(matchingMetrics, metrics...)
}
metricsServer.RecordHPAScalerError(namespace, scaledObject.Name, scalerName, scalerIndex, info.Metric, err)
metricsServer.RecordHPAScalerError(namespace, scaledObject.Name, scalerName, scalerIndex, triggerName, info.Metric, err)
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/scaling/cache/scalers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ type ScalersCache struct {
}

type ScalerBuilder struct {
Scaler scalers.Scaler
Factory func() (scalers.Scaler, error)
Scaler scalers.Scaler
TriggerName string
Factory func() (scalers.Scaler, error)
}

func (c *ScalersCache) GetScalers() []scalers.Scaler {
result := make([]scalers.Scaler, 0, len(c.Scalers))
type ScalerPair struct {
Scaler scalers.Scaler
TriggerName string
}

func (c *ScalersCache) GetScalers() []ScalerPair {
result := make([]ScalerPair, 0, len(c.Scalers))
for _, s := range c.Scalers {
result = append(result, s.Scaler)
result = append(result, ScalerPair{
Scaler: s.Scaler,
TriggerName: s.TriggerName,
})
}
return result
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/scaling/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,9 @@ func (h *scaleHandler) buildScalers(ctx context.Context, withTriggers *kedav1alp
}

result = append(result, cache.ScalerBuilder{
Scaler: scaler,
Factory: factory,
Scaler: scaler,
TriggerName: trigger.Name,
Factory: factory,
})
}

Expand Down

0 comments on commit 531c21e

Please sign in to comment.