Sentry – the MongoDB watcher

If you’re looking for a simple service for monitoring your MongoDB (things like opening a connection to the database or executing a query and processing the results), then you might find interesting a new type of the watcher added to the Sentry. The MongoDB watcher uses the C# MongoDB Driver under the hood but it’s been implemented in a way, that it’s possible to provide any driver that you’d like.
In this post I’ll present how to use it and why it’s been implemented this way instead of another one.


As usual, let’s start with the minimalistic configuration:

By using such configuration you will already get the notifications (as long as you make use of the hooks) related to opening a connection to the database. Please note that the timeout is an optional parameter (by default it’s 5 seconds).
And what if you’d like to run a query and then process its results in order to make sure that the data is valid? You can do it like this:

When you call the WithQuery(), it’s required to pass a name of the collection and the query itself, which has a json format – in that case it will search the Users to find a particular user who has a name admin. Then, via invoking the EnsureThat() you can process the result of such query and provide a predicate that has to be met in order to return a valid watcher check result.
Quite simple, isn’t it?

There are more options of course but the ones listed in these 2 examples will be probably the most common.
In case you were wondering what are these mentioned interfaces, please take a look at the code below (for the sake of keeping a clean examples I’ve removed comments in code):

With these 2 interfaces you can provide your own implementation of the underlying MongoDB driver (and what’s even more important, I can easily mock such interfaces for the testing purposes ;)). Actually you can either provide the IMongoDbConnection that will return the custom IMongoDb or just set the provider for IMongoDb if you don’t care about the connection interface and can handle everything on your own.

And what about the dynamic type? Well, I’m using this one currently in all of the watchers, simply because it can be resolved at a runtime, which means that it’s a no-brainer to implement such functionality and therefore all of the watchers have quite similar behavior for validating the result with the EnsureThat().
To find out more about this particular watcher you can read the wiki docs here.

There are more watchers coming soon (for example the one for Redis is almost completed) and some additional functionalities (totally different than watchers) that should significantly increase your productivity while using the Sentry. Stay tuned as some cool features will be posted in a near future.

1 Comment Sentry – the MongoDB watcher

  1. Pingback:

Leave A Comment

Your email address will not be published. Required fields are marked *