|
| 1 | +from fastmcp.server.middleware import Middleware, MiddlewareContext |
| 2 | +from opentelemetry import trace |
| 3 | +from opentelemetry.trace import Status, StatusCode |
| 4 | + |
| 5 | + |
| 6 | +class OpenTelemetryMiddleware(Middleware): |
| 7 | + """Middleware that creates OpenTelemetry spans for MCP operations.""" |
| 8 | + |
| 9 | + def __init__(self, tracer_name: str): |
| 10 | + self.tracer = trace.get_tracer(tracer_name) |
| 11 | + |
| 12 | + async def on_call_tool(self, context: MiddlewareContext, call_next): |
| 13 | + """Create a span for each tool call with detailed attributes.""" |
| 14 | + tool_name = context.message.name |
| 15 | + |
| 16 | + with self.tracer.start_as_current_span( |
| 17 | + f"tool.{tool_name}", |
| 18 | + attributes={ |
| 19 | + "mcp.method": context.method, |
| 20 | + "mcp.source": context.source, |
| 21 | + "mcp.tool.name": tool_name, |
| 22 | + # If arguments are sensitive, consider omitting or sanitizing them |
| 23 | + # If arguments are long/nested, consider adding a size or depth limit |
| 24 | + "mcp.tool.arguments": str(context.message.arguments), |
| 25 | + }, |
| 26 | + ) as span: |
| 27 | + try: |
| 28 | + result = await call_next(context) |
| 29 | + span.set_attribute("mcp.tool.success", True) |
| 30 | + span.set_status(Status(StatusCode.OK)) |
| 31 | + return result |
| 32 | + except Exception as e: |
| 33 | + span.set_attribute("mcp.tool.success", False) |
| 34 | + span.set_attribute("mcp.tool.error", str(e)) |
| 35 | + span.set_status(Status(StatusCode.ERROR, str(e))) |
| 36 | + span.record_exception(e) |
| 37 | + raise |
| 38 | + |
| 39 | + async def on_read_resource(self, context: MiddlewareContext, call_next): |
| 40 | + """Create a span for each resource read.""" |
| 41 | + resource_uri = str(getattr(context.message, "uri", "unknown")) |
| 42 | + |
| 43 | + with self.tracer.start_as_current_span( |
| 44 | + f"resource.{resource_uri}", |
| 45 | + attributes={ |
| 46 | + "mcp.method": context.method, |
| 47 | + "mcp.source": context.source, |
| 48 | + "mcp.resource.uri": resource_uri, |
| 49 | + }, |
| 50 | + ) as span: |
| 51 | + try: |
| 52 | + result = await call_next(context) |
| 53 | + span.set_attribute("mcp.resource.success", True) |
| 54 | + span.set_status(Status(StatusCode.OK)) |
| 55 | + return result |
| 56 | + except Exception as e: |
| 57 | + span.set_attribute("mcp.resource.success", False) |
| 58 | + span.set_attribute("mcp.resource.error", str(e)) |
| 59 | + span.set_status(Status(StatusCode.ERROR, str(e))) |
| 60 | + span.record_exception(e) |
| 61 | + raise |
| 62 | + |
| 63 | + async def on_get_prompt(self, context: MiddlewareContext, call_next): |
| 64 | + """Create a span for each prompt retrieval.""" |
| 65 | + prompt_name = getattr(context.message, "name", "unknown") |
| 66 | + |
| 67 | + with self.tracer.start_as_current_span( |
| 68 | + f"prompt.{prompt_name}", |
| 69 | + attributes={ |
| 70 | + "mcp.method": context.method, |
| 71 | + "mcp.source": context.source, |
| 72 | + "mcp.prompt.name": prompt_name, |
| 73 | + }, |
| 74 | + ) as span: |
| 75 | + try: |
| 76 | + result = await call_next(context) |
| 77 | + span.set_attribute("mcp.prompt.success", True) |
| 78 | + span.set_status(Status(StatusCode.OK)) |
| 79 | + return result |
| 80 | + except Exception as e: |
| 81 | + span.set_attribute("mcp.prompt.success", False) |
| 82 | + span.set_attribute("mcp.prompt.error", str(e)) |
| 83 | + span.set_status(Status(StatusCode.ERROR, str(e))) |
| 84 | + span.record_exception(e) |
| 85 | + raise |
0 commit comments