-
Notifications
You must be signed in to change notification settings - Fork 466
[HealthChecks] Add health check middleware #11173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+587
−109
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ae13bea
Add health check middleware
jviau 2770507
Fix test nullref
jviau 4f33439
Update deps.json and runtimeassemblies.json
jviau d6999d1
Update ExistingRuntimeAssemblies.txt
jviau ea8b88a
Update deps.json
jviau c6fff79
Extract JsonSerializerOptions to reusable static
jviau 5f40062
Mark sealed
jviau 79ab30a
Address PR comments
jviau 7c2616a
Merge remote-tracking branch 'upstream/dev' into jviau/health-checks-2
jviau a7b60be
Update deps.json
jviau de8c8d0
Add max wait time, respond to RequestAborted
jviau dca7992
Remove unused assembly
jviau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
src/WebJobs.Script.WebHost/Diagnostics/HealthChecks/HealthCheckResponseWriter.cs
This file contains hidden or 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,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using HealthChecks.UI.Client; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.HealthChecks | ||
{ | ||
public class HealthCheckResponseWriter | ||
{ | ||
public static Task WriteResponseAsync(HttpContext httpContext, HealthReport report) | ||
{ | ||
ArgumentNullException.ThrowIfNull(httpContext); | ||
ArgumentNullException.ThrowIfNull(report); | ||
|
||
// We will write a detailed report if ?expand=true is present. | ||
if (httpContext.Request.Query.TryGetValue("expand", out StringValues value) | ||
&& bool.TryParse(value, out bool expand) && expand) | ||
{ | ||
return UIResponseWriter.WriteHealthCheckUIResponse(httpContext, report); | ||
} | ||
|
||
return WriteMinimalResponseAsync(httpContext, report); | ||
} | ||
|
||
private static Task WriteMinimalResponseAsync(HttpContext httpContext, HealthReport report) | ||
{ | ||
MinimalResponse body = new(report.Status); | ||
return JsonSerializer.SerializeAsync( | ||
httpContext.Response.Body, body, JsonSerializerOptionsProvider.Options, httpContext.RequestAborted); | ||
} | ||
|
||
internal readonly struct MinimalResponse(HealthStatus status) | ||
{ | ||
public HealthStatus Status { get; } = status; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/WebJobs.Script.WebHost/Diagnostics/HealthChecks/HealthCheckWaitMiddleware.cs
This file contains hidden or 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,40 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Azure.WebJobs.Script.WebHost.Models; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.HealthChecks | ||
{ | ||
public sealed class HealthCheckWaitMiddleware(RequestDelegate next, IScriptHostManager manager) | ||
{ | ||
private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next)); | ||
private readonly IScriptHostManager _manager = manager ?? throw new ArgumentNullException(nameof(manager)); | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
|
||
// If specified, the ?wait={seconds} query param will wait for an | ||
// active script host for that duration. This is to avoid excessive polling | ||
// when waiting for the initial readiness probe. | ||
if (context.Request.Query.TryGetValue("wait", out StringValues wait)) | ||
{ | ||
if (!int.TryParse(wait.ToString(), out int waitSeconds) || waitSeconds < 0) | ||
jviau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
context.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
await context.Response.WriteAsJsonAsync( | ||
ErrorResponse.BadArgument("'wait' query param must be a positive integer", $"wait={wait}")); | ||
return; | ||
} | ||
|
||
await _manager.DelayUntilHostReadyAsync(waitSeconds); | ||
jviau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
await _next(context); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,63 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.WebHost.Models | ||
{ | ||
/// <summary> | ||
/// Represents an error response. | ||
/// See https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/common-api-details.md#error-response-content. | ||
/// </summary> | ||
/// <param name="Code"> | ||
/// The error code. This is NOT the HTTP status code. | ||
/// Unlocalized string which can be used to programmatically identify the error. | ||
/// The code should be Pascal-cased, and should serve to uniquely identify a particular class of error, | ||
/// for example "BadArgument". | ||
/// </param> | ||
/// <param name="Message"> | ||
/// The error message. Describes the error in detail and provides debugging information. | ||
/// If Accept-Language is set in the request, it must be localized to that language. | ||
/// </param>] | ||
public record ErrorResponse( | ||
[property: JsonProperty("code")][property: JsonPropertyName("code")] string Code, | ||
[property: JsonProperty("message")][property: JsonPropertyName("message")] string Message) | ||
{ | ||
/// <summary> | ||
/// Gets the target of the particular error. For example, the name of the property in error. | ||
/// </summary> | ||
[JsonProperty("target")] | ||
[JsonPropertyName("target")] | ||
public string Target { get; init; } | ||
|
||
/// <summary> | ||
/// Gets the details of this error. | ||
/// </summary> | ||
[JsonProperty("details")] | ||
[JsonPropertyName("details")] | ||
public IEnumerable<ErrorResponse> Details { get; init; } = []; | ||
|
||
/// <summary> | ||
/// Gets the additional information for this error. | ||
/// </summary> | ||
[JsonProperty("additionalInfo")] | ||
[JsonPropertyName("additionalInfo")] | ||
public IEnumerable<ErrorAdditionalInfo> AdditionalInfo { get; init; } = []; | ||
|
||
public static ErrorResponse BadArgument(string message, string target = null) | ||
{ | ||
return new("BadArgument", message) { Target = target }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Represents additional information for an error. | ||
/// </summary> | ||
/// <param name="Type">The type of additional information.</param> | ||
/// <param name="Info">The additional error information.</param> | ||
public record ErrorAdditionalInfo( | ||
[property: JsonProperty("type")][property: JsonPropertyName("type")] string Type, | ||
[property: JsonProperty("info")][property: JsonPropertyName("info")] object Info); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script | ||
{ | ||
/// <summary> | ||
/// Provides constants related to JSON serialization options used. | ||
/// </summary> | ||
public static class JsonSerializerOptionsProvider | ||
{ | ||
/// <summary> | ||
/// Shared Json serializer with the following settings: | ||
/// - AllowTrailingCommas: true | ||
/// - PropertyNamingPolicy: CamelCase | ||
/// - DefaultIgnoreCondition: WhenWritingNull. | ||
/// </summary> | ||
public static readonly JsonSerializerOptions Options = CreateJsonOptions(); | ||
|
||
private static JsonSerializerOptions CreateJsonOptions() | ||
jviau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var options = new JsonSerializerOptions | ||
{ | ||
AllowTrailingCommas = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
}; | ||
|
||
options.Converters.Add(new JsonStringEnumConverter()); | ||
|
||
return options; | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.