Add basic OpenTelemetry tracing for client and server requests (#2381)
This commit is contained in:
parent
3ce0f76e6e
commit
37891f42a4
1 changed files with 436 additions and 83 deletions
44
tests/shared/test_otel.py
Normal file
44
tests/shared/test_otel.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from logfire.testing import CaptureLogfire
|
||||
|
||||
from mcp import types
|
||||
from mcp.client.client import Client
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
|
||||
pytestmark = pytest.mark.anyio
|
||||
|
||||
|
||||
# Logfire warns about propagated trace context by default (distributed_tracing=None).
|
||||
# This is expected here since we're testing cross-boundary context propagation.
|
||||
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
|
||||
async def test_client_and_server_spans(capfire: CaptureLogfire):
|
||||
"""Verify that calling a tool produces client and server spans with correct attributes."""
|
||||
server = MCPServer("test")
|
||||
|
||||
@server.tool()
|
||||
def greet(name: str) -> str:
|
||||
"""Greet someone."""
|
||||
return f"Hello, {name}!"
|
||||
|
||||
async with Client(server) as client:
|
||||
result = await client.call_tool("greet", {"name": "World"})
|
||||
|
||||
assert isinstance(result.content[0], types.TextContent)
|
||||
assert result.content[0].text == "Hello, World!"
|
||||
|
||||
spans = capfire.exporter.exported_spans_as_dict()
|
||||
span_names = {s["name"] for s in spans}
|
||||
|
||||
assert "MCP send tools/call greet" in span_names
|
||||
assert "MCP handle tools/call greet" in span_names
|
||||
|
||||
client_span = next(s for s in spans if s["name"] == "MCP send tools/call greet")
|
||||
server_span = next(s for s in spans if s["name"] == "MCP handle tools/call greet")
|
||||
|
||||
assert client_span["attributes"]["mcp.method.name"] == "tools/call"
|
||||
assert server_span["attributes"]["mcp.method.name"] == "tools/call"
|
||||
|
||||
# Server span should be in the same trace as the client span (context propagation).
|
||||
assert server_span["context"]["trace_id"] == client_span["context"]["trace_id"]
|
||||
Loading…
Reference in a new issue