Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): histogram metric for loop latency #5812

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/metricscollector/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
otelScalerMetricsLatencyVals []OtelMetricFloat64Val
otelScalerMetricsLatencyValDeprecated []OtelMetricFloat64Val
otelInternalLoopLatencyVals []OtelMetricFloat64Val
otelUnterlanLoopLatencyValHistogram []OtelMetricFloat64Val
otelInternalLoopLatencyValDeprecated []OtelMetricFloat64Val
otelBuildInfoVal OtelMetricInt64Val

Expand Down Expand Up @@ -170,6 +171,7 @@ func initMeters() {
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"),
Expand All @@ -180,6 +182,15 @@ func initMeters() {
otLog.Error(err, msg)
}

_, err = meter.Float64Histogram(
"keda.internal.scale.loop.latency.bucket",
api.WithDescription("Internal latency of ScaledObject/ScaledJob loop execution"),
api.WithUnit("s"),
)
if err != nil {
otLog.Error(err, msg)
}

JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
_, err = meter.Float64ObservableGauge(
"keda.scaler.active",
api.WithDescription("Indicates whether a scaler is active (1), or not (0)"),
Expand Down Expand Up @@ -324,6 +335,11 @@ func (o *OtelMetrics) RecordScalableObjectLatency(namespace string, name string,
otelInternalLoopLatencyD.val = float64(value.Milliseconds())
otelInternalLoopLatencyD.measurementOption = opt
otelInternalLoopLatencyValDeprecated = append(otelInternalLoopLatencyValDeprecated, otelInternalLoopLatencyD)

otelInternalLoopLatencyHistogram := OtelMetricFloat64Val{}
otelInternalLoopLatencyHistogram.val = value.Seconds()
otelInternalLoopLatencyHistogram.measurementOption = opt
otelUnterlanLoopLatencyValHistogram = append(otelUnterlanLoopLatencyValHistogram, otelInternalLoopLatencyHistogram)
}

func ScalerActiveCallback(_ context.Context, obsrv api.Float64Observer) error {
Expand Down
12 changes: 12 additions & 0 deletions pkg/metricscollector/prommetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ var (
[]string{"namespace", "type", "resource"},
)

internalLoopLatencyHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: DefaultPromMetricsNamespace,
Subsystem: "internal_scale_loop",
Name: "latency_seconds_bucket",
Help: "Total deviation (in seconds) between the expected execution time and the actual execution time for the scaling loop. Represented as a histogram.",
},
[]string{"namespace", "type", "resource"},
)

// Total emitted cloudevents.
cloudeventEmitted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand Down Expand Up @@ -242,6 +252,7 @@ func NewPromMetrics() *PromMetrics {
metrics.Registry.MustRegister(scalerMetricsLatency)
metrics.Registry.MustRegister(internalLoopLatencyDeprecated)
metrics.Registry.MustRegister(internalLoopLatency)
metrics.Registry.MustRegister(internalLoopLatencyHistogram)
metrics.Registry.MustRegister(scalerActive)
metrics.Registry.MustRegister(scalerErrorsDeprecated)
metrics.Registry.MustRegister(scalerErrors)
Expand Down Expand Up @@ -284,6 +295,7 @@ func (p *PromMetrics) RecordScalerLatency(namespace string, scaledResource strin
func (p *PromMetrics) RecordScalableObjectLatency(namespace string, name string, isScaledObject bool, value time.Duration) {
internalLoopLatency.WithLabelValues(namespace, getResourceType(isScaledObject), name).Set(value.Seconds())
internalLoopLatencyDeprecated.WithLabelValues(namespace, getResourceType(isScaledObject), name).Set(float64(value.Milliseconds()))
internalLoopLatencyHistogram.WithLabelValues(namespace, getResourceType(isScaledObject), name).Observe(value.Seconds())
}

// RecordScalerActive create a measurement of the activity of the scaler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,37 @@ func testScalableObjectMetrics(t *testing.T) {
}
}
assert.Equal(t, true, found)

val, ok = family["keda_internal_scale_loop_latency_seconds_bucket"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, but I think that the name is keda_internal_scale_loop_latency_bucket here
e2e test has failed here
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see the problem with otel. There is no otel metric for this. The problem is I don't have enough expertise to create an otel histogram metric 🤔 So we have two options. Remove e2e test or make the otel metric somehow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have to call the the record function instead of using an observable approach

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, now I can see in the code that I tried something, but apparently it's not the right way to do it.

assert.True(t, ok, "keda_internal_scale_loop_latency_seconds_bucket 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)
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/sequential/prometheus_metrics/prometheus_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,36 @@ func testScalableObjectMetrics(t *testing.T) {
}
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)
} else {
t.Errorf("scaledobject metric not available")
}
if val, ok := family["keda_internal_scale_loop_latency_seconds_bucket"]; 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 {
Expand Down
Loading