Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs and cleanup some chaos API #1914

Merged
merged 11 commits into from
Jan 23, 2024
31 changes: 31 additions & 0 deletions docs/chaos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@

![Simmy](../media/simmy-logo.png)

## Usage

<!-- snippet: chaos-usage -->
```cs
var builder = new ResiliencePipelineBuilder<HttpResponseMessage>();

// First, configure regular resilience strategies
builder
.AddConcurrencyLimiter(10, 100)
.AddRetry(new())
.AddCircuitBreaker(new())
martincostello marked this conversation as resolved.
Show resolved Hide resolved
.AddTimeout(TimeSpan.FromSeconds(5));

// Finally, configure chaos strategies if you want to inject chaos.
// These should come after the regular resilience strategies.

// 2% of invocations will be injected with chaos
const double InjectionRate = 0.02;

builder
// Inject a chaos latency to executions
.AddChaosLatency(InjectionRate, TimeSpan.FromMinutes(1))
// Inject a chaos fault to executions
.AddChaosFault(InjectionRate, () => new InvalidOperationException("Injected by chaos strategy!"))
// Inject a chaos outcome to executions
.AddChaosResult(InjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError))
// Inject a chaos behavior to executions
.AddChaosBehavior(0.001, () => RestartRedisAsync());
martincostello marked this conversation as resolved.
Show resolved Hide resolved
```
<!-- endSnippet -->

## Motivation

There are a lot of questions when it comes to chaos engineering and making sure that a system is actually ready to face the worst possible scenarios:
Expand Down Expand Up @@ -38,7 +69,7 @@
| [Latency](latency.md) | No | Injects latency into executions before the calls are made. |
| [Behavior](behavior.md) | No | Allows you to inject *any* extra behaviour, before a call is placed. |

## Usage

Check failure on line 72 in docs/chaos/index.md

View workflow job for this annotation

GitHub Actions / publish-docs

Multiple headings with the same content

docs/chaos/index.md:72 MD024/no-duplicate-heading Multiple headings with the same content [Context: "## Usage"] https://github.com/DavidAnson/markdownlint/blob/v0.33.0/doc/md024.md

It is usual to place the chaos strategy as the last strategy in the resilience pipeline. By placing the chaos strategies as last, they subvert the usual outbound call at the last minute, substituting their fault or adding extra latency, etc. The existing resilience strategies - further out in the `ResiliencePipeline` - still apply, so you can test how the Polly resilience strategies you have configured handle the chaos/faults injected by Simmy.

Expand Down
41 changes: 41 additions & 0 deletions src/Snippets/Docs/Chaos.Index.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net.Http;
using Polly.Simmy;

namespace Snippets.Docs;

internal static partial class Chaos
{
public static void Usage()
{
#region chaos-usage

var builder = new ResiliencePipelineBuilder<HttpResponseMessage>();

// First, configure regular resilience strategies
builder
.AddConcurrencyLimiter(10, 100)
.AddRetry(new())
.AddCircuitBreaker(new())
.AddTimeout(TimeSpan.FromSeconds(5));

// Finally, configure chaos strategies if you want to inject chaos.
// These should come after the regular resilience strategies.

// 2% of invocations will be injected with chaos
const double InjectionRate = 0.02;

builder

Check failure on line 27 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 27 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
// Inject a chaos latency to executions

Check failure on line 28 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

Check failure on line 28 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
.AddChaosLatency(InjectionRate, TimeSpan.FromMinutes(1))

Check failure on line 29 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
// Inject a chaos fault to executions

Check failure on line 30 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
.AddChaosFault(InjectionRate, () => new InvalidOperationException("Injected by chaos strategy!"))

Check failure on line 31 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
// Inject a chaos outcome to executions

Check failure on line 32 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
.AddChaosResult(InjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError))

Check failure on line 33 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
// Inject a chaos behavior to executions

Check failure on line 34 in src/Snippets/Docs/Chaos.Index.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
.AddChaosBehavior(0.001, () => RestartRedisAsync());

#endregion
}

private static ValueTask RestartRedisAsync() => ValueTask.CompletedTask;
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}
Loading