#pragma warning disable IDE0060 // Remove unused parameter /// <summary> /// Initializes a new instance of the <see cref="HealthCheckMiddleware"/> class. /// </summary> /// <param name="next"> /// Ignored. Required to exist in the constructor in order to meet the definition of a middleware. /// </param> /// <param name="healthCheckRunner"> /// The <see cref="IHealthCheckRunner"/> that evaluates the health of the service. /// </param> /// <param name="indent">Whether to indent the JSON output.</param> public HealthCheckMiddleware(RequestDelegate next, IHealthCheckRunner healthCheckRunner, bool indent) #pragma warning restore IDE0060 // Remove unused parameter { // This is a terminal middleware, so throw away the RequestDelegate. _healthCheckRunner = healthCheckRunner; _indent = indent; }
/// <summary> /// Adds a terminal <see cref="HealthCheckMiddleware"/> to the application. /// </summary> /// <param name="builder">The application builder.</param> /// <param name="healthCheckRunner"> /// The <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/>, /// the value of the <see cref="HealthCheck.GetRunner"/> method is used. /// </param> /// <param name="route">The route of the health endpoint.</param> /// <param name="indent">Whether to indent the JSON output.</param> /// <returns>The application builder.</returns> public static IApplicationBuilder UseRockLibHealthChecks(this IApplicationBuilder builder, IHealthCheckRunner healthCheckRunner, string route = "/health", bool indent = false) { healthCheckRunner = healthCheckRunner ?? HealthCheck.GetRunner(); route = $"/{route.Trim('/')}"; return(builder .Map(new PathString(route), appBuilder => { appBuilder.UseMiddleware <HealthCheckMiddleware>(healthCheckRunner, indent); })); }
/// <summary> /// Sets the <see cref="HealthResponse.StatusCode"/> property of the <paramref name="response"/> /// according to the value of its <see cref="HealthResponse.Status"/> property and the values of /// the <see cref="IHealthCheckRunner.PassStatusCode"/>, <see cref="IHealthCheckRunner.WarnStatusCode"/>, /// and <see cref="IHealthCheckRunner.FailStatusCode"/> properties of the <paramref name="runner"/>. /// </summary> /// <param name="response">The <see cref="HealthResponse"/>.</param> /// <param name="runner">The <see cref="IHealthCheckRunner"/>.</param> /// <returns>The same <see cref="HealthResponse"/> object.</returns> public static HealthResponse SetStatusCode(this HealthResponse response, IHealthCheckRunner runner) { switch (response.Status) { case HealthStatus.Pass: response.StatusCode = runner.PassStatusCode; break; case HealthStatus.Warn: response.StatusCode = runner.WarnStatusCode; break; case HealthStatus.Fail: response.StatusCode = runner.FailStatusCode; break; } return(response); }
/// <summary> /// Creates an instance of <see cref="HealthResponse"/> with its properties set from /// the properties of an instance of <see cref="IHealthCheckRunner"/> and a collection /// of <see cref="HealthCheckResult"/> values. /// </summary> /// <param name="runner">An instance of <see cref="IHealthCheckRunner"/>.</param> /// <param name="results">A collection of <see cref="HealthCheckResult"/> values.</param> /// <returns>An instance of <see cref="HealthResponse"/>.</returns> public static HealthResponse CreateHealthResponse(this IHealthCheckRunner runner, IEnumerable <HealthCheckResult> results) { if (runner == null) { throw new ArgumentNullException(nameof(runner)); } if (results == null) { throw new ArgumentNullException(nameof(results)); } return(new HealthResponse(results) { Description = runner.Description, ServiceId = runner.ServiceId, Version = runner.Version, ReleaseId = runner.ReleaseId, ContentType = runner.ContentType }.SetStatusCode(runner)); }
/// <summary> /// Adds a health check route to the route collection. /// </summary> /// <param name="routes">The route collection.</param> /// <param name="healthCheckRunner"> /// The <see cref="IHealthCheckRunner"/> to use. If <see langword="null"/>, /// the value of the <see cref="HealthCheck.GetRunner"/> method is used. /// </param> /// <param name="route">The route of the health endpoint.</param> /// <param name="indent">Whether to indent the JSON output.</param> public static void MapHealthRoute(this HttpRouteCollection routes, IHealthCheckRunner healthCheckRunner, string route = "/health", bool indent = false) { if (routes == null) { throw new ArgumentNullException(nameof(routes)); } if (string.IsNullOrWhiteSpace(route)) { throw new ArgumentNullException(nameof(route)); } healthCheckRunner = healthCheckRunner ?? HealthCheck.GetRunner(); route = route.Trim('/'); var routeIndex = Interlocked.Increment(ref _routeIndex); var routeName = routeIndex < 1 ? "HealthApi" : $"HealthApi-{routeIndex}"; routes.MapHttpRoute(routeName, route, null, null, new HealthCheckHandler(healthCheckRunner, indent)); }
/// <summary> /// Initializes a new instance of the <see cref="HealthCheckHandler"/> class. /// </summary> /// <param name="healthCheckRunner"> /// The <see cref="IHealthCheckRunner"/> that evaluates the health of the service. /// </param> /// <param name="indent">Whether to indent the JSON output.</param> public HealthCheckHandler(IHealthCheckRunner healthCheckRunner, bool indent) { _healthCheckRunner = healthCheckRunner ?? throw new ArgumentNullException(nameof(healthCheckRunner)); _indent = indent; }
public HealthController(IHealthCheckRunner healthCheckRunner) { _healthCheckRunner = healthCheckRunner; }