$ show jazzband/django-silk
django-silk is a live profiling and inspection tool for Django web applications. In production, it integrates as middleware to intercept every HTTP request/response, capturing request/response bodies, headers, database queries (via monkey-patching SQLCompiler.execute_sql), and code profiling data (via a decorator, context manager, or dynamic injection). It stores this data in Django ORM models (Request, Response, SQLQuery, Profile) with optional file-based storage for binary profiler results, and provides a comprehensive web UI with filtering, sorting, SQL analysis (including EXPLAIN), code generation (curl/Django test client), and graph visualization (using gprof2dot). The architecture centers on a singleton DataCollector that accumulates data during request processing and persists it upon response completion, with configurable retention policies via management commands and model-level garbage collection.
The LLM identified 4 files worth highlighting for reuse:
Implements a thread-safe Singleton metaclass (`Singleton`). Used internally by `DataCollector` and `SilkyConfig`. This is a clean, isolated implementation of the Singleton pattern that can be extracted and reused in any Python project requiring a single instance with controlled instantiation. Demonstrates metaclass-based design and proper handling of singleton initialization.
Generates a curl command-line string from a captured Django request (method, path, headers, body). Isolated utility that takes a `silk.models.Request` instance and produces a executable curl command. Reusable for debugging, documentation, or test case generation in any project dealing with HTTP request replay or export. Demonstrates careful handling of headers, body encoding, and escaping.
Provides `ProfilerResultStorage`, a subclass of Django's `FileSystemStorage` with configuration options via Django's `STORAGES` setting or a custom `SILKY_STORAGE_CLASS`. Allows storing profiling result files (`.prof`) on different backends without modifying core logic. Reusable pattern for abstracting file storage in any Django app that needs configurable, pluggable storage for generated files.
Parses the raw text output of `cProfile.run()` into a structured list of dictionaries with function names, call counts, time, and cumulative time. Pure function with no dependencies on the rest of Silk. Reusable for any project that needs to consume cProfile output programmatically, e.g., for custom analysis or dashboards. Demonstrates robust string parsing and data normalization.