Monitor third-party systems with custom checks

Component: ServiceControl CustomChecks
NuGet Package NServiceBus.CustomChecks (1.x)
Target NServiceBus Version: 5.x

Code walk-through

When integrating with third-party systems, often messages fail when those systems are down. The Particular Platform has extensibility to enable the monitoring of third-party systems which are exposed as HTTP endpoints. This sample shows how to achieve this with custom checks.

Third-party system console

The third-party system console application simulates a HTTP service running on http://localhost:57789. Verify that the third-party system is running by opening the url in a browser. When no error is received, the third-party system is running correctly.

The sample console

The sample console hosts an endpoint instance which has a custom check associated with it.

The custom check

The monitoring capability implements a PeriodicCheck which calls a defined URI every time the specified interval is elapsed. When the third-party system doesn't respond in a timely fashion a CheckResult.Failed event is sent to ServiceControl.

class ThirdPartyMonitor :
    CustomCheck
{
    const string url = "http://localhost:57789";
    static ILog log = LogManager.GetLogger<ThirdPartyMonitor>();
    static HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(3) };

    public ThirdPartyMonitor()
        : base(
            id: $"Monitor {url}",
            category: "Monitor 3rd Party ",
            repeatAfter: TimeSpan.FromSeconds(10))
    {
    }

    public override CheckResult PerformCheck()
    {
        try
        {
            using (var response = client.GetAsync(url).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    log.Info($"Succeeded in contacting {url}");
                    return CheckResult.Pass;
                }
                var error = $"Failed to contact '{url}'. HttpStatusCode: {response.StatusCode}";
                log.Info(error);
                return CheckResult.Failed(error);
            }
        }
        catch (Exception exception)
        {
            var error = $"Failed to contact '{url}'. Error: {exception.Message}";
            log.Info(error);
            return CheckResult.Failed(error);
        }
    }
}

Related Articles


Last modified