Publishing health status messages

When our health timer elapses, our _healthTimer_Elapsed method will be called. When this happens, we will create our HealthStatusMessage with all the information populated, and then publish the message. Remember, no DateTime. Now, anywhere within our codebase:

private void _healthTimer_Elapsed(object sender, 
ElapsedEventArgs e)
{
HealthStatusMessage h = new HealthStatusMessage
{
ID = ID,
memoryUsed = Environment.WorkingSet,
CPU = Convert.ToDouble(getCPUCounter()),
date = SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc().ToLocalTime(),
serviceName = "Deployment Monitor MicroService",
message = "OK",
status = (int) MSStatus.Healthy
};
PublishMessage(h, "EvolvedAI", "");
}

When we publish this HealthStatusMessage, we will include the CPU and memory usage of our program. I have found that it's great to have this information available should you ever have an errant or zombie microservice that has a mind of its own. This will give you a detailed history of how your microservice performed up to a point of failure if one occurs. Just store the information into a SQL or NoSQL database for historical purposes and you are all set. I can't tell you how many times I have seen a microservice not responding and the first indication is that memory usage is unusually high. Plotting a trend line on a chart showing the memory usage, or alternatively a histogram, is also another big value add if it's ever needed. And trust me on this one, it's never needed until something goes wrong!

As soon as the microservice receives its deployment start message, it kicks off a deployment timer, and uses that time to monitor the length of the deployment. In our sample, if that timer expires after 15 minutes without us receiving the deployment stop message, we know that the deployment took too long, and we need to alert the user. I'll leave it as an exercise to the reader to make this parameter passable to the function. You could also pass an action to denote what to do when and if the deployment does take too long. You could send an email, log it into a SQL or NoSQL database, trigger a Syslog or Windows event viewer message, and so much more. This is one area where you get to customize this microservice to best fit your individual needs!

Here is what our deployment timer event looks like:

private void _deploymentTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_deploymentInProgress)
Console.WriteLine("ERROR: Deployment is taking too long");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset