Skip to content

Add fault injection support to proxy backend#5

Merged
epli2 merged 2 commits into
mainfrom
claude/add-fault-injection-sJceM
Mar 9, 2026
Merged

Add fault injection support to proxy backend#5
epli2 merged 2 commits into
mainfrom
claude/add-fault-injection-sJceM

Conversation

@epli2
Copy link
Copy Markdown
Owner

@epli2 epli2 commented Mar 9, 2026

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 fault module (crates/phantom-capture/src/fault.rs):

    • Defines FaultRule enum supporting two injection types:
      • Delay: Inject fixed or random latency (with optional URL pattern matching)
      • Error: Return synthetic HTTP error responses with configurable probability
    • Implements FaultConfig to hold an ordered list of rules
    • Provides parse_fault_spec() parser supporting flexible CLI syntax:
      • delay:100ms — fixed delay
      • delay:100ms-500ms — random range
      • delay:200ms:/api — conditional on URL pattern
      • error:503 — always inject error
      • error:500:0.5:/api — probabilistic error with URL filter
    • Includes comprehensive unit tests for all parsing scenarios
  • Updated ProxyCaptureBackend (crates/phantom-capture/src/proxy.rs):

    • Added fault_config field to store fault rules
    • Implemented with_faults() builder method for configuration
    • Integrated fault rule evaluation in handle_request():
      • Applies delays via tokio::time::sleep()
      • Intercepts requests matching error rules and returns synthetic responses
      • Emits proper HttpTrace records for injected faults
      • Uses random number generation for probabilistic injection
  • CLI integration (src/main.rs):

    • Added --fault SPEC flag (repeatable for multiple rules)
    • Implemented build_fault_config() to parse and validate fault specifications
    • Integrated fault config into proxy backend initialization
    • Added comprehensive help text with examples
  • Module exports (crates/phantom-capture/src/lib.rs):

    • Exported FaultConfig, FaultRule, and parse_fault_spec for public use

Implementation Details

  • Fault rules are evaluated in order during request handling
  • URL pattern matching uses simple substring containment (no regex)
  • Delays use rand::random() for range selection
  • Error injection short-circuits normal request forwarding and immediately emits a trace
  • Injected error responses include x-fault-injected: phantom header for identification
  • All parsing errors provide clear, actionable error messages

https://claude.ai/code/session_013JsZoVqikbBRzwiLjDozKM

claude added 2 commits March 9, 2026 10:57
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
@epli2 epli2 merged commit 6e19d28 into main Mar 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants