Culture

New Wrapture Library Simplifies Python Monkey Patching

Developer Graham Dumpleton has released Wrapture, an alpha Python monkey patching library that combines unit testing and live application tracing to streamline debugging and observability.

Simon Willison1 day agoCulture
Illustration generated for this story

Software engineer Graham Dumpleton has introduced Wrapture, a new Python monkey patching library designed to unify unit testing and observability. Initially released on August 31st, the alpha-stage software allows developers to mock code for tests while simultaneously offering deep tracing capabilities similar to commercial application performance monitoring tools. Wrapture can patch callables, attributes, dictionaries, and generators, making it a highly versatile utility for Python environments.

One of the library's standout features is its support for zero-code tracing. By using a separate TOML configuration file, developers can instrument and monitor their applications without modifying any of their existing Python source code. Wrapture can capture method calls chronologically and visualize them as hierarchical trees, as well as manage phased behaviors where patched methods change their output across multiple consecutive calls. It also includes tools to track execution times, both for individual calls and as aggregated metrics, to help developers pinpoint slow code.

To extend its utility, the ecosystem includes a companion package called wrapture-instrumentation. This package provides out-of-the-box instrumentation for a wide array of popular Python frameworks and libraries, including django, flask, fastapi, sqlalchemy, sqlite3, httpx, requests, and uvicorn. It also supports aiohttp.client, aiohttp.web, grpc, http.client, jinja2, starlette, urllib.request, urllib3, werkzeug.serving, wsgiref.simple_server, xmlrpc.client, and xmlrpc.server. For teams with existing monitoring pipelines, Wrapture supports exporting trace data directly to OpenTelemetry.

For Python practitioners, Wrapture bridges the gap between writing mock tests and diagnosing live production issues. Industry observer Simon Willison described the package as a "Swiss Army Knife" that will provide long-term value across various programming challenges. Dumpleton has supported the launch with daily tutorials and interactive workshops built as JupyterLab notebooks, helping developers quickly adopt the tool despite its early alpha status.

This is our own summary of reporting by Simon Willison

More in Culture