registry/internal/api/router/router.go
pree-dew d237fc90f8
Add OpenTelemetry metrics instrumentation (#290)
## Motivation and Context
- It is important to have basic observability in place so that
availability issues can be figured out easily

## How Has This Been Tested?
- Registration of metrics is tested
- Prometheus endpoint `/metrics` is included in test cases to test the
metrics exposed
- Test locally to understand any regression.

## Breaking Changes
- No

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [x] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
- Telemetry implementation is currently done by keeping metrics in mind
as of now, other types of telemetry can be added on top of it later.
- Instrumentation takes care of these problems already:
- Cardinality control by limiting latency buckets and not including
labels that grows with throughput.
  - Latency buckets are added as per SLA guidelines
  - Each metric prefixed with `mcp` to easily identify.
- Pulling mechanism is used since the idea is to scrape metrics via an
agent like vmagent, prometheus agent or otel-collector.
- Metrics follow standard approach of defining counters, histogram and
gauges as per opentelemetry guidelines.
2025-08-21 14:23:37 +01:00

126 lines
3.7 KiB
Go

// Package router contains API routing logic
package router
import (
"net/http"
"strings"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/adapters/humago"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/modelcontextprotocol/registry/internal/config"
"github.com/modelcontextprotocol/registry/internal/service"
"github.com/modelcontextprotocol/registry/internal/telemetry"
)
// Middleware configuration options
type middlewareConfig struct {
skipPaths map[string]bool
}
type MiddlewareOption func(*middlewareConfig)
// getRoutePath extracts the route pattern from the context
func getRoutePath(ctx huma.Context) string {
// Try to get the operation from context
if op := ctx.Operation().Path; op != "" {
return ctx.Operation().Path
}
// Fallback to URL path (less ideal for metrics as it includes path parameters)
return ctx.URL().Path
}
func MetricTelemetryMiddleware(metrics *telemetry.Metrics, options ...MiddlewareOption) func(huma.Context, func(huma.Context)) {
config := &middlewareConfig{
skipPaths: make(map[string]bool),
}
for _, opt := range options {
opt(config)
}
return func(ctx huma.Context, next func(huma.Context)) {
path := ctx.URL().Path
// Skip instrumentation for specified paths
// extract the last part of the path to match against skipPaths
pathParts := strings.Split(path, "/")
pathToMatch := "/" + pathParts[len(pathParts)-1]
if config.skipPaths[pathToMatch] || config.skipPaths[path] {
next(ctx)
return
}
start := time.Now()
method := ctx.Method()
routePath := getRoutePath(ctx)
next(ctx)
duration := time.Since(start).Seconds()
statusCode := ctx.Status()
// Combine common and custom attributes
attrs := []attribute.KeyValue{
attribute.String("method", method),
attribute.String("path", routePath),
attribute.Int("status_code", statusCode),
}
// Record metrics
metrics.Requests.Add(ctx.Context(), 1, metric.WithAttributes(attrs...))
if statusCode >= 400 {
metrics.ErrorCount.Add(ctx.Context(), 1, metric.WithAttributes(attrs...))
}
metrics.RequestDuration.Record(ctx.Context(), duration, metric.WithAttributes(attrs...))
}
}
// WithSkipPaths allows skipping instrumentation for specific paths
func WithSkipPaths(paths ...string) MiddlewareOption {
return func(c *middlewareConfig) {
for _, path := range paths {
c.skipPaths[path] = true
}
}
}
// NewHumaAPI creates a new Huma API with all routes registered
//
//nolint:ireturn // huma.API is the expected interface type for Huma APIs
func NewHumaAPI(cfg *config.Config, registry service.RegistryService, mux *http.ServeMux, metrics *telemetry.Metrics) huma.API {
// Create Huma API configuration
humaConfig := huma.DefaultConfig("MCP Registry API", "1.0.0")
humaConfig.Info.Description = "A community driven registry service for Model Context Protocol (MCP) servers."
// Disable $schema property in responses: https://github.com/danielgtaylor/huma/issues/230
humaConfig.CreateHooks = []func(huma.Config) huma.Config{}
// Create a new API using humago adapter for standard library
api := humago.New(mux, humaConfig)
// Add metrics middleware with options
api.UseMiddleware(MetricTelemetryMiddleware(metrics,
WithSkipPaths("/health", "/metrics", "/ping", "/docs"),
))
// Register routes for all API versions
RegisterV0Routes(api, cfg, registry, metrics)
// Add /metrics for Prometheus metrics using promhttp
mux.Handle("/metrics", metrics.PrometheusHandler())
// Add redirect from / to /docs
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "/docs", http.StatusMovedPermanently)
}
})
return api
}