Skip to main content

Crate turbomcp_telemetry

Crate turbomcp_telemetry 

Source
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 export
  • prometheus - Standalone Prometheus metrics (without OpenTelemetry)
  • tower - Tower middleware for automatic request instrumentation
  • full - 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.
metricsprometheus
Prometheus metrics for MCP servers
prelude
Prelude module for convenient imports
span_attributes
MCP span attribute keys following OpenTelemetry semantic conventions
towertower
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§

TelemetryConfig
Telemetry configuration
TelemetryConfigBuilder
Builder for TelemetryConfig
TelemetryGuard
Guard that manages telemetry lifecycle

Enums§

TelemetryError
Errors that can occur during telemetry operations

Traits§

Instrument
Attaches spans to a std::future::Future.

Type Aliases§

TelemetryResult
Result type for telemetry operations

Attribute Macros§

instrument
Instruments a function to create and enter a tracing span every time the function is called.