Feature Proposal - Add callback to be run when chaos is injected
It would be nice to be able to run code when chaos is injected (by any of the simmy injection policies).
Example use-case:
var chaosExceptionPolicy = MonkeyPolicy
.InjectExceptionAsync(with => with
.Fault(new TimeoutException())
.InjectionRate(0.2)
.Enabled()
.OnInject((context, exception, cancellation) => logger.LogInformation("Injecting Chaos"));
Alternative:
Using InjectBehavior rather than InjectException, and throwing the exception myself would also work without changes in Simmy.
Hey @iCodeSometime that's a great idea (and a nice workaround too by using the InjectBehavior monkey). If you want to contribute, a PR is more than welcome!
If so, here's a rough idea about the implementation: you would need to update the engines (async, sync) and call the delegate after the chaos is injected (behavior, exception or, result). In the case of the exception monkey, you should call the delegate right before throwing the exception (obviously)
You'll also need to update the InjectOptionsBase (both async and sync implementations) and its extensions so that you can expose the OnInject method.