Skip to content

fix: emit active metric for resource scalers#7755

Merged
rickbrouwer merged 6 commits into
kedacore:mainfrom
puneetdixit200:fix/4945-initialize-scaler-active
Jun 1, 2026
Merged

fix: emit active metric for resource scalers#7755
rickbrouwer merged 6 commits into
kedacore:mainfrom
puneetdixit200:fix/4945-initialize-scaler-active

Conversation

@puneetdixit200

@puneetdixit200 puneetdixit200 commented May 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #4945.

This records keda_scaler_active for resource metrics before skipping the CPU/memory metric specs that KEDA does not query directly. The existing behavior for resource metric values/errors stays unchanged, but the active metric is initialized so dashboards can resolve labels for ScaledObjects that only use CPU or memory triggers.

Tests:

  • GOMODCACHE="$PWD/.gomodcache" GOCACHE="$PWD/.gocache" go test ./pkg/scaling -run TestGetScaledObjectStateRecordsResourceScalerActiveMetric -count=2
  • GOMODCACHE="$PWD/.gomodcache" GOCACHE="$PWD/.gocache" go test ./pkg/scaling -count=1
  • GOMODCACHE="$PWD/.gomodcache" GOCACHE="$PWD/.gocache" go test ./pkg/metricscollector -count=1
  • git diff --check

Checklist

  • When introducing a new scaler, I agree with the scaling governance policy
  • I have verified that my change is according to the deprecations & breaking changes policy
  • Tests have been added
  • Ensure make generate-scalers-schema has been run to update any outdated generated files
  • Changelog has been updated and is aligned with the changelog requirements
  • A PR is opened to update the Helm chart
  • A PR is opened to update the documentation if applicable
  • Commits are signed with Developer Certificate of Origin

Documentation, Helm chart, scaler schema, and new-scaler governance updates are not applicable here because this only initializes an existing metric and does not change manifests, scaler schemas, or public configuration.

@puneetdixit200 puneetdixit200 requested a review from a team as a code owner May 21, 2026 17:15
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! 🙏

Please understand that we will do our best to review your PR and give you feedback as soon as possible, but please bear with us if it takes a little longer as expected.

While you are waiting, make sure to:

  • Add an entry in our changelog in alphabetical order and link related issue
  • Update the documentation, if needed
  • Add unit & e2e tests for your changes
  • GitHub checks are passing
  • Is the DCO check failing? Here is how you can fix DCO issues

Once the initial tests are successful, a KEDA member will ensure that the e2e tests are run. Once the e2e tests have been successfully completed, the PR may be merged at a later date. Please be patient.

Learn more about our contribution guide.

@keda-automation keda-automation requested a review from a team May 21, 2026 17:15
@snyk-io

snyk-io Bot commented May 21, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

Comment thread pkg/scaling/scale_handler_test.go Outdated
Use context.Background in the resource scaler active metric regression test instead of context.TODO.

Signed-off-by: Puneet Dixit <[email protected]>
@JorTurFer

Copy link
Copy Markdown
Member

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes missing initialization of the keda_scaler_active Prometheus metric for CPU/memory (resource) triggers by recording activity metrics for resource and containerResource metric specs that KEDA otherwise skips querying directly. This improves Grafana dashboard label discovery for ScaledObjects that only use CPU/memory triggers.

Changes:

  • Record keda_scaler_active for Resource / ContainerResource metric specs before continuing past non-external metric specs.
  • Add a unit test that asserts keda_scaler_active is emitted with the expected labels for a CPU resource trigger.
  • Add a changelog entry for the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/scaling/scale_handler.go Emit keda_scaler_active for resource/containerResource metric specs that were previously skipped.
pkg/scaling/scale_handler_test.go Add regression test to ensure the active metric is emitted for resource scalers.
CHANGELOG.md Document the bugfix for missing keda_scaler_active on CPU/memory triggers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/scaling/scale_handler.go
@puneetdixit200

Copy link
Copy Markdown
Contributor Author

Added the requested Prometheus and OpenTelemetry sequential coverage in 6f2ee46. Local checks passed: go test ./pkg/scaling -count=1, and go test -tags e2e ./tests/sequential/prometheus_metrics ./tests/sequential/opentelemetry_metrics -run "^$" -count=0.

@zroubalik

zroubalik commented May 26, 2026

Copy link
Copy Markdown
Member

/run-e2e sequential
Update: You can check the progress here

@puneetdixit200

Copy link
Copy Markdown
Contributor Author

Thanks for running the sequential e2e. I checked the failing run/artifact and it looks like this failed in shared setup before the new sequential metric tests ran: TestVerifyKEDA timed out waiting for keda-admission to reach 1 replica after 3 minutes, while keda-webhooks.log shows the admission webhook process started at 2026-05-26T08:04:02Z immediately after that wait window. The operator and metrics server logs otherwise came up normally.

Could you rerun /run-e2e sequential when you have a chance?

@rickbrouwer

rickbrouwer commented May 26, 2026

Copy link
Copy Markdown
Member

/run-e2e sequential
Update: You can check the progress here

passed tests: 5
Execution of tests/sequential/disruption/disruption_test.go, has passed after "one" attempts
Execution of tests/sequential/prometheus_metrics/prometheus_metrics_test.go, has passed after "one" attempts
Execution of tests/sequential/datadog_dca/datadog_dca_test.go, has passed after "one" attempts
Execution of tests/sequential/broken_scaledobject_tolerancy/broken_scaledobject_tolerancy_test.go, has passed after "one" attempts
Execution of tests/sequential/opentelemetry_metrics/opentelemetry_metrics_test.go, has passed after "one" attempts
failed tests: 0

@keda-automation keda-automation requested a review from a team May 26, 2026 08:18
@zroubalik zroubalik added the Awaiting/2nd-approval This PR needs one more approval review label May 26, 2026
…-scaler-active

Signed-off-by: Deepak kudi <[email protected]>

# Conflicts:
#	CHANGELOG.md
@puneetdixit200

Copy link
Copy Markdown
Contributor Author

Merged current main into this branch to clear the dirty state. Current head is 3492f0bd.

Local verification before push:

  • git diff --check
  • go test ./pkg/scaling -count=1 (ok github.com/kedacore/keda/v2/pkg/scaling 2.960s)

Fresh GitHub checks are queued/running now; DCO is already green.

@puneetdixit200

Copy link
Copy Markdown
Contributor Author

Merged current main into this branch to clear the conflict. The only manual conflict resolution was keeping the existing CHANGELOG.md General fix entry for #4945 in the current Unreleased Fixes list.

Local verification:

  • git diff --check upstream/main...HEAD
  • go test ./pkg/scaling -count=1
  • go test -tags e2e ./tests/sequential/prometheus_metrics ./tests/sequential/opentelemetry_metrics -run "^$" -count=0

GitHub checks are running on the new head 3492f0bd.

@rickbrouwer

rickbrouwer commented Jun 1, 2026

Copy link
Copy Markdown
Member

/run-e2e sequential
Update: You can check the progress here

passed tests: 5
Execution of tests/sequential/opentelemetry_metrics/opentelemetry_metrics_test.go, has passed after "one" attempts
Execution of tests/sequential/disruption/disruption_test.go, has passed after "one" attempts
Execution of tests/sequential/prometheus_metrics/prometheus_metrics_test.go, has passed after "one" attempts
Execution of tests/sequential/datadog_dca/datadog_dca_test.go, has passed after "one" attempts
Execution of tests/sequential/broken_scaledobject_tolerancy/broken_scaledobject_tolerancy_test.go, has passed after "one" attempts
failed tests: 0

@rickbrouwer rickbrouwer added waiting-for-e2e and removed Awaiting/2nd-approval This PR needs one more approval review labels Jun 1, 2026
@rickbrouwer rickbrouwer enabled auto-merge (squash) June 1, 2026 07:01
@rickbrouwer rickbrouwer merged commit 92bca15 into kedacore:main Jun 1, 2026
25 checks passed
wozniakjan added a commit that referenced this pull request Jun 1, 2026
…ration (#7808)

#7755 added TestGetScaledObjectStateRecordsResourceScalerActiveMetric and
TestGetScaledObjectStateSkipsResourceScalerActiveMetricWithModifiers using
record.NewFakeRecorder. In the meantime #7781 migrated this file to the
events recorder and dropped the k8s.io/client-go/tools/record import, so
the merged result fails to compile with "undefined: record", breaking the
unit-test build and Static Checks on main.

Use events.NewFakeRecorder, matching the rest of the file and the
events.EventRecorder type of the Recorder/recorder fields.

Signed-off-by: Jan Wozniak <[email protected]>
shcherbak pushed a commit to shcherbak/keda that referenced this pull request Jun 3, 2026
Signed-off-by: Puneet Dixit <[email protected]>
Signed-off-by: Rick Brouwer <[email protected]>
Co-authored-by: Puneet Dixit <[email protected]>
Co-authored-by: Rick Brouwer <[email protected]>
Co-authored-by: Deepak kudi <[email protected]>
Signed-off-by: Yurii Shcherbak <[email protected]>
shcherbak pushed a commit to shcherbak/keda that referenced this pull request Jun 3, 2026
…ration (kedacore#7808)

kedacore#7755 added TestGetScaledObjectStateRecordsResourceScalerActiveMetric and
TestGetScaledObjectStateSkipsResourceScalerActiveMetricWithModifiers using
record.NewFakeRecorder. In the meantime kedacore#7781 migrated this file to the
events recorder and dropped the k8s.io/client-go/tools/record import, so
the merged result fails to compile with "undefined: record", breaking the
unit-test build and Static Checks on main.

Use events.NewFakeRecorder, matching the rest of the file and the
events.EventRecorder type of the Recorder/recorder fields.

Signed-off-by: Jan Wozniak <[email protected]>
Signed-off-by: Yurii Shcherbak <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prometheus metric keda_scaler_active never gets initialized

5 participants