Skip to content

Add user breakdown to MCP metrics endpoint - #185

Open
seanhumessonos wants to merge 2 commits into
jenkinsci:mainfrom
seanhumessonos:update-metrics
Open

Add user breakdown to MCP metrics endpoint#185
seanhumessonos wants to merge 2 commits into
jenkinsci:mainfrom
seanhumessonos:update-metrics

Conversation

@seanhumessonos

@seanhumessonos seanhumessonos commented May 9, 2026

Copy link
Copy Markdown

Summary

The /mcp-server/metrics endpoint previously only reported server wide aggregate counters. This change extends it to include a per-user breakdown, making it easier to identify which Jenkins accounts are actively connected and how much traffic each one is generating.

Changelist

  • Added UserMetrics inner class to McpConnectionMetrics tracking per-user SSE connections (total and active), streamable requests, and connection errors
  • Added overloaded recording methods (recordSseConnectionStart, recordSseConnectionEnd, recordStreamableRequest, recordConnectionError) that accept a username and update both global and per-user counters atomically
  • Updated Endpoint.java to pass Jenkins.getAuthentication2().getName() at all three metric recording call sites (SSE lifecycle and both streamable request paths)
  • Metrics JSON response now includes a users object keyed by Jenkins username; all users are tracked including anonymous and SYSTEM
  • Updated reset() to clear per-user metrics alongside global counters
  • Added unit tests covering per-user SSE, streamable, error tracking, reset behavior, and the live endpoint JSON shape
  • Updated README metrics section with the new response shape and a note on the per-user breakdown

Testing done

Ran unit tests in vscode:


Tests run: 11, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 43.61 s -- in io.jenkins.plugins.mcp.server.McpConnectionMetricsTest

Results:

Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  56.396 s
Finished at: 2026-05-09T16:22:14-04:00
------------------------------------------------------------------------

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

@seanhumessonos
seanhumessonos requested a review from a team as a code owner May 9, 2026 20:31
@seanhumessonos

Copy link
Copy Markdown
Author

Requesting a review from @olamy and @topikachu when they have a chance.

@olamy olamy added the enhancement For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted label May 12, 2026
/**
* Per-user metrics, keyed by Jenkins username (including "anonymous" and "SYSTEM").
*/
static final ConcurrentHashMap<String, UserMetrics> userMetrics = new ConcurrentHashMap<>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what happen for instance with large number of users? (possible memory leak?)

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.

Hi @olamy
Thank you for taking a look at the PR. You're right that entries are never removed and the map grows once per unique username. Since this is bounded by the number of Jenkins users rather than by traffic, I think the memory footprint should stay small in practice (~150 bytes per user). I can add a comment to that effect if you'd like. Thoughts?

@seanhumessonos
seanhumessonos requested a review from olamy June 22, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants