Skip to content

feat(seatbelt): introduce chaos injection middleware#335

Open
martintmk wants to merge 16 commits intomainfrom
chaos-injection
Open

feat(seatbelt): introduce chaos injection middleware#335
martintmk wants to merge 16 commits intomainfrom
chaos-injection

Conversation

@martintmk
Copy link
Member

@martintmk martintmk commented Mar 24, 2026

Add chaos injection middleware

Introduces Injection, a new middleware under seatbelt::chaos::injection that replaces service output with a user-provided value at a configurable probability. Gated behind the chaos-injection feature flag.

API

use seatbelt::chaos::injection::Injection;

// Callback-based: receives consumed input
Injection::layer("my_injection", &context)
    .rate(0.1)
    .output_with(|input, _args| format!("fault for {input}"));

// Fixed value (cloned each time)
Injection::layer("my_injection", &context)
    .rate(0.1)
    .output("injected error".to_string());

Highlights

  • Type-state builder enforces rate + output are set before use
  • Dual Service implslayered::Service and tower_service::Service
  • rate() clamps to [0.0, 1.0] instead of panicking
  • output_with() receives consumed input for context-aware fault generation
  • InjectionConfig with serde support for file-based configuration
  • Telemetryresilience.event counter (chaos.injection) + structured log
  • enable_if / disable for conditional activation

Changes

  • New feature: chaos-injection (depends on fastrand)
  • New module: src/chaos/injection/ (8 files)
  • New example: examples/chaos_injection.rs
  • New integration tests: tests/chaos_injection.rs (18 tests via rstest, layered + tower)
  • Updated lib.rs, context.rs, metrics.rs feature gates

Copilot AI review requested due to automatic review settings March 24, 2026 13:13
@codecov
Copy link

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (9377895) to head (32709e6).

Additional details and impacted files
@@           Coverage Diff            @@
##             main     #335    +/-   ##
========================================
  Coverage   100.0%   100.0%            
========================================
  Files         207      210     +3     
  Lines       15403    15552   +149     
========================================
+ Hits        15403    15552   +149     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new chaos injection middleware to the seatbelt crate, enabling controlled fault/output injection into service pipelines for resilience testing.

Changes:

  • Introduces seatbelt::chaos::injection middleware (layer + service + config + docs) with optional Tower Service support.
  • Adds an example (examples/chaos_injection.rs) and integration tests covering layered + tower execution paths.
  • Extends telemetry/metrics plumbing and feature gating to include the new middleware (and ensures resilience event counter is available for more middleware combinations).

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/seatbelt/tests/chaos_injection.rs Integration tests for chaos injection behavior (layered + tower).
crates/seatbelt/src/metrics.rs Expands cfg gating so resilience event counter is available for additional middleware/features.
crates/seatbelt/src/lib.rs Documents chaos testing, adds chaos module export, updates feature gating.
crates/seatbelt/src/context.rs Allows telemetry helper creation when chaos-injection is enabled.
crates/seatbelt/src/chaos/mod.rs Adds the new top-level chaos module and docs.
crates/seatbelt/src/chaos/injection/telemetry.rs Defines the chaos injection telemetry event name.
crates/seatbelt/src/chaos/injection/service.rs Implements the injection middleware and telemetry emission + unit tests.
crates/seatbelt/src/chaos/injection/mod.rs Public module docs and re-exports for injection.
crates/seatbelt/src/chaos/injection/layer.rs Builder/type-state layer for configuring injection.
crates/seatbelt/src/chaos/injection/config.rs Serializable config for injection middleware + tests.
crates/seatbelt/src/chaos/injection/callbacks.rs Callback wrapper type for injected output factory.
crates/seatbelt/src/chaos/injection/args.rs Callback args type for injected output factory.
crates/seatbelt/examples/chaos_injection.rs Example demonstrating injection usage.
crates/seatbelt/Cargo.toml Adds chaos-injection feature and registers the new example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 24, 2026 13:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 18 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

martintmk and others added 4 commits March 24, 2026 15:01
Replace manual assertions with insta snapshots in config, layer, and
service tests where the pattern is setting values and asserting state:

- config::default_values -> default_snapshot (assert_json_snapshot)
- layer::new_needs_rate_and_output (assert_debug_snapshot)
- layer::config_sets_rate_and_enabled -> config_applies_all_settings (assert_debug_snapshot)
- service::injection_future_debug_contains_struct_name -> injection_future_debug_snapshot (assert_debug_snapshot)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 24, 2026 14:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 23 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@geeknoid
Copy link
Member

Very nice.

What would you think of a variation on this which is "pretend endpoint N is completely dead for the next N seconds". This would provide another tool to test out recovery logic.

@martintmk
Copy link
Member Author

Very nice.

What would you think of a variation on this which is "pretend endpoint N is completely dead for the next N seconds". This would provide another tool to test out recovery logic.

This is already possible with a custom InjectionLayer::output_with that can contain complex logic to extract the some key from the input and fail the execution for a certain period. For now, I keep the actual middleware straightforward.

@geeknoid
Copy link
Member

Maybe add an example then of how to simulate an extended outage?

Copilot AI review requested due to automatic review settings March 25, 2026 14:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 24 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 25, 2026 15:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 24 out of 25 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@martintmk martintmk enabled auto-merge (squash) March 25, 2026 17:59
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.

3 participants