Skip to content

Commit

Permalink
get back deprecated opentelemetry metrics keda_internal_scale_loop_la…
Browse files Browse the repository at this point in the history
…tency and keda_scaler_metrics_latency

Signed-off-by: Jan Wozniak <[email protected]>
  • Loading branch information
wozniakjan committed Apr 15, 2024
1 parent c60020b commit db27705
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
46 changes: 42 additions & 4 deletions pkg/metricscollector/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ var (
otTriggerRegisteredTotalsCounter api.Int64UpDownCounter
otCrdRegisteredTotalsCounter api.Int64UpDownCounter

otelScalerMetricVal OtelMetricFloat64Val
otelScalerMetricsLatencyVal OtelMetricFloat64Val
otelInternalLoopLatencyVal OtelMetricFloat64Val
otelBuildInfoVal OtelMetricInt64Val
otelScalerMetricVal OtelMetricFloat64Val
otelScalerMetricsLatencyVal OtelMetricFloat64Val
otelScalerMetricsLatencyValDeprecated OtelMetricFloat64Val
otelInternalLoopLatencyVal OtelMetricFloat64Val
otelInternalLoopLatencyValDeprecated OtelMetricFloat64Val
otelBuildInfoVal OtelMetricInt64Val

otCloudEventEmittedCounter api.Int64Counter
otCloudEventQueueStatusVal OtelMetricFloat64Val
Expand Down Expand Up @@ -129,6 +131,14 @@ func initMeters() {

_, err = meter.Float64ObservableGauge(
"keda.scaler.metrics.latency",
api.WithDescription("DEPRECATED - use `keda_scaler_metrics_latency_seconds` instead"),
api.WithFloat64Callback(ScalerMetricsLatencyCallbackDeprecated),
)
if err != nil {
otLog.Error(err, msg)
}
_, err = meter.Float64ObservableGauge(
"keda.scaler.metrics.latency.seconds",
api.WithDescription("The latency of retrieving current metric from each scaler"),
api.WithUnit("s"),
api.WithFloat64Callback(ScalerMetricsLatencyCallback),
Expand All @@ -139,6 +149,14 @@ func initMeters() {

_, err = meter.Float64ObservableGauge(
"keda.internal.scale.loop.latency",
api.WithDescription("DEPRECATED - use `keda_internal_scale_loop_latency_seconds` instead"),
api.WithFloat64Callback(ScalableObjectLatencyCallbackDeprecated),
)
if err != nil {
otLog.Error(err, msg)
}
_, err = meter.Float64ObservableGauge(
"keda.internal.scale.loop.latency.seconds",
api.WithDescription("Internal latency of ScaledObject/ScaledJob loop execution"),
api.WithUnit("s"),
api.WithFloat64Callback(ScalableObjectLatencyCallback),
Expand Down Expand Up @@ -222,10 +240,20 @@ func ScalerMetricsLatencyCallback(_ context.Context, obsrv api.Float64Observer)
return nil
}

func ScalerMetricsLatencyCallbackDeprecated(_ context.Context, obsrv api.Float64Observer) error {
if otelScalerMetricsLatencyValDeprecated.measurementOption != nil {
obsrv.Observe(otelScalerMetricsLatencyValDeprecated.val, otelScalerMetricsLatencyValDeprecated.measurementOption)
}
otelScalerMetricsLatencyValDeprecated = OtelMetricFloat64Val{}
return nil
}

// RecordScalerLatency create a measurement of the latency to external metric
func (o *OtelMetrics) RecordScalerLatency(namespace string, scaledResource string, scaler string, triggerIndex int, metric string, isScaledObject bool, value time.Duration) {
otelScalerMetricsLatencyVal.val = value.Seconds()
otelScalerMetricsLatencyVal.measurementOption = getScalerMeasurementOption(namespace, scaledResource, scaler, triggerIndex, metric, isScaledObject)
otelScalerMetricsLatencyValDeprecated.val = float64(value.Milliseconds())
otelScalerMetricsLatencyValDeprecated.measurementOption = getScalerMeasurementOption(namespace, scaledResource, scaler, triggerIndex, metric, isScaledObject)
}

func ScalableObjectLatencyCallback(_ context.Context, obsrv api.Float64Observer) error {
Expand All @@ -236,6 +264,14 @@ func ScalableObjectLatencyCallback(_ context.Context, obsrv api.Float64Observer)
return nil
}

func ScalableObjectLatencyCallbackDeprecated(_ context.Context, obsrv api.Float64Observer) error {
if otelInternalLoopLatencyValDeprecated.measurementOption != nil {
obsrv.Observe(otelInternalLoopLatencyValDeprecated.val, otelInternalLoopLatencyValDeprecated.measurementOption)
}
otelInternalLoopLatencyValDeprecated = OtelMetricFloat64Val{}
return nil
}

// RecordScalableObjectLatency create a measurement of the latency executing scalable object loop
func (o *OtelMetrics) RecordScalableObjectLatency(namespace string, name string, isScaledObject bool, value time.Duration) {
resourceType := "scaledjob"
Expand All @@ -250,6 +286,8 @@ func (o *OtelMetrics) RecordScalableObjectLatency(namespace string, name string,

otelInternalLoopLatencyVal.val = value.Seconds()
otelInternalLoopLatencyVal.measurementOption = opt
otelInternalLoopLatencyValDeprecated.val = float64(value.Milliseconds())
otelInternalLoopLatencyValDeprecated.measurementOption = opt
}

func ScalerActiveCallback(_ context.Context, obsrv api.Float64Observer) error {
Expand Down
11 changes: 8 additions & 3 deletions pkg/metricscollector/opentelemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ func TestLoopLatency(t *testing.T) {
assert.Nil(t, err)
scopeMetrics := got.ScopeMetrics[0]
assert.NotEqual(t, len(scopeMetrics.Metrics), 0)
latency := retrieveMetric(scopeMetrics.Metrics, "keda.internal.scale.loop.latency")

latency := retrieveMetric(scopeMetrics.Metrics, "keda.internal.scale.loop.latency")
assert.NotNil(t, latency)
assert.Equal(t, latency.Unit, "s")

assert.Equal(t, latency.Unit, "")
data := latency.Data.(metricdata.Gauge[float64]).DataPoints[0]
assert.Equal(t, data.Value, float64(500))

latencySeconds := retrieveMetric(scopeMetrics.Metrics, "keda.internal.scale.loop.latency.seconds")
assert.NotNil(t, latencySeconds)
assert.Equal(t, latency.Unit, "s")
data = latency.Data.(metricdata.Gauge[float64]).DataPoints[0]
assert.Equal(t, data.Value, float64(0.5))
}

0 comments on commit db27705

Please sign in to comment.