-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[mcp] add server prometheus metrics #59773
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a324fe5
[mcp] add server prometheus metrics
greedy52 febe628
remove TODO and nolint
greedy52 a3427d3
use counter where possible and limit known methods
greedy52 65572b0
move reporting test to individual tests
greedy52 7966e8b
nolint for "cancelled"
greedy52 b380b2c
Merge branch 'master' into STeve/56588_metrics
greedy52 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
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,139 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "slices" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
|
|
||
| "github.com/gravitational/teleport" | ||
| ) | ||
|
|
||
| var ( | ||
| setupErrors = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: teleport.MetricNamespace, | ||
| Name: "setup_errors_total", | ||
| Subsystem: "mcp", | ||
| Help: "Number of errors encountered when setting up MCP sessions", | ||
| }, | ||
| []string{"transport"}, | ||
| ) | ||
|
|
||
| accumulatedSessions = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: teleport.MetricNamespace, | ||
| Name: "sessions_total", | ||
| Subsystem: "mcp", | ||
| Help: "Number of accumulated MCP sessions", | ||
| }, | ||
| []string{"transport"}, | ||
| ) | ||
|
|
||
| activeSessions = prometheus.NewGaugeVec( | ||
| prometheus.GaugeOpts{ | ||
| Namespace: teleport.MetricNamespace, | ||
| Name: "active_sessions_total", | ||
| Subsystem: "mcp", | ||
| Help: "Number of active MCP sessions", | ||
| }, | ||
| []string{"transport"}, | ||
| ) | ||
|
|
||
| messagesFromClient = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: teleport.MetricNamespace, | ||
| Name: "messages_from_client_total", | ||
| Subsystem: "mcp", | ||
| Help: "Number of messages received from the MCP client", | ||
| }, | ||
| []string{"transport", "type", "method"}, | ||
|
greedy52 marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| messagesFromServer = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Namespace: teleport.MetricNamespace, | ||
| Name: "messages_from_server_total", | ||
| Subsystem: "mcp", | ||
| Help: "Number of messages received from the MCP server", | ||
| }, | ||
| []string{"transport", "type", "method"}, | ||
| ) | ||
|
|
||
| allPrometheusCollectors = []prometheus.Collector{ | ||
| setupErrors, | ||
| accumulatedSessions, activeSessions, | ||
| messagesFromClient, messagesFromServer, | ||
| } | ||
|
|
||
| // knownNotificationMethods is a list of known method names for notifications. | ||
| // | ||
| // The list is obtained by searching these in addition to mcp-go: | ||
| // - https://github.com/modelcontextprotocol/modelcontextprotocol | ||
| // - https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/server/index.ts | ||
| knownNotificationMethods = []mcp.MCPMethod{ | ||
| //nolint:misspell // "cancelled" is "UK" spelling but our linter is set to use US locale | ||
| "notifications/cancelled", | ||
| "notifications/initialized", | ||
| "notifications/message", | ||
| "notifications/progress", | ||
| mcp.MethodNotificationPromptsListChanged, // notifications/prompts/list_changed | ||
| mcp.MethodNotificationResourcesListChanged, // notifications/resources/list_changed | ||
| mcp.MethodNotificationResourceUpdated, // notifications/resources/updated | ||
| mcp.MethodNotificationToolsListChanged, // notifications/tools/list_changed | ||
| "notifications/roots/list_changed", | ||
| } | ||
|
|
||
| // knownRequestMethods is a list of known method names for requests. | ||
| // | ||
| // The list is obtained by searching these in addition to mcp-go: | ||
| // - https://github.com/modelcontextprotocol/modelcontextprotocol | ||
| // - https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/server/index.ts | ||
| knownRequestMethods = []mcp.MCPMethod{ | ||
| mcp.MethodInitialize, // initialize | ||
| mcp.MethodPing, // ping | ||
| mcp.MethodResourcesList, // resources/list | ||
| mcp.MethodResourcesTemplatesList, // resources/templates/list | ||
| mcp.MethodResourcesRead, // resources/read | ||
| mcp.MethodPromptsList, // prompts/list | ||
| mcp.MethodPromptsGet, // prompts/get | ||
| mcp.MethodToolsList, // tools/list | ||
| mcp.MethodToolsCall, // tools/call | ||
| mcp.MethodSetLogLevel, // logging/setLevel | ||
| mcp.MethodElicitationCreate, // elicitation/create | ||
| "roots/list", | ||
| "sampling/createMessage", | ||
| } | ||
| ) | ||
|
|
||
| func reportNotificationMethod(method mcp.MCPMethod) string { | ||
| if slices.Contains(knownNotificationMethods, method) { | ||
| return string(method) | ||
| } | ||
| return "unknown" | ||
| } | ||
|
|
||
| func reportRequestMethod(method mcp.MCPMethod) string { | ||
| if slices.Contains(knownRequestMethods, method) { | ||
| return string(method) | ||
| } | ||
| return "unknown" | ||
| } | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.