Quite recently I’ve started working on new features for the Warden project and published the first prelease version to the NuGet (along with other packages that are meant to be additional plugins). In this post, I will discuss the available extensions that are basically responsible for making the fluent API even more fluent and the integrations that are a totally new thing, implemented as separate plugins (similarly to the watchers), which provide a quick access to the external services that can be part of the Warden (e.g. the SendGrid).
Let’s start with the extensions. Lately, there was a code like this (which is still valid of course):
1 2 3 4 5 6 7 8 9 10 11 |
var configuration = MsSqlWatcherConfiguration .Create(@"Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True") .WithQuery("select * from users where id = @id", new Dictionary<string, object> {["id"] = 1 }) .EnsureThat(users => users.Any(user => user.Name == "admin")) .Build(); var mssqlWatcher = MsSqlWatcher.Create("My MSSQL watcher", configuration); var wardenConfiguration = WardenConfiguration .Create() .AddWatcher(mssqlWatcher) //Configure other watchers, hooks etc. |
As you can see, it is responsible for creating a new watcher and then adding it to the configuration of the Warden by using the generic AddWatcher().
From now on, there’s another way to do it (inspired with other frameworks such as the MVC and its middleware):
1 2 3 4 5 6 7 8 |
var wardenConfiguration = WardenConfiguration .Create() .AddMsSqlWatcher(@"Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True", cfg => { cfg.WithQuery("select * from users where id = @id", new Dictionary<string, object> {["id"] = 1}) .EnsureThat(users => users.Any(user => user.Name == "admin")); }) //Configure other watchers, hooks etc. |
The code example above fits nicely (at least in my opinion) into the whole ecosystem – it’s just another extension available while configuring the Warden.
Please note that you may either use lambda expression for configuring the particular watcher or provide the configuration object (like in the first example) – just feel free to use either of the mentioned options interchangeably. The point is that now you have another way to setup the monitoring plugins.
Now comes the second part, which would be the integrations.
An integration (as the name states) is just an external service configured to work with the Warden. This stuff is still a little bit in the experimental phase and I’ll describe it in more depth in the upcoming posts, however if you’d like to find out more about the current implementation click here. So what can we do with integrations? Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var wardenConfiguration = WardenConfiguration .Create() .IntegrateWithSendGrid("api-key", "noreply@system.com", cfg => { cfg.WithDefaultSubject("Monitoring status") .WithDefaultReceivers("admin@system.com"); }) .SetAggregatedWatcherHooks((hooks, integrations) => { hooks.OnFirstFailureAsync(results => integrations.SendGrid().SendEmailAsync("Monitoring errors have occured.")) .OnFirstSuccessAsync(results => integrations.SendGrid().SendEmailAsync("Everything is up and running again!")); }) //Configure watchers, hooks etc.. |
As you can see in the example above, we’re adding the new integration for SendGrid mailing service (which likewise the watchers can be setup via the generic method AddIntegration()).
For each hooks configuration, there’s a brand new, overloaded method that has these integrations provider. What is it? If you take a look at its interface it should be very straightforward:
1 2 3 4 5 6 7 8 9 10 |
//Marker interface public interface IIntegration { } public interface IIntegrator { void Register<T>(T integration) where T : class, IIntegration; T Resolve<T>() where T : class, IIntegration; } |
You can think of it as a simple (actually, very trivial) type of an IoC container and you can definitely provide your own implementation by using the SetIntegratorProvider() while configuring the Warden. Its only job is to either register or resolve the new integration and in that case, we can access the SendGrid integration via custom extension method (fluent API all the way).
To sum up – Warden now supports both watchers that are responsible for monitoring and integrations that can make your life simpler (and change the world of course) by allowing to quickly access some external services (like mailing and in the future maybe Skype or Slack) in order to e.g. notify about any system failures.
“Select * […]” is pretty much always bad idea 🙂
Sure thing it is :). Just want to keep the code examples as simple as possible ;).
Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.
Dai Software
Keep Posting:)