perf: avoid reading HTTP request body before drop/sample decisions#44
Merged
perf: avoid reading HTTP request body before drop/sample decisions#44
Conversation
sohil-kshirsagar
approved these changes
Jan 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimizes inbound HTTP request handling by performing drop transform checks and sampling decisions before reading the request body. Previously, the WSGI handler (and by extension Flask) would capture the full request body upfront, even for requests that would ultimately be dropped or not sampled, resulting in unnecessary I/O.
Problem
The WSGI handler was capturing the full request body before checking:
/health*endpoints)This meant:
wsgi.inputstream was consumed and replaced with aBytesIO, even when not neededSolution
Introduced
should_record_inbound_http_request()utility inmode_utils.pythat consolidates drop and sampling checks into a single pre-flight check. All HTTP server instrumentations now call this before reading the request body.New flow:
Changes
drift/core/mode_utils.py: Addedshould_record_inbound_http_request()utilitydrift/instrumentation/wsgi/handler.py: Pre-flight check beforecapture_request_body()drift/instrumentation/django/middleware.py: Pre-flight check beforerequest.bodyaccessdrift/instrumentation/fastapi/instrumentation.py: Consolidated inline drop/sample checks into shared utilityDesign Decisions
RECORD mode only: Pre-flight checks only apply to RECORD mode. REPLAY mode skips them since the CLI controls what gets replayed.
HTTP-specific naming: Function is named
should_record_inbound_http_request(not genericshould_record_inbound_request) to be explicit about protocol specificity and leave room for futureshould_record_inbound_grpc_requestor similar.Consistent behavior: All three HTTP server instrumentations (WSGI/Flask, Django, FastAPI) now have identical pre-flight check behavior.
Testing