Skip to content

Conversation

strawgate
Copy link
Collaborator

Add comprehensive documentation for integrating OpenTelemetry with FastMCP for distributed tracing and observability.

Changes

  • New integration guide at docs/integrations/opentelemetry.mdx
  • Covers logging integration with LoggingHandler
  • Demonstrates span creation via custom middleware
  • Includes production OTLP export configuration
  • Provides complete working example
  • Updated related docs with OpenTelemetry references
  • Registered in docs.json under new Observability section
  • Added working example in examples/opentelemetry_example.py

Closes #1998

Generated with Claude Code

Add comprehensive documentation for integrating OpenTelemetry with FastMCP:
- New integration guide at docs/integrations/opentelemetry.mdx
- Covers logging integration with LoggingHandler
- Demonstrates span creation via custom middleware
- Includes production OTLP export configuration
- Provides complete working example

Also update related docs:
- Add OpenTelemetry reference in middleware.mdx
- Add tip about OpenTelemetry in logging.mdx
- Register doc in docs.json under new Observability section

Example code:
- examples/opentelemetry_example.py with working weather server

Closes #1998

Co-authored-by: William Easton <strawgate@users.noreply.github.com>
@marvin-context-protocol marvin-context-protocol bot added the documentation Updates to docs, examples, or guides. Primary change is documentation-related. label Oct 4, 2025
Copy link

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why we wouldn't just bake the instrumentation directly into FastMCP and enable it by default? It's not harmful to have the no-op/non-recording spans if you're not exporting them anywhere. There may be a very slight performance effect, but I think it's pretty minimal. Worst case, we could give people an option to disable it if they didn't want OTEL.

Comment on lines +63 to +75
# Set up tracing
trace_provider = TracerProvider(resource=resource)
trace_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(trace_provider)

# Set up logging
logger_provider = LoggerProvider(resource=resource)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(ConsoleLogExporter()))
set_logger_provider(logger_provider)

# Attach OpenTelemetry to FastMCP's logger
fastmcp_logger = get_logger("my_server")
fastmcp_logger.addHandler(LoggingHandler(logger_provider=logger_provider))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach would be considered "manual" instrumentation, which is always an option, but wouldn't most folks want something like the "Zero Code" version: https://opentelemetry.io/docs/zero-code/python/

I think we should at least point folks to this as the default, and then we can explain in more detail the manual approach.

I do think there are some additional envvars or CLI switches required to get logging setup with the zero code approach.


### Basic Tracing Middleware

Create a middleware that emits spans for all MCP requests:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just ship this middleware with FastMCP? I'd argue it should be on by default, even.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Updates to docs, examples, or guides. Primary change is documentation-related.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document logging with OpenTelemetry
2 participants