-
Notifications
You must be signed in to change notification settings - Fork 464
[HealthChecks] Register health check services & checks #11183
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
Open
jviau
wants to merge
4
commits into
dev
Choose a base branch
from
jviau/health-checks-4
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
src/WebJobs.Script/Diagnostics/HealthChecks/HealthCheckExtensions.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,59 @@ | ||
// 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.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks | ||
{ | ||
/// <summary> | ||
/// Health check related extension methods. | ||
/// </summary> | ||
internal static class HealthCheckExtensions | ||
{ | ||
/// <summary> | ||
/// Registers all health check services required for the functions host. Should be called | ||
/// on the WebHost. | ||
/// </summary> | ||
/// <param name="builder">The builder to register health checks with.</param> | ||
/// <returns>The original builder, for call chaining.</returns> | ||
public static IHealthChecksBuilder AddWebJobsScriptHealthChecks(this IHealthChecksBuilder builder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
builder | ||
.AddWebHostHealthCheck() | ||
.AddScriptHostHealthCheck(); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a health check for the web host lifecycle. | ||
/// </summary> | ||
/// <param name="builder">The builder to register health checks with.</param> | ||
/// <returns>The original builder, for call chaining.</returns> | ||
public static IHealthChecksBuilder AddWebHostHealthCheck(this IHealthChecksBuilder builder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
builder.AddCheck<ScriptHostHealthCheck>( | ||
"az.functions.web_host.lifecycle", tags: [HealthCheckTags.Liveness]); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a health check for the script host lifecycle. | ||
/// </summary> | ||
/// <param name="builder">The builder to register health checks with.</param> | ||
/// <returns>The original builder, for call chaining.</returns> | ||
public static IHealthChecksBuilder AddScriptHostHealthCheck(this IHealthChecksBuilder builder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
builder.AddCheck<ScriptHostHealthCheck>( | ||
"az.functions.script_host.lifecycle", tags: [HealthCheckTags.Readiness]); | ||
jviau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return builder; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/WebJobs.Script/Diagnostics/HealthChecks/HealthCheckTags.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,37 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks | ||
{ | ||
/// <summary> | ||
/// Contains tags used for health checks in the Azure Functions host. | ||
/// </summary> | ||
internal static class HealthCheckTags | ||
{ | ||
private const string Prefix = "az.functions."; | ||
|
||
/// <summary> | ||
/// The 'az.functions.liveness' tag is used for liveness checks in the Azure Functions host. | ||
/// </summary> | ||
/// <remarks> | ||
/// Liveness checks are used to determine if the host is alive and responsive. | ||
/// </remarks> | ||
public const string Liveness = Prefix + "liveness"; | ||
|
||
/// <summary> | ||
/// The 'az.functions.readiness' tag is used for readiness checks in the Azure Functions host. | ||
/// </summary> | ||
/// <remarks> | ||
/// Readiness checks are used to determine if the host is ready to process requests. | ||
/// </remarks> | ||
public const string Readiness = Prefix + "readiness"; | ||
|
||
/// <summary> | ||
/// The 'az.functions.configuration' tag is used for configuration-related health checks in the Azure Functions host. | ||
/// </summary> | ||
/// <remarks> | ||
/// These are typically customer configuration related, such as configuring AzureWebJobsStorage access. | ||
/// </remarks> | ||
public const string Configuration = Prefix + "configuration"; | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
test/WebJobs.Script.Tests/Diagnostics/HealthChecks/HealthCheckExtensionsTests.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,102 @@ | ||
// 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.Linq; | ||
using AwesomeAssertions; | ||
using Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.HealthChecks | ||
{ | ||
public class HealthCheckExtensionsTests | ||
{ | ||
[Fact] | ||
public void AddWebJobsScriptHealthChecks_ThrowsOnNullBuilder() | ||
{ | ||
IHealthChecksBuilder builder = null; | ||
Action act = () => HealthCheckExtensions.AddWebJobsScriptHealthChecks(builder); | ||
act.Should().Throw<ArgumentNullException>().WithParameterName("builder"); | ||
} | ||
|
||
[Fact] | ||
public void AddWebHostHealthCheck_ThrowsOnNullBuilder() | ||
{ | ||
IHealthChecksBuilder builder = null; | ||
Action act = () => HealthCheckExtensions.AddWebHostHealthCheck(builder); | ||
act.Should().Throw<ArgumentNullException>().WithParameterName("builder"); | ||
} | ||
|
||
[Fact] | ||
public void AddScriptHostHealthCheck_ThrowsOnNullBuilder() | ||
{ | ||
IHealthChecksBuilder builder = null; | ||
Action act = () => HealthCheckExtensions.AddScriptHostHealthCheck(builder); | ||
act.Should().Throw<ArgumentNullException>().WithParameterName("builder"); | ||
} | ||
|
||
[Fact] | ||
public void AddWebJobsScriptHealthChecks_RegistersBothHealthChecks() | ||
{ | ||
// arrange | ||
Mock<IHealthChecksBuilder> builder = new(MockBehavior.Strict); | ||
builder.Setup(b => b.Add(It.IsAny<HealthCheckRegistration>())).Returns(builder.Object); | ||
|
||
// act | ||
IHealthChecksBuilder returned = builder.Object.AddWebJobsScriptHealthChecks(); | ||
|
||
// assert | ||
returned.Should().BeSameAs(builder.Object); | ||
builder.Verify(b => b.Add(It.Is<HealthCheckRegistration>(r => | ||
r.Name == "az.functions.web_host.lifecycle" && | ||
r.Tags.Contains(HealthCheckTags.Liveness) && | ||
r.Factory != null)), Times.Once); | ||
builder.Verify(b => b.Add(It.Is<HealthCheckRegistration>(r => | ||
r.Name == "az.functions.script_host.lifecycle" && | ||
r.Tags.Contains(HealthCheckTags.Readiness) && | ||
r.Factory != null)), Times.Once); | ||
builder.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public void AddWebHostHealthCheck_RegistersWebHostHealthCheck() | ||
{ | ||
// arrange | ||
Mock<IHealthChecksBuilder> builder = new(MockBehavior.Strict); | ||
builder.Setup(b => b.Add(It.IsAny<HealthCheckRegistration>())).Returns(builder.Object); | ||
|
||
// act | ||
IHealthChecksBuilder returned = builder.Object.AddWebHostHealthCheck(); | ||
|
||
// assert | ||
returned.Should().BeSameAs(builder.Object); | ||
builder.Verify(b => b.Add(It.Is<HealthCheckRegistration>(r => | ||
r.Name == "az.functions.web_host.lifecycle" && | ||
r.Tags.Contains(HealthCheckTags.Liveness) && | ||
r.Factory != null)), Times.Once); | ||
builder.VerifyNoOtherCalls(); | ||
} | ||
|
||
[Fact] | ||
public void AddScriptHostHealthCheck_RegistersScriptHostHealthCheck() | ||
{ | ||
// arrange | ||
Mock<IHealthChecksBuilder> builder = new(MockBehavior.Strict); | ||
builder.Setup(b => b.Add(It.IsAny<HealthCheckRegistration>())).Returns(builder.Object); | ||
|
||
// act | ||
IHealthChecksBuilder returned = builder.Object.AddScriptHostHealthCheck(); | ||
|
||
// assert | ||
returned.Should().BeSameAs(builder.Object); | ||
builder.Verify(b => b.Add(It.Is<HealthCheckRegistration>(r => | ||
r.Name == "az.functions.script_host.lifecycle" && | ||
r.Tags.Contains(HealthCheckTags.Readiness) && | ||
r.Factory != null)), Times.Once); | ||
builder.VerifyNoOtherCalls(); | ||
} | ||
} | ||
} |
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 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.