toolhive icon indicating copy to clipboard operation
toolhive copied to clipboard

Add OpenTelemetry metrics to vMCP server

Open JAORMX opened this issue 3 weeks ago • 0 comments

Summary

Instrument the vMCP server with OpenTelemetry metrics to enable observability and monitoring of virtual MCP server deployments.

Background

ToolHive already has a comprehensive telemetry package (pkg/telemetry/) that provides:

  • Distributed tracing with W3C Trace Context propagation
  • Prometheus metrics export
  • OTLP support for remote collectors
  • HTTP middleware for automatic instrumentation

The vMCP server currently lacks this instrumentation, making it difficult to monitor and debug production deployments.

Reusable Components

Component Location Description
Telemetry Middleware pkg/telemetry/middleware.go HTTP instrumentation
Config pkg/telemetry/config.go Configuration model
Providers pkg/telemetry/providers/ OTEL provider setup

Existing Metrics (from pkg/telemetry/middleware.go)

toolhive_mcp_requests_total      // Total requests by method, status, MCP method, server, transport
toolhive_mcp_request_duration_seconds  // Request latency histogram
toolhive_mcp_active_connections  // Active connections gauge
toolhive_mcp_tool_calls_total    // Tool-specific call counts

Implementation

  1. Add telemetry middleware to vMCP server (pkg/vmcp/server/server.go)

    • Wrap HTTP routes with telemetry.HTTPMiddleware
    • Configure service name as toolhive-vmcp
  2. Add vMCP-specific metrics

    toolhive_vmcp_backend_requests_total     // Requests per backend
    toolhive_vmcp_backend_latency_seconds    // Per-backend latency
    toolhive_vmcp_composite_tool_duration_seconds  // Composite workflow execution time
    toolhive_vmcp_backends_discovered        // Gauge of active backends
    
  3. Add configuration support

    • CRD: Add telemetry config to VirtualMCPServerSpec
    • CLI: Add telemetry flags to vmcp serve command
  4. Expose /metrics endpoint

    • Prometheus scrape endpoint for metrics collection

Acceptance Criteria

  • [ ] vMCP server uses existing telemetry middleware
  • [ ] Prometheus metrics exported at /metrics endpoint
  • [ ] Per-backend request metrics tracked
  • [ ] Composite tool execution metrics tracked
  • [ ] Telemetry configurable via CRD and CLI
  • [ ] Sensitive data (tokens, secrets) redacted from traces
  • [ ] Unit tests for metrics emission
  • [ ] Documentation updated

JAORMX avatar Dec 02 '25 20:12 JAORMX