Skip to content

Commit

Permalink
fix: Prometheus metrics expose correct info (#5779)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <[email protected]>
  • Loading branch information
JorTurFer authored May 6, 2024
1 parent 66e824d commit c7e901f
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/metricscollector/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func initMeters() {

_, err = meter.Float64ObservableGauge(
"keda.scaler.metrics.latency",
api.WithDescription("DEPRECATED - use `keda_scaler_metrics_latency_seconds` instead"),
api.WithDescription("DEPRECATED - use `keda.scaler.metrics.latency.seconds` instead"),
api.WithFloat64Callback(ScalerMetricsLatencyCallbackDeprecated),
)
if err != nil {
Expand All @@ -164,7 +164,7 @@ func initMeters() {

_, err = meter.Float64ObservableGauge(
"keda.internal.scale.loop.latency",
api.WithDescription("DEPRECATED - use `keda_internal_scale_loop_latency_seconds` instead"),
api.WithDescription("DEPRECATED - use `keda.internal.scale.loop.latency.seconds` instead"),
api.WithFloat64Callback(ScalableObjectLatencyCallbackDeprecated),
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metricscollector/prommetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ var (
Namespace: DefaultPromMetricsNamespace,
Subsystem: "resource",
Name: "totals",
Help: fmt.Sprintf("%v use 'keda_resource_handled_total' instead.", bestPracticeDeprecatedMsg),
Help: fmt.Sprintf("%v use 'keda_resource_registered_total' instead.", bestPracticeDeprecatedMsg),
},
[]string{"type", "namespace"},
)
Expand Down
105 changes: 103 additions & 2 deletions tests/sequential/opentelemetry_metrics/opentelemetry_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"fmt"
"maps"
"os/exec"
"strings"
"testing"
Expand Down Expand Up @@ -818,6 +819,24 @@ func testScalerMetricLatency(t *testing.T) {
}
assert.Equal(t, true, found)
}
val, ok = family["keda_scaler_metrics_latency_seconds"]
assert.True(t, ok, "keda_scaler_metrics_latency_seconds not available")
if ok {
var found bool
metrics := val.GetMetric()
for _, metric := range metrics {
t.Log("--- latency metric detail info ---", "metric", metric)
labels := metric.GetLabel()
for _, label := range labels {
if (*label.Name == labelScaledObject && *label.Value == scaledObjectName) ||
(*label.Name == labelScaledJob && *label.Value == scaledJobName) {
assert.InDelta(t, float64(0), *metric.Gauge.Value, 0.001)
found = true
}
}
}
assert.Equal(t, true, found)
}
}

func testScalableObjectMetrics(t *testing.T) {
Expand Down Expand Up @@ -855,6 +874,37 @@ func testScalableObjectMetrics(t *testing.T) {
}
assert.Equal(t, true, found)
}

val, ok = family["keda_internal_scale_loop_latency_seconds"]
assert.True(t, ok, "keda_internal_scale_loop_latency_seconds not available")
if ok {
var found bool
metrics := val.GetMetric()

// check scaledobject loop
found = false
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == labelType && *label.Value == "scaledobject" {
found = true
}
}
}
assert.Equal(t, true, found)

// check scaledjob loop
found = false
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == labelType && *label.Value == "scaledjob" {
found = true
}
}
}
assert.Equal(t, true, found)
}
}

func testScalerActiveMetric(t *testing.T, kc *kubernetes.Clientset) {
Expand Down Expand Up @@ -1013,15 +1063,16 @@ func getLatestCommit(t *testing.T) string {
return strings.Trim(out.String(), "\n")
}

func checkTriggerTotalValues(t *testing.T, families map[string]*prommodel.MetricFamily, expected map[string]int) {
func checkTriggerTotalValues(t *testing.T, families map[string]*prommodel.MetricFamily, expectedValues map[string]int) {
t.Log("--- testing trigger total metrics ---")
expected := map[string]int{}

family, ok := families["keda_trigger_totals"]
assert.True(t, ok, "keda_trigger_totals not available")
if !ok {
return
}

maps.Copy(expected, expectedValues)
metrics := family.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
Expand All @@ -1040,6 +1091,31 @@ func checkTriggerTotalValues(t *testing.T, families map[string]*prommodel.Metric
}

assert.Equal(t, 0, len(expected))

family, ok = families["keda_trigger_registered_count"]
assert.True(t, ok, "keda_trigger_registered_count not available")
if !ok {
return
}
maps.Copy(expected, expectedValues)
metrics = family.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == labelType {
triggerType := *label.Value
metricValue := *metric.Gauge.Value
expectedMetricValue := float64(expected[triggerType])

assert.Equalf(t, expectedMetricValue, metricValue, "expected %f got %f for trigger type %s",
expectedMetricValue, metricValue, triggerType)

delete(expected, triggerType)
}
}
}

assert.Equal(t, 0, len(expected))
}

func checkCRTotalValues(t *testing.T, families map[string]*prommodel.MetricFamily, expected map[string]map[string]int) {
Expand Down Expand Up @@ -1069,6 +1145,31 @@ func checkCRTotalValues(t *testing.T, families map[string]*prommodel.MetricFamil
assert.Equalf(t, expectedMetricValue, metricValue, "expected %f got %f for cr type %s & namespace %s",
expectedMetricValue, metricValue, crType, namespace)
}

family, ok = families["keda_resource_registered_count"]
assert.True(t, ok, "keda_resource_registered_count not available")
if !ok {
return
}

metrics = family.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
var namespace, crType string
for _, label := range labels {
if *label.Name == labelType {
crType = *label.Value
} else if *label.Name == namespaceString {
namespace = *label.Value
}
}

metricValue := *metric.Gauge.Value
expectedMetricValue := float64(expected[crType][namespace])

assert.Equalf(t, expectedMetricValue, metricValue, "expected %f got %f for cr type %s & namespace %s",
expectedMetricValue, metricValue, crType, namespace)
}
}

func assertScaledObjectFlagMetric(t *testing.T, families map[string]*prommodel.MetricFamily, scaledObjectName string, metricName string, expected bool) {
Expand Down
Loading

0 comments on commit c7e901f

Please sign in to comment.