mcp: add structured analytics logging - #319
nuclearcat wants to merge 1 commit into
Conversation
Logging is required to provide metrics to teams so they can understand use cases and identify bottlenecks early. Record tool usage, outcomes, and latency along with server lifecycle events as JSON, without including arguments, results, credentials, or exception messages. Write analytics to stderr by default and support an append-only log file through --log-file. Document the logging behavior and cover analytics with focused tests. Assisted-by: OpenAI Codex Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
| call_id = uuid.uuid4().hex | ||
| outcome = "error" | ||
| try: | ||
| result = await call_tool(request) |
There was a problem hiding this comment.
Existing tools use tool_offload(), whose worker execution is shielded from cancellation. Check for pending cancellation after the handler returns, and add a protocol-level cancellation test using a real registered tool. The current test only covers an artificial async tool.
| def analytics_logging(path=None): | ||
| """Configure only our analytics logger; keep stdout free for MCP.""" | ||
| handler = ( | ||
| logging.FileHandler(path, encoding="utf-8") |
There was a problem hiding this comment.
The documentation delegates rotation externally, but FileHandler never reopens a replaced file. I reproduced this by renaming the log and creating its replacement: subsequent events went into the rotated file, while the current file stayed empty. Use WatchedFileHandler, provide a reopen mechanism, or explicitly document the supported rotation procedure.
| try: | ||
| yield | ||
| finally: | ||
| logger.handlers, logger.level, logger.propagate = previous |
There was a problem hiding this comment.
Assigning logger.level directly leaves Python’s enabled-level cache stale. After restoring WARNING, I confirmed that INFO events were still emitted. Restore the level through logger.setLevel(previous_level) and test logging after the context exits.
Logging is required to provide metrics to teams so they can understand use cases and identify bottlenecks early. Record tool usage, outcomes, and latency along with server lifecycle events as JSON, without including arguments, results, credentials, or exception messages.
Write analytics to stderr by default and support an append-only log file through --log-file. Document the logging behavior and cover analytics with focused tests.
Assisted-by: OpenAI Codex