Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
Add e2e test
  • Loading branch information
kevinteng525 committed Jan 6, 2023
1 parent e05020c commit d5c2bed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
15 changes: 0 additions & 15 deletions pkg/prommetrics/adapter/adapter_prommetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ var (
},
metricLabels,
)
scalerMetricsLatency = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "keda_metrics_adapter",
Subsystem: "scaler",
Name: "metrics_latency",
Help: "Scaler Metrics Latency",
},
metricLabels,
)
scalerErrors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "keda_metrics_adapter",
Expand Down Expand Up @@ -83,7 +74,6 @@ func init() {
registry = prometheus.NewRegistry()
registry.MustRegister(scalerErrorsTotal)
registry.MustRegister(scalerMetricsValue)
registry.MustRegister(scalerMetricsLatency)
registry.MustRegister(scalerErrors)
registry.MustRegister(scaledObjectErrors)
}
Expand Down Expand Up @@ -114,11 +104,6 @@ func (metricsServer PrometheusMetricServer) RecordHPAScalerMetric(namespace stri
scalerMetricsValue.With(getLabels(namespace, scaledObject, scaler, scalerIndex, metric)).Set(value)
}

// RecordHPAScalerLatency create a measurement of the latency to external metric
func (metricsServer PrometheusMetricServer) RecordHPAScalerLatency(namespace string, scaledObject string, scaler string, scalerIndex int, metric string, value float64) {
scalerMetricsLatency.With(getLabels(namespace, scaledObject, scaler, scalerIndex, 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) {
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/go-logr/logr"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -167,10 +166,7 @@ func (p *KedaProvider) GetExternalMetric(ctx context.Context, namespace string,
}
// Filter only the desired metric
if strings.EqualFold(metricSpec.External.Metric.Name, info.Metric) {
startTime := time.Now()
metrics, err := cache.GetMetricsForScaler(ctx, scalerIndex, info.Metric)
scalerLatency := time.Since(startTime).Milliseconds()
promMetricsServer.RecordHPAScalerLatency(namespace, scaledObject.Name, scalerName, scalerIndex, info.Metric, float64(scalerLatency))
metrics, err = fallback.GetMetricsWithFallback(ctx, p.client, logger, metrics, err, info.Metric, scaledObject, metricSpec)
if err != nil {
scalerError = true
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 @@ -225,6 +225,7 @@ func TestScaler(t *testing.T) {
"replica count should be 2 after 2 minute")

testScalerMetricValue(t)
testScalerMetricLatency(t)
testMetricsServerScalerMetricValue(t)
testOperatorMetrics(t, kc, data)

Expand Down Expand Up @@ -286,6 +287,29 @@ func testScalerMetricValue(t *testing.T) {
}
}

func testScalerMetricLatency(t *testing.T) {
t.Log("--- testing scaler metric latency ---")

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

if val, ok := family["keda_scaler_metrics_latency"]; ok {
var found bool
metrics := val.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == "scaledObject" && *label.Value == scaledObjectName {
assert.Equal(t, float64(0), *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 d5c2bed

Please sign in to comment.