Provides OpenTelemetry instrumentation, pre-configured metrics, and health check utilities for the MCP Gateway. It exports functions for registering custom health probes and initializes standard OTel tracing and Pino-based structured logging.
Returns per-component health with individual latency timings
createRedisProbe(pingFn, timeoutMs?)
Factory for Redis ping-based health probe
createUpstreamProbe(url, timeoutMs?)
Factory for HTTP GET-based upstream health probe
HealthProbe
() => Promise<ComponentHealth>
HealthStatus
{ status, version?, uptimeSeconds?, components? }
ComponentHealth
{ status, message?, latencyMs? }
Metrics
All metrics use OpenTelemetry API. Meter name is SERVICE_NAME (mcp-gateway).
Counters
Metric
Labels
Description
gateway.requests.total
tenant_id, status
Total requests processed
gateway.auth.attempts
method, result
Auth attempts by type and outcome
gateway.auth.failures
reason
Failed auth attempts
gateway.cache.hits
tool
Cache hit count per tool
gateway.cache.misses
tool
Cache miss count per tool
gateway.rate_limit.exceeded
tenant_id
Rate limit exceeded count
gateway.allowlist.denied
tenant_id, tool
Allowlist denial count
gateway.upstream.requests
upstream, method
Upstream request count
gateway.upstream.errors
upstream, error_type
Upstream error count
gateway.fanout.upstreams
strategy
Fan-out upstream count
gateway.validation.errors
type
Validation error count
gateway.audit.events
event_type
Audit event count
Histograms
Metric
Labels
Description
gateway.requests.duration_ms
tenant_id, method
Request processing time
gateway.upstream.latency_ms
upstream
Upstream call latency
Gauges
Metric
Labels
Description
gateway.cache.size
—
Current cache entry count
gateway.rate_limit.remaining
tenant_id
Remaining rate limit tokens
Utility
Export
Description
resetMetricsState()
Reset all metric values (for testing only)
Tracing
Export
Description
getTracer()
Get the OpenTelemetry tracer
startSpan(name, options?)
Start a new span with standard attributes
endSpan(span, status?)
End a span with optional status code
OpenTelemetry Lifecycle
Export
Description
setupOTel()
Initialize the OTel SDK (called automatically on import)
shutdownOTel()
Gracefully shut down OTel (flush pending telemetry)
Auto-Init Behavior
The SDK initializes automatically when the package is imported, but only if OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set. In production without the endpoint, it logs a warning and skips initialization. Raw JSON is always used (no pretty-printing in production).
Usage Patterns
Enabling OpenTelemetry
terminal
# Set the OTLP endpoint (e.g., Jaeger, OpenTelemetry Collector)export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318# The SDK auto-initializes on import — no code needednode dist/index.js