Skip to content

fix: record left status in jenkins.queue.count metric and add queue metrics test - #1271

Open
kanagaabishek wants to merge 4 commits into
jenkinsci:mainfrom
kanagaabishek:fix/queue-metrics-left-status-and-test
Open

fix: record left status in jenkins.queue.count metric and add queue metrics test#1271
kanagaabishek wants to merge 4 commits into
jenkinsci:mainfrom
kanagaabishek:fix/queue-metrics-left-status-and-test

Conversation

@kanagaabishek

@kanagaabishek kanagaabishek commented Mar 29, 2026

Copy link
Copy Markdown

… metrics test

Issue #1174: The 'left' queue item status was counted in the batchCallback but never recorded to the queueItems ObservableLongMeasurement, making the 'left' status invisible in the jenkins.queue.count metric.

This commit:

  • Records left items with STATUS='left' in the queueItems gauge
  • Adds MonitoringQueueListenerTest verifying all queue metrics are registered and exported after a job completes

Fixes: #1174

Fixes #1174

The left queue item status was counted inside the batchCallback in
MonitoringQueueListener but was never recorded to the queueItems
ObservableLongMeasurement. This meant the left status was silently
dropped from the jenkins.queue.count metric while all other statuses
(blocked, buildable, stuck, waiting) were correctly recorded.

Root cause

In MonitoringQueueListener.postConstruct(), the batch callback tracks
left items via AtomicInteger left and increments it correctly, but
the corresponding queueItems.record(left.get(), Attributes.of(STATUS, "left"))
call was missing. Every other status had its recording call — left did not.

Changes

  • MonitoringQueueListener.java: added queueItems.record(left.get(), Attributes.of(STATUS, "left")) alongside the existing status recordings
  • MonitoringQueueListenerTest.java: first dedicated test for queue metrics — verifies all six queue metrics (jenkins.queue.count, jenkins.queue.waiting, jenkins.queue.blocked, jenkins.queue.buildable, jenkins.queue.left, jenkins.queue.time_spent_millis) are registered and exported after a job completes

Testing done

Added MonitoringQueueListenerTest — the first automated test for queue metrics.

The test:

  1. Schedules and completes a FreeStyleProject job
  2. Forces an SDK meter provider flush via forceFlush().join(10, TimeUnit.SECONDS)
  3. Asserts all six queue metrics are present in the exported metrics map
  4. Asserts jenkins.queue.left counter value is >= 1 after the job completes

To run:
./mvnw test -Dtest="MonitoringQueueListenerTest"

Test passes locally on Java 21 / Maven 3.9.9.

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

… metrics test

Issue jenkinsci#1174: The 'left' queue item status was counted in the batchCallback
but never recorded to the queueItems ObservableLongMeasurement, making
the 'left' status invisible in the jenkins.queue.count metric.

This commit:
- Records left items with STATUS='left' in the queueItems gauge
- Adds MonitoringQueueListenerTest verifying all queue metrics are
  registered and exported after a job completes

Fixes: jenkinsci#1174
@kanagaabishek
kanagaabishek requested a review from a team as a code owner March 29, 2026 07:17
@kanagaabishek

Copy link
Copy Markdown
Author

I noticed PR #1240 also addresses #1174 by fixing ACL-restricted queue visibility.

My PR addresses a separate gap: the left status is counted via AtomicInteger left but the corresponding queueItems.record(left.get(), ...) call was never written, so jenkins.queue.count{status="left"} is always absent regardless of ACL context.

These two fixes address different failure modes of the same issue — #1240 fixes visibility under restricted auth, this PR fixes completeness of the left dimension.

Happy to rebase on top of #1240 if the maintainers prefer a combined fix.

@Test
public void testQueueMetricsAreRegisteredAndExported() throws Exception {
// Schedule and complete a job so the queue is exercised
FreeStyleProject project = jenkinsRule.createFreeStyleProject("queue-metrics-test");

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.

Why a FreeStyle job? FreeStyle job is supported (mostly) but it is not the main focus of the plugin, we support Pipelines, if something does not work with FreeStyle jobs is not a priority, and we can remove support any time if we want.

@kanagaabishek kanagaabishek Apr 14, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@kuisathaverat Thank you for the review. Updated the test to use WorkflowJob (Pipeline) instead of FreeStyleProject — Pipeline is the primary supported job type for this plugin. The queue mechanics exercised by the test are identical; only the job type changed.
Please let me know if any further changes required

Use WorkflowJob (Pipeline) instead of FreeStyleProject as suggested
in review — Pipeline is the primary supported job type for this plugin.
@kuisathaverat
kuisathaverat removed their request for review May 4, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build queue metrics not registered

2 participants