-
Notifications
You must be signed in to change notification settings - Fork 319
refactor: extract metrics formatter #543
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
Open
zereight
wants to merge
2
commits into
main
Choose a base branch
from
refactor/metrics-formatter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| export interface MetricsSnapshot { | ||
| requestsProcessed: number; | ||
| rejectedByRateLimit: number; | ||
| rejectedByCapacity: number; | ||
| authFailures: number; | ||
| totalSessions: number; | ||
| expiredSessions: number; | ||
| activeSessions: number; | ||
| authenticatedSessions: number; | ||
| gitlabClientPool: { size: number; maxSize: number }; | ||
| uptime: number; | ||
| memoryUsage: NodeJS.MemoryUsage; | ||
| statelessRequests: number; | ||
| statelessAuthFromHeader: number; | ||
| statelessAuthFromSealedSid: number; | ||
| statelessAuthFailures: number; | ||
| statelessSidRotated: number; | ||
| config: { | ||
| maxSessions: number; | ||
| maxRequestsPerMinute: number; | ||
| sessionTimeoutSeconds: number; | ||
| remoteAuthEnabled: boolean; | ||
| mcpOAuthEnabled: boolean; | ||
| statelessModeEnabled: boolean; | ||
| statelessRotationKey: boolean; | ||
| }; | ||
| } | ||
|
|
||
| export function escapePrometheusLabel(value: unknown): string { | ||
| return String(value).replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"'); | ||
| } | ||
|
|
||
| export function formatPrometheusMetrics(snapshot: MetricsSnapshot): string { | ||
| const configLabels = Object.entries({ | ||
| max_sessions: snapshot.config.maxSessions, | ||
| max_requests_per_minute: snapshot.config.maxRequestsPerMinute, | ||
| session_timeout_seconds: snapshot.config.sessionTimeoutSeconds, | ||
| remote_auth_enabled: snapshot.config.remoteAuthEnabled, | ||
| mcp_oauth_enabled: snapshot.config.mcpOAuthEnabled, | ||
| stateless_mode_enabled: snapshot.config.statelessModeEnabled, | ||
| stateless_rotation_key: snapshot.config.statelessRotationKey, | ||
| }) | ||
| .map(([key, value]) => `${key}="${escapePrometheusLabel(value)}"`) | ||
| .join(","); | ||
|
|
||
| return [ | ||
| "# HELP gitlab_mcp_requests_processed_total Total MCP requests processed", | ||
| "# TYPE gitlab_mcp_requests_processed_total counter", | ||
| `gitlab_mcp_requests_processed_total ${snapshot.requestsProcessed}`, | ||
| "", | ||
| "# HELP gitlab_mcp_requests_rejected_total Requests rejected, by reason", | ||
| "# TYPE gitlab_mcp_requests_rejected_total counter", | ||
| `gitlab_mcp_requests_rejected_total{reason="rate_limit"} ${snapshot.rejectedByRateLimit}`, | ||
| `gitlab_mcp_requests_rejected_total{reason="capacity"} ${snapshot.rejectedByCapacity}`, | ||
| "", | ||
| "# HELP gitlab_mcp_auth_failures_total Authentication failures", | ||
| "# TYPE gitlab_mcp_auth_failures_total counter", | ||
| `gitlab_mcp_auth_failures_total ${snapshot.authFailures}`, | ||
| "", | ||
| "# HELP gitlab_mcp_sessions_total Total sessions created", | ||
| "# TYPE gitlab_mcp_sessions_total counter", | ||
| `gitlab_mcp_sessions_total ${snapshot.totalSessions}`, | ||
| "", | ||
| "# HELP gitlab_mcp_sessions_expired_total Sessions expired due to inactivity", | ||
| "# TYPE gitlab_mcp_sessions_expired_total counter", | ||
| `gitlab_mcp_sessions_expired_total ${snapshot.expiredSessions}`, | ||
| "", | ||
| "# HELP gitlab_mcp_active_sessions Currently active sessions", | ||
| "# TYPE gitlab_mcp_active_sessions gauge", | ||
| `gitlab_mcp_active_sessions ${snapshot.activeSessions}`, | ||
| "", | ||
| "# HELP gitlab_mcp_authenticated_sessions Currently authenticated sessions", | ||
| "# TYPE gitlab_mcp_authenticated_sessions gauge", | ||
| `gitlab_mcp_authenticated_sessions ${snapshot.authenticatedSessions}`, | ||
| "", | ||
| "# HELP gitlab_mcp_client_pool_size Current GitLab client pool size", | ||
| "# TYPE gitlab_mcp_client_pool_size gauge", | ||
| `gitlab_mcp_client_pool_size ${snapshot.gitlabClientPool.size}`, | ||
| "", | ||
| "# HELP gitlab_mcp_client_pool_max_size Maximum GitLab client pool size", | ||
| "# TYPE gitlab_mcp_client_pool_max_size gauge", | ||
| `gitlab_mcp_client_pool_max_size ${snapshot.gitlabClientPool.maxSize}`, | ||
| "", | ||
| "# HELP gitlab_mcp_uptime_seconds Process uptime in seconds", | ||
| "# TYPE gitlab_mcp_uptime_seconds gauge", | ||
| `gitlab_mcp_uptime_seconds ${snapshot.uptime}`, | ||
| "", | ||
| "# HELP gitlab_mcp_memory_usage_bytes Node.js memory usage by type", | ||
| "# TYPE gitlab_mcp_memory_usage_bytes gauge", | ||
| ...Object.entries(snapshot.memoryUsage).map( | ||
| ([key, value]) => `gitlab_mcp_memory_usage_bytes{type="${escapePrometheusLabel(key)}"} ${value}` | ||
| ), | ||
| "", | ||
| "# HELP gitlab_mcp_stateless_requests_total Stateless MCP requests processed", | ||
| "# TYPE gitlab_mcp_stateless_requests_total counter", | ||
| `gitlab_mcp_stateless_requests_total ${snapshot.statelessRequests}`, | ||
| "", | ||
| "# HELP gitlab_mcp_stateless_auth_total Stateless auth successes, by source", | ||
| "# TYPE gitlab_mcp_stateless_auth_total counter", | ||
| `gitlab_mcp_stateless_auth_total{source="header"} ${snapshot.statelessAuthFromHeader}`, | ||
| `gitlab_mcp_stateless_auth_total{source="sealed_session_id"} ${snapshot.statelessAuthFromSealedSid}`, | ||
| "", | ||
| "# HELP gitlab_mcp_stateless_auth_failures_total Stateless auth failures", | ||
| "# TYPE gitlab_mcp_stateless_auth_failures_total counter", | ||
| `gitlab_mcp_stateless_auth_failures_total ${snapshot.statelessAuthFailures}`, | ||
| "", | ||
| "# HELP gitlab_mcp_stateless_session_id_rotations_total Stateless session id rotations", | ||
| "# TYPE gitlab_mcp_stateless_session_id_rotations_total counter", | ||
| `gitlab_mcp_stateless_session_id_rotations_total ${snapshot.statelessSidRotated}`, | ||
| "", | ||
| "# HELP gitlab_mcp_config_info Static configuration (value is always 1)", | ||
| "# TYPE gitlab_mcp_config_info gauge", | ||
| `gitlab_mcp_config_info{${configLabels}} 1`, | ||
| "", | ||
| ].join("\n"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { describe, test } from "node:test"; | ||
| import assert from "node:assert"; | ||
| import { escapePrometheusLabel, formatPrometheusMetrics } from "../../server/metrics.js"; | ||
|
|
||
| describe("Prometheus metrics formatting", () => { | ||
| test("escapes label values", () => { | ||
| assert.strictEqual(escapePrometheusLabel('a"b\\c\nd'), 'a\\"b\\\\c\\nd'); | ||
| }); | ||
|
|
||
| test("formats current MCP metrics", () => { | ||
| const body = formatPrometheusMetrics({ | ||
| requestsProcessed: 2, | ||
| rejectedByRateLimit: 1, | ||
| rejectedByCapacity: 0, | ||
| authFailures: 3, | ||
| totalSessions: 4, | ||
| expiredSessions: 5, | ||
| activeSessions: 6, | ||
| authenticatedSessions: 7, | ||
| gitlabClientPool: { size: 8, maxSize: 100 }, | ||
| uptime: 9, | ||
| memoryUsage: { | ||
| rss: 10, | ||
| heapTotal: 11, | ||
| heapUsed: 12, | ||
| external: 13, | ||
| arrayBuffers: 14, | ||
| }, | ||
| statelessRequests: 15, | ||
| statelessAuthFromHeader: 16, | ||
| statelessAuthFromSealedSid: 17, | ||
| statelessAuthFailures: 18, | ||
| statelessSidRotated: 19, | ||
| config: { | ||
| maxSessions: 1000, | ||
| maxRequestsPerMinute: 60, | ||
| sessionTimeoutSeconds: 3600, | ||
| remoteAuthEnabled: true, | ||
| mcpOAuthEnabled: false, | ||
| statelessModeEnabled: false, | ||
| statelessRotationKey: false, | ||
| }, | ||
| }); | ||
|
|
||
| assert.match(body, /# HELP gitlab_mcp_requests_processed_total/); | ||
| assert.match(body, /gitlab_mcp_requests_processed_total 2/); | ||
| assert.match(body, /gitlab_mcp_requests_rejected_total\{reason="rate_limit"\} 1/); | ||
| assert.match(body, /gitlab_mcp_config_info\{[^}]*remote_auth_enabled="true"/); | ||
| }); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
LGTM!
The test provides solid coverage of the core formatting logic. If you want to strengthen the test suite, consider adding assertions for additional metric lines (e.g., memory_usage_bytes labels, stateless counters) to ensure comprehensive validation of the Prometheus exposition format.
🤖 Prompt for AI Agents