In today’s post, I’ll briefly describe one of the most recent watchers responsible for the Redis monitoring. Of course, it is a part of the Warden project, therefore, all of the features such as hooks and integrations are available. Let’s not waste anymore time and start with the code examples.
Let me start with a classic, easiest configuration possible which may look like this:
1 2 3 |
var configuration = RedisWatcherConfiguration .Create("localhost", 1) .Build(); |
What it does? For the connection string localhost and database id 1 it will monitor its availability by a simple heartbeat (ping).
Can we do anything more sophisticated? Certainly, we can:
1 2 3 4 5 |
var configuration = RedisWatcherConfiguration .Create("localhost", 1) .WithQuery("get test") .EnsureThat(results => results.Any(x => x == "test-value")) .Build(); |
In this scenario, it will run a command get test and then validate that this key contains the value test-value. For now, the subset of commands is very, very limited, only get, set, lrange are available, but there will be more added in a future (if they will be needed).
Thanks to the latest improvements, in order to add and configure the Redis watcher you can make use of one of the many available extension methods, for example:
1 2 3 4 5 6 7 8 |
var wardenConfiguration = WardenConfiguration .Create() .AddRedisWatcher("localhost", 1, cfg => { cfg.WithQuery("get test") .EnsureThat(results => results.Any(x => x == "test-value")); }) //Configure other watchers, hooks etc. |
To install the package, which is currently in the prerelease version, run the following command in the Package Manager:
1 |
Install-Package Warden.Watchers.Redis -Pre |
As for the custom configuration, the following interfaces are available to be implemented, if you feel a need to provide your own Redis connector:
1 2 3 4 5 6 7 8 9 10 11 |
public interface IRedisConnection { string ConnectionString { get; } TimeSpan Timeout { get; } Task GetDatabaseAsync(int database); } public interface IRedis { Task<IEnumerable> QueryAsync(string query); } |
By default it uses the StackExchange.Redis library to connect to the Redis and run the commands.
If you’re interested about more technical details and available configuration, please read the wiki docs.