Graham Dumpleton: Tracing Flas... Note

Graham Dumpleton: Tracing Flask with wrapture

Tracing a web application's request involves capturing the HTTP request details and all calls made during its handling as a hierarchical tree. WSGI applications present challenges for tracing because status and headers are passed via a callback, the body is an iterable consumed after the call, and exceptions are handled internally. A Flask application example demonstrates this, with routes for health checks, quotes, and orders. The configuration for tracing now includes an [[instrument]] entry to specify Flask instrumentation. This setup allows for detailed observation of request flows. When a Flask request is processed, the trace shows the request line initiating the tree, followed by nested calls representing application logic. Errors are captured through exception handling within Flask, allowing instrumentation to note them against the request event. To manage noise, specific paths like health checks can be ignored, preventing them from generating trace data. Flask extensions' lifecycle callbacks can also be optionally excluded from observation. The Flask instrumentation works by applying bindings to key points in Flask's execution, including its WSGI middleware, route handling, and exception management. The core unit of tracing is the request event, which contains all relevant data, including status, duration, and HTTP details. This detailed tracing enables analysis of where application time is being spent.