Expand description
OpenTelemetry integration and observability for TurboMCP SDK
This crate provides comprehensive telemetry capabilities for MCP servers and clients:
- Distributed Tracing: OpenTelemetry traces with MCP-specific span attributes
- Metrics Collection: Request counts, latencies, error rates with Prometheus export
- Structured Logging: JSON-formatted logs correlated with traces
- Tower Middleware: Automatic instrumentation for MCP request handling
§Quick Start
ⓘ
use turbomcp_telemetry::{TelemetryConfig, TelemetryGuard};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize telemetry with OTLP export
let config = TelemetryConfig::builder()
.service_name("my-mcp-server")
.otlp_endpoint("http://localhost:4317")
.build();
let _guard = config.init()?;
// Your MCP server code here...
Ok(())
}§Feature Flags
opentelemetry- Full OpenTelemetry integration with OTLP exportprometheus- Standalone Prometheus metrics (without OpenTelemetry)tower- Tower middleware for automatic request instrumentationfull- All features enabled
§Architecture
┌─────────────────────────────────────────────────────────────┐
│ TurboMCP Application │
├─────────────────────────────────────────────────────────────┤
│ TelemetryLayer (Tower Middleware) │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ Tracing │ │ Metrics │ │ Context Propagation │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
├─────────────────────────────────────────────────────────────┤
│ Export Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ OTLP │ │ Prometheus │ │ Stdout │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘Modules§
- attributes
- MCP-specific span attributes and context helpers
- instrument
- Attach a span to a
std::future::Future. - metrics
prometheus - Prometheus metrics for MCP servers
- prelude
- Prelude module for convenient imports
- span_
attributes - MCP span attribute keys following OpenTelemetry semantic conventions
- tower
tower - Tower middleware for automatic MCP request instrumentation
Macros§
- debug
- Constructs an event at the debug level.
- debug_
span - Constructs a span at the debug level.
- error
- Constructs an event at the error level.
- error_
span - Constructs a span at the error level.
- info
- Constructs an event at the info level.
- info_
span - Constructs a span at the info level.
- trace
- Constructs an event at the trace level.
- trace_
span - Constructs a span at the trace level.
- warn
- Constructs an event at the warn level.
- warn_
span - Constructs a span at the warn level.
Structs§
- Telemetry
Config - Telemetry configuration
- Telemetry
Config Builder - Builder for
TelemetryConfig - Telemetry
Guard - Guard that manages telemetry lifecycle
Enums§
- Telemetry
Error - Errors that can occur during telemetry operations
Traits§
- Instrument
- Attaches spans to a
std::future::Future.
Type Aliases§
- Telemetry
Result - Result type for telemetry operations
Attribute Macros§
- instrument
- Instruments a function to create and enter a
tracingspan every time the function is called.