Probing is one of the method to monitor application to indicate its healthiness. In previous article that it was mentioned on adding health check in application. This article will introduce in deep on create custom probe.
- Create probe to store health check information.
In application, create new class named `HealthCheckProbe`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characterspublic class HealthCheckProbe { /// <summary> /// Health check component name. /// </summary> public string Component { get; set; } /// <summary> /// Health check component description. /// </summary> public string Description { get; set; } /// <summary> /// Health check component status. /// </summary> public string Status { get; set; } /// <summary> /// Time used on the health check. /// </summary> public TimeSpan Duration { get; set; } /// <summary> /// Captured expection name if unhealthy detected. /// </summary> public string Exception { get; set; } /// <summary> /// Unhealthy diagnosis message. /// </summary> public string Message { get; set; } } - Create Health check response message and health check probes.
In application, create new class namedHealthCheckResponse
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters/// <summary> /// Health check response. /// </summary> public class HealthCheckResponse { /// <summary> /// Overall status. /// </summary> public string Status { get; set; } /// <summary> /// Detail status for each health check probes. /// </summary> public IEnumerable<HealthCheckProbe> HealthCheckProbes { get; set; } /// <summary> /// Health check time used. /// </summary> public TimeSpan Duration { get; set; } } - Update API
/healthcheck
response to serializedHealthCheckResponse
in JSON format.
In application, update methodConfigurate()
inStartup.cs
as below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characterspublic void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseHealthChecks("/health", new HealthCheckOptions { ResponseWriter = async (context, report) => { context.Response.ContentType = "application/json"; HealthCheckResponse response = new HealthCheckResponse { Status = report.Status.ToString(), HealthCheckProbes = report.Entries.Select(o => new HealthCheckProbe { Component = o.Key, Description = o.Value.Description, Status = o.Value.Status.ToString(), Duration = o.Value.Duration, Exception = o.Value.Exception == null ? string.Empty : o.Value.Exception.ToString(), Message = o.Value.Exception == null ? string.Empty : o.Value.Exception.Message, }), Duration = report.TotalDuration, }; await context.Response.Body.WriteAsync(JsonSerializer.SerializeToUtf8Bytes(response)); } }); } - Create custom probe for health-checking application. This example will check with URL to return HTTP 200 and string as well.
In application, create new class namedDataSourceProbe
.
- Register probe to application.
In application, update methodConfigurateService()
inStartup.cs
as below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characterspublic void ConfigureServices(IServiceCollection services) { services.AddHealthChecks().AddCheck<DataSourceHealthCheckProbe>(name: "Data source health check"); } - Test application and verify result.
Run application in debug mode, call endpoint/health
, expected JSON will return with probe details.
Leave a Reply