-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from AntonioFalcao/release
Adding Readiness and Liveness health checks.
- Loading branch information
Showing
12 changed files
with
212 additions
and
17 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
70 changes: 70 additions & 0 deletions
70
....GraphQL3.Store.WebAPI/Extensions/EndpointRouteBuilders/EndpointRouteBuilderExtensions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Newtonsoft.Json; | ||
|
||
namespace Dotnet5.GraphQL3.Store.WebAPI.Extensions.EndpointRouteBuilders | ||
{ | ||
public static class EndpointRouteBuilderExtensions | ||
{ | ||
public static void MapLivenessHealthChecks(this IEndpointRouteBuilder endpoints) | ||
=> endpoints.MapHealthChecks( | ||
pattern: "/health/live", | ||
options: new HealthCheckOptions | ||
{ | ||
AllowCachingResponses = false, | ||
ResponseWriter = WriteHealthCheckLiveResponseAsync, | ||
Predicate = registration => registration.Tags.Any() is false | ||
}); | ||
|
||
public static void MapReadinessHealthChecks(this IEndpointRouteBuilder endpoints) | ||
=> endpoints.MapHealthChecks( | ||
pattern: "/health/ready", | ||
options: new HealthCheckOptions | ||
{ | ||
AllowCachingResponses = false, | ||
ResponseWriter = WriteHealthCheckReadyResponseAsync, | ||
Predicate = registration => registration.Tags.Contains("ready"), | ||
ResultStatusCodes = | ||
{ | ||
[HealthStatus.Healthy] = StatusCodes.Status200OK, | ||
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError, | ||
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable | ||
} | ||
}); | ||
|
||
private static Task WriteHealthCheckReadyResponseAsync(HttpContext httpContext, HealthReport healthReport) | ||
{ | ||
httpContext.Response.ContentType = "application/json"; | ||
|
||
return httpContext.Response.WriteAsync( | ||
JsonConvert.SerializeObject(new | ||
{ | ||
OverallStatus = healthReport.Status.ToString(), | ||
TotalCheckDuration = healthReport.TotalDuration.TotalSeconds.ToString("00:00:00.000"), | ||
DependencyHealthChecks = healthReport.Entries.Select( | ||
dependency => new | ||
{ | ||
Name = dependency.Key, | ||
Status = dependency.Value.Status.ToString(), | ||
Duration = dependency.Value.Duration.TotalSeconds.ToString("00:00:00.000") | ||
}) | ||
})); | ||
} | ||
|
||
private static Task WriteHealthCheckLiveResponseAsync(HttpContext httpContext, HealthReport healthReport) | ||
{ | ||
httpContext.Response.ContentType = "application/json"; | ||
return httpContext.Response.WriteAsync( | ||
JsonConvert.SerializeObject(new | ||
{ | ||
OverallStatus = healthReport.Status.ToString(), | ||
TotalCheckDuration = healthReport.TotalDuration.TotalSeconds.ToString("00:00:00.000") | ||
})); | ||
} | ||
} | ||
} |
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
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
70 changes: 70 additions & 0 deletions
70
....GraphQL3.Store.WebMVC/Extensions/EndpointRouteBuilders/EndpointRouteBuilderExtensions.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Newtonsoft.Json; | ||
|
||
namespace Dotnet5.GraphQL3.Store.WebMVC.Extensions.EndpointRouteBuilders | ||
{ | ||
public static class EndpointRouteBuilderExtensions | ||
{ | ||
public static void MapLivenessHealthChecks(this IEndpointRouteBuilder endpoints) | ||
=> endpoints.MapHealthChecks( | ||
pattern: "/health/live", | ||
options: new HealthCheckOptions | ||
{ | ||
AllowCachingResponses = false, | ||
ResponseWriter = WriteHealthCheckLiveResponseAsync, | ||
Predicate = registration => registration.Tags.Any() is false | ||
}); | ||
|
||
public static void MapReadinessHealthChecks(this IEndpointRouteBuilder endpoints) | ||
=> endpoints.MapHealthChecks( | ||
pattern: "/health/ready", | ||
options: new HealthCheckOptions | ||
{ | ||
AllowCachingResponses = false, | ||
ResponseWriter = WriteHealthCheckReadyResponseAsync, | ||
Predicate = registration => registration.Tags.Contains("ready"), | ||
ResultStatusCodes = | ||
{ | ||
[HealthStatus.Healthy] = StatusCodes.Status200OK, | ||
[HealthStatus.Degraded] = StatusCodes.Status500InternalServerError, | ||
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable | ||
} | ||
}); | ||
|
||
private static Task WriteHealthCheckReadyResponseAsync(HttpContext httpContext, HealthReport healthReport) | ||
{ | ||
httpContext.Response.ContentType = "application/json"; | ||
|
||
return httpContext.Response.WriteAsync( | ||
JsonConvert.SerializeObject(new | ||
{ | ||
OverallStatus = healthReport.Status.ToString(), | ||
TotalCheckDuration = healthReport.TotalDuration.TotalSeconds.ToString("00:00:00.000"), | ||
DependencyHealthChecks = healthReport.Entries.Select( | ||
dependency => new | ||
{ | ||
Name = dependency.Key, | ||
Status = dependency.Value.Status.ToString(), | ||
Duration = dependency.Value.Duration.TotalSeconds.ToString("00:00:00.000") | ||
}) | ||
})); | ||
} | ||
|
||
private static Task WriteHealthCheckLiveResponseAsync(HttpContext httpContext, HealthReport healthReport) | ||
{ | ||
httpContext.Response.ContentType = "application/json"; | ||
return httpContext.Response.WriteAsync( | ||
JsonConvert.SerializeObject(new | ||
{ | ||
OverallStatus = healthReport.Status.ToString(), | ||
TotalCheckDuration = healthReport.TotalDuration.TotalSeconds.ToString("00:00:00.000") | ||
})); | ||
} | ||
} | ||
} |
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
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