Sentry – first sneak peek

After a few days of a quite intensive coding, I’ve managed to implement the first, basic version of the Sentry project and its API, along with a few (by this I mean 2) fully functional examples. I’ll use the website and MSSQL “watchers”, as these are the only ones that are currently working, however there’s many more coming in the near future. Let’s dive into the code already, as it usually speaks for itself.

 

First example will be the simplest possible:

This few lines of code will setup the Sentry to observe the website for the given url, and MSSQL database for the given connection string, every 5 seconds (by default). Other than that there is not too much potential gain, but that’s just the beginning of the journey. Here comes something a little bit more useful:

As you can see, the watchers are configurable as well. For example, it’s possible to set the condition (a predicate) for which the website and database checks will be found as successful. There are more options of course, yet I just wanted to show that these configuration objects are rather flexible (yes, you can do much more with them than just passing a simple connection string or the url of the website). Still, as refined as it gets, there’s a little use unless we can define what to do when something goes wrong with the validation of the selected resources.

Now, that’s a lot of the code, so what does it do? Well, at first I guess that you’ve already noticed that as far as the configuration goes, you can pass either the lambda expression or the configuration object itself – it’s the default behavior for all of the configurations in the project. The interesting thing (and probably the most useful, after the watchers) are the hooks. Hooks (what a fancy name) are basically the callbacks that are being invoked under the special circumstances. There are 2 kinds of hooks – one that are specific for the watchers (all watchers have the same types of hooks) and the other ones that are part of the main Sentry object. Lets start one by one and find out what this configuration does:

First hook states that whenever an exception occurs, it will be handled by the Logger.Error() method (NLog user here). The second hook means that whenever an iteration is completed (it’s a one cycle that executes all of the watchers) it will be handled by OnIterationCompleted() method. I’ll describe in the future posts what kind of values are being returned here, and how you can make use of it e.g. for custom metrics.

SetGlobalWatcherHooks() does exactly what it says – it defines the hooks that are common for all of the watchers, simple as that. What is interesting here, are these two special types of hooks. What do they do? Well, these callbacks will be invoked only if the watcher execution result differs from the previous one (has a different state). Think of it like this: let’s assume that you want to notify the system administrator when the system goes down, but only for the first time. You don’t want to keep on spamming his email inbox after each failure every 5 seconds (or any other time interval that can be set). And when the system is up and running again and the watcher succeeds (performs a check operation that is valid), it will send an email only once that everything is on the right track.

The first and the last line don’t really need a comment I guess. The mid-part is interesting though. It defines the hooks specific only for that watcher. These are the same types of hooks that we can access using SetGlobalWatcherHooks(), but they will be invoked only for the website watcher. It does not override global hooks. Also, please note that all of the hooks can be defined either as synchronous or asynchronous.

These last two lines of code should be quite easy to understand. By default the Sentry has an infinite loop (at least in theory), however if you want to run it only specific number of times, be my guest. Of course, it can be stopped by invoking the StopAsync() method. You can also define the custom interval (delay) between each iteration. And that’s all for now.
Please let me know what do you think about this whole idea, and feel free to post any comments (ideas, criticism, suggestions, whatever comes to your mind). You can play with the code and examples (console application and windows service) by cloning the repository on theGitHub.

3 Comments Sentry – first sneak peek

  1. Pingback: dotnetomaniak.pl

  2. Radek Maziarka

    Static methods? How will I unit test code with your library? UT above all !!!

    I’m kidding 😉

    Great work – looks simply and approachable. Great opportunity to try it with some microservice’s sandbox.

    Reply

Leave A Comment

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