Skip to content

Commit

Permalink
feat: expose keda_scaler_active Prometheus metric (#4124)
Browse files Browse the repository at this point in the history
Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
  • Loading branch information
zroubalik authored Jan 16, 2023
1 parent 29b8af5 commit 8feb847
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Here is an overview of all **stable** additions:

- **General**: Introduce admission webhooks to automatically validate resource changes to prevent misconfiguration and enforce best practices ([#3755](https://github.com/kedacore/keda/issues/3755))
- **General**: Introduce new ArangoDB Scaler ([#4000](https://github.com/kedacore/keda/issues/4000))
- **Prometheus Metrics**: Introduce scaler activity in Prometheus metrics ([#4114](https://github.com/kedacore/keda/issues/4114))
- **Prometheus Metrics**: Introduce scaler latency in Prometheus metrics ([#4037](https://github.com/kedacore/keda/issues/4037))

Here is an overview of all new **experimental** features:
Expand Down
20 changes: 20 additions & 0 deletions pkg/prommetrics/prommetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ var (
},
metricLabels,
)
scalerActive = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: DefaultPromMetricsNamespace,
Subsystem: "scaler",
Name: "active",
Help: "Activity of a Scaler Metric",
},
metricLabels,
)
scalerErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: DefaultPromMetricsNamespace,
Expand Down Expand Up @@ -106,6 +115,7 @@ func init() {
metrics.Registry.MustRegister(scalerErrorsTotal)
metrics.Registry.MustRegister(scalerMetricsValue)
metrics.Registry.MustRegister(scalerMetricsLatency)
metrics.Registry.MustRegister(scalerActive)
metrics.Registry.MustRegister(scalerErrors)
metrics.Registry.MustRegister(scaledObjectErrors)

Expand All @@ -123,6 +133,16 @@ func RecordScalerLatency(namespace string, scaledObject string, scaler string, s
scalerMetricsLatency.With(getLabels(namespace, scaledObject, scaler, scalerIndex, metric)).Set(value)
}

// RecordScalerActive create a measurement of the activity of the scaler
func RecordScalerActive(namespace string, scaledObject string, scaler string, scalerIndex int, metric string, active bool) {
activeVal := 0
if active {
activeVal = 1
}

scalerActive.With(getLabels(namespace, scaledObject, scaler, scalerIndex, metric)).Set(float64(activeVal))
}

// RecordScalerError counts the number of errors occurred in trying get an external metric used by the HPA
func RecordScalerError(namespace string, scaledObject string, scaler string, scalerIndex int, metric string, err error) {
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/scaling/scale_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ func (h *scaleHandler) getScaledObjectState(ctx context.Context, scaledObject *k
}
}
prommetrics.RecordScalerError(scaledObject.Namespace, scaledObject.Name, scalerName, scalerIndex, metricName, err)
prommetrics.RecordScalerActive(scaledObject.Namespace, scaledObject.Name, scalerName, scalerIndex, metricName, isMetricActive)
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/internals/prometheus_metrics/prometheus_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestPrometheusMetrics(t *testing.T) {

testScalerMetricValue(t)
testScalerMetricLatency(t)
testScalerActiveMetric(t)
testMetricsServerScalerMetricValue(t)
testOperatorMetrics(t, kc, data)
testWebhookMetrics(t, data)
Expand Down Expand Up @@ -314,6 +315,29 @@ func testScalerMetricLatency(t *testing.T) {
}
}

func testScalerActiveMetric(t *testing.T) {
t.Log("--- testing scaler active metric ---")

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))

if val, ok := family["keda_scaler_active"]; ok {
var found bool
metrics := val.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == labelScaledObject && *label.Value == scaledObjectName {
assert.Equal(t, float64(1), *metric.Gauge.Value)
found = true
}
}
}
assert.Equal(t, true, found)
} else {
t.Errorf("metric not available")
}
}

// [DEPRECATED] handle exporting Prometheus metrics from Operator to Metrics Server
func testMetricsServerScalerMetricValue(t *testing.T) {
t.Log("--- testing scaler metric value in metrics server ---")
Expand Down

0 comments on commit 8feb847

Please sign in to comment.