Add fault injection support to proxy backend#5
Merged
Conversation
Introduces a --fault CLI flag (proxy backend only) that injects artificial delays or synthetic HTTP error responses into proxied requests, enabling chaos-style failure testing without modifying the target application. Supported rule formats: delay:100ms fixed latency delay:100ms-500ms random latency in range delay:200ms:/api latency only for matching URLs error:503 always return HTTP 503 error:503:0.5 50% probability of HTTP 503 error:500:0.1:/api 10% error on URLs containing "/api" Multiple rules can be combined with repeated --fault flags. Fault-injected traces are recorded in the store with the synthetic status code for later analysis. https://claude.ai/code/session_013JsZoVqikbBRzwiLjDozKM
3 new integration tests in tests/fault_injection.rs that verify
--fault rules work end-to-end via the phantom CLI:
- test_fault_error_always: --fault error:503 returns synthetic HTTP 503
with x-fault-injected header and {"fault":"injected"} body in trace
- test_fault_delay_adds_latency: --fault delay:300ms adds >= 300ms to
the trace duration_ms while forwarding the real backend response
- test_fault_url_pattern_filter: --fault error:503:/api/health injects
503 only for matching URLs; unmatched URLs receive the real response
Also:
- Fix fault-injected trace to include synthetic response headers
(content-type, x-fault-injected) so the trace record is accurate
- Fix clippy/fmt issues in fault.rs
Tests require curl on PATH and clear no_proxy/NO_PROXY so curl routes
127.0.0.1 requests through the phantom proxy regardless of system
noproxy settings.
https://claude.ai/code/session_013JsZoVqikbBRzwiLjDozKM
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
This PR adds comprehensive fault injection capabilities to the proxy backend, allowing users to inject artificial delays and synthetic HTTP errors into proxied requests via CLI flags. This enables testing of client resilience and error handling without modifying the actual backend services.
Key Changes
New
faultmodule (crates/phantom-capture/src/fault.rs):FaultRuleenum supporting two injection types:Delay: Inject fixed or random latency (with optional URL pattern matching)Error: Return synthetic HTTP error responses with configurable probabilityFaultConfigto hold an ordered list of rulesparse_fault_spec()parser supporting flexible CLI syntax:delay:100ms— fixed delaydelay:100ms-500ms— random rangedelay:200ms:/api— conditional on URL patternerror:503— always inject errorerror:500:0.5:/api— probabilistic error with URL filterUpdated
ProxyCaptureBackend(crates/phantom-capture/src/proxy.rs):fault_configfield to store fault ruleswith_faults()builder method for configurationhandle_request():tokio::time::sleep()HttpTracerecords for injected faultsCLI integration (
src/main.rs):--fault SPECflag (repeatable for multiple rules)build_fault_config()to parse and validate fault specificationsModule exports (
crates/phantom-capture/src/lib.rs):FaultConfig,FaultRule, andparse_fault_specfor public useImplementation Details
rand::random()for range selectionx-fault-injected: phantomheader for identificationhttps://claude.ai/code/session_013JsZoVqikbBRzwiLjDozKM