Skip to content

Commit d376b91

Browse files
committed
metrics: include the name of the server
all MCP servers will have the same metrics, so let's add a label for the name of the server (here: `argocd-mcp-server`) to help with our PromQL queries Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
1 parent 1c9fed0 commit d376b91

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

internal/metrics/metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
Name: "mcp_calls_total",
1414
Help: "Total number of MCP calls",
1515
},
16-
[]string{"method", "name", "success"},
16+
[]string{"server", "method", "name", "success"},
1717
)
1818

1919
// MCPCallDuration measures the duration of MCP calls by method, name (for `tools/call`) and success
@@ -23,6 +23,6 @@ var (
2323
Help: "Duration of MCP calls in seconds",
2424
Buckets: prometheus.DefBuckets,
2525
},
26-
[]string{"method", "name", "success"},
26+
[]string{"server", "method", "name", "success"},
2727
)
2828
)

internal/server/middleware.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/modelcontextprotocol/go-sdk/mcp"
1111
)
1212

13+
const serverName = "argocd-mcp-server"
14+
1315
func NewMetricsMiddleware(logger *slog.Logger) mcp.Middleware {
1416
return func(next mcp.MethodHandler) mcp.MethodHandler {
1517
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
@@ -30,8 +32,8 @@ func NewMetricsMiddleware(logger *slog.Logger) mcp.Middleware {
3032
success = success && !r.IsError
3133
}
3234
// increment/update the metrics
33-
metrics.MCPCallsTotal.WithLabelValues(method, tool, strconv.FormatBool(success)).Inc()
34-
metrics.MCPCallDuration.WithLabelValues(method, tool, strconv.FormatBool(success)).Observe(float64(duration.Seconds()))
35+
metrics.MCPCallsTotal.WithLabelValues(serverName, method, tool, strconv.FormatBool(success)).Inc()
36+
metrics.MCPCallDuration.WithLabelValues(serverName, method, tool, strconv.FormatBool(success)).Observe(float64(duration.Seconds()))
3537
return result, err
3638
}
3739
}

taskfile.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ tasks:
2424

2525
podman-compose-up:
2626
cmds:
27-
- podman-compose up -d
27+
# --build is used to build the images if they are not already built
28+
- podman-compose up -d --build
2829

2930
podman-compose-down:
3031
cmds:

test/e2e/server_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestStatefulServer(t *testing.T) {
6363
var mcpCallsDurationSecondsInfBucketBefore int64
6464
if td.name == "http" {
6565
mcpCallsTotalMetricBefore, mcpCallsDurationSecondsInfBucketBefore = getMetrics(t, "http://localhost:50081", map[string]string{
66+
"server": "argocd-mcp-server",
6667
"method": "tools/call",
6768
"name": "unhealthyApplications",
6869
"success": "true",
@@ -99,6 +100,7 @@ func TestStatefulServer(t *testing.T) {
99100
if td.name == "http" {
100101
// get the metrics after the call
101102
mcpCallsTotalMetricAfter, mcpCallsDurationSecondsInfBucketAfter := getMetrics(t, "http://localhost:50081", map[string]string{
103+
"server": "argocd-mcp-server",
102104
"method": "tools/call",
103105
"name": "unhealthyApplications",
104106
"success": "true",
@@ -114,6 +116,7 @@ func TestStatefulServer(t *testing.T) {
114116
var mcpCallsDurationSecondsInfBucketBefore int64
115117
if td.name == "http" {
116118
mcpCallsTotalMetricBefore, mcpCallsDurationSecondsInfBucketBefore = getMetrics(t, "http://localhost:50081", map[string]string{
119+
"server": "argocd-mcp-server",
117120
"method": "tools/call",
118121
"name": "unhealthyApplicationResources",
119122
"success": "true",
@@ -181,6 +184,7 @@ func TestStatefulServer(t *testing.T) {
181184
if td.name == "http" {
182185
// get the metrics after the call
183186
mcpCallsTotalMetricAfter, mcpCallsDurationSecondsInfBucketAfter := getMetrics(t, "http://localhost:50081", map[string]string{
187+
"server": "argocd-mcp-server",
184188
"method": "tools/call",
185189
"name": "unhealthyApplicationResources",
186190
"success": "true",
@@ -195,6 +199,7 @@ func TestStatefulServer(t *testing.T) {
195199
var mcpCallsDurationSecondsInfBucketBefore int64
196200
if td.name == "http" {
197201
mcpCallsTotalMetricBefore, mcpCallsDurationSecondsInfBucketBefore = getMetrics(t, "http://localhost:50081", map[string]string{
202+
"server": "argocd-mcp-server",
198203
"method": "tools/call",
199204
"name": "unhealthyApplicationResources",
200205
"success": "false",
@@ -215,6 +220,7 @@ func TestStatefulServer(t *testing.T) {
215220
if td.name == "http" {
216221
// get the metrics after the call
217222
mcpCallsTotalMetricAfter, mcpCallsDurationSecondsInfBucketAfter := getMetrics(t, "http://localhost:50081", map[string]string{
223+
"server": "argocd-mcp-server",
218224
"method": "tools/call",
219225
"name": "unhealthyApplicationResources",
220226
"success": "false",

0 commit comments

Comments
 (0)