Monitor 3rd party systems with custom checks

Component: ServiceControl CustomChecks
NuGet Package ServiceControl.Plugin.Nsb6.CustomChecks (3.x)
Target NServiceBus Version: 6.x

Code walk-through

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

3rd Party System console

The 3rd party system console application simulates a HTTP service running on http://localhost:57789. Verify that the 3rd party system is running by opening the url in a browser. When no error is received, the 3rd 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 3rd system doesn't respond in a timely fashion a CheckResult.Failed 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 async Task<CheckResult> PerformCheck()
    {
        try
        {
            using (var response = await client.GetAsync(url)
                .ConfigureAwait(false))
            {
                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

  • Custom Check Monitoring
    Describes how ServicePulse uses custom checks to monitor and detect problem that are unique to the solution or endpoint(s) monitored.
  • ServiceControl Endpoint Plugins
    Describes the purpose and internal behavior of the endpoint plugins used by ServiceControl.

Last modified