-
Notifications
You must be signed in to change notification settings - Fork 469
[HealthChecks] Add ScriptHost and WebHost IHealthCheck #11161
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
Merged
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions
52
src/WebJobs.Script/Diagnostics/HealthChecks/ScriptHostHealthCheck.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,52 @@ | ||
| // 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; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks | ||
| { | ||
| internal class ScriptHostHealthCheck(IScriptHostManager manager) : IHealthCheck | ||
| { | ||
| private readonly IScriptHostManager _manager = manager ?? throw new ArgumentNullException(nameof(manager)); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _healthy = | ||
| Task.FromResult(HealthCheckResult.Healthy()); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyNoScriptHost = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("No script host available")); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyNotStarted = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("Script host not started")); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyStopping = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("Script host stopping")); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyStopped = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("Script host stopped")); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyOffline = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("Script host offline")); | ||
|
|
||
| private static readonly Task<HealthCheckResult> _unhealthyUnknown = | ||
| Task.FromResult(HealthCheckResult.Unhealthy("Script host in unknown state")); | ||
|
|
||
| public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| => _manager.State switch | ||
| { | ||
| ScriptHostState.Default => _unhealthyNoScriptHost, | ||
| ScriptHostState.Starting => _unhealthyNotStarted, | ||
| ScriptHostState.Initialized or ScriptHostState.Running => _healthy, | ||
| ScriptHostState.Error => UnhealthyError(_manager.LastError), | ||
| ScriptHostState.Stopping => _unhealthyStopping, | ||
| ScriptHostState.Stopped => _unhealthyStopped, | ||
| ScriptHostState.Offline => _unhealthyOffline, | ||
| _ => _unhealthyUnknown, | ||
| }; | ||
|
|
||
| private static Task<HealthCheckResult> UnhealthyError(Exception ex) | ||
| => Task.FromResult(HealthCheckResult.Unhealthy($"Script host in error state: {Environment.NewLine}{ex.Message}", ex)); | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
src/WebJobs.Script/Diagnostics/HealthChecks/WebHostHealthCheck.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,41 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks | ||
| { | ||
| internal class WebHostHealthCheck(IHostApplicationLifetime lifetime) : IHealthCheck | ||
| { | ||
| private static readonly Task<HealthCheckResult> _healthy = Task.FromResult(HealthCheckResult.Healthy()); | ||
| private static readonly Task<HealthCheckResult> _unhealthyNotStarted = Task.FromResult(HealthCheckResult.Unhealthy("Not Started")); | ||
| private static readonly Task<HealthCheckResult> _unhealthyStopping = Task.FromResult(HealthCheckResult.Unhealthy("Stopping")); | ||
| private static readonly Task<HealthCheckResult> _unhealthyStopped = Task.FromResult(HealthCheckResult.Unhealthy("Stopped")); | ||
|
|
||
| public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| bool isStopped = lifetime.ApplicationStopped.IsCancellationRequested; | ||
| if (isStopped) | ||
| { | ||
| return _unhealthyStopped; | ||
| } | ||
|
|
||
| bool isStopping = lifetime.ApplicationStopping.IsCancellationRequested; | ||
| if (isStopping) | ||
| { | ||
| return _unhealthyStopping; | ||
| } | ||
|
|
||
| bool isStarted = lifetime.ApplicationStarted.IsCancellationRequested; | ||
| if (!isStarted) | ||
| { | ||
| return _unhealthyNotStarted; | ||
| } | ||
|
|
||
| return _healthy; | ||
| } | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
test/WebJobs.Script.Tests/Diagnostics/HealthChecks/ScriptHostHealthCheckTests.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,61 @@ | ||
| // 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 FluentAssertions; | ||
| using Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.HealthChecks | ||
| { | ||
| public class ScriptHostHealthCheckTests | ||
| { | ||
| [Theory] | ||
| [InlineData(ScriptHostState.Default, "No script host available", HealthStatus.Unhealthy)] | ||
| [InlineData(ScriptHostState.Starting, "Script host not started", HealthStatus.Unhealthy)] | ||
| [InlineData(ScriptHostState.Initialized, null, HealthStatus.Healthy)] | ||
| [InlineData(ScriptHostState.Running, null, HealthStatus.Healthy)] | ||
| [InlineData(ScriptHostState.Stopping, "Script host stopping", HealthStatus.Unhealthy)] | ||
| [InlineData(ScriptHostState.Stopped, "Script host stopped", HealthStatus.Unhealthy)] | ||
| [InlineData(ScriptHostState.Offline, "Script host offline", HealthStatus.Unhealthy)] | ||
| [InlineData((ScriptHostState)100, "Script host in unknown state", HealthStatus.Unhealthy)] | ||
| public async Task CheckHealthAsync_MatchesScriptHostHealth( | ||
| ScriptHostState state, string description, HealthStatus status) | ||
| { | ||
| // arrange | ||
| Mock<IScriptHostManager> manager = new(); | ||
| manager.Setup(m => m.State).Returns(state); | ||
| ScriptHostHealthCheck healthCheck = new(manager.Object); | ||
|
|
||
| // act | ||
| HealthCheckResult result = await healthCheck.CheckHealthAsync(new(), default); | ||
|
|
||
| // assert | ||
| result.Status.Should().Be(status); | ||
| result.Description.Should().Be(description); | ||
| result.Exception.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_Error_IncludesException() | ||
| { | ||
| // arrange | ||
| Exception error = new("Some exception message"); | ||
| Mock<IScriptHostManager> manager = new(); | ||
| manager.Setup(m => m.State).Returns(ScriptHostState.Error); | ||
| manager.Setup(m => m.LastError).Returns(error); | ||
| ScriptHostHealthCheck healthCheck = new(manager.Object); | ||
|
|
||
| // act | ||
| HealthCheckResult result = await healthCheck.CheckHealthAsync(new(), default); | ||
|
|
||
| // assert | ||
| result.Status.Should().Be(HealthStatus.Unhealthy); | ||
| result.Description.Should().Be($"Script host in error state: {Environment.NewLine}{error.Message}"); | ||
| result.Exception.Should().Be(error); | ||
| } | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
test/WebJobs.Script.Tests/Diagnostics/HealthChecks/WebHostHealthCheckTests.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,79 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.HealthChecks | ||
| { | ||
| public class WebHostHealthCheckTests | ||
| { | ||
| public enum LifetimeState | ||
| { | ||
| NotStarted = 0, | ||
| Running = 1, | ||
| Stopping = 2, | ||
| Stopped = 3, | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LifetimeState.NotStarted)] | ||
| [InlineData(LifetimeState.Running)] | ||
| [InlineData(LifetimeState.Stopping)] | ||
| [InlineData(LifetimeState.Stopped)] | ||
| public async Task CheckHealthAsync_MatchesApplicationHealth(LifetimeState state) | ||
| { | ||
| // arrange | ||
| IHostApplicationLifetime lifetime = CreateLifetime(state, out string description, out HealthStatus expected); | ||
| WebHostHealthCheck healthCheck = new(lifetime); | ||
|
|
||
| // act | ||
| HealthCheckResult result = await healthCheck.CheckHealthAsync(new(), default); | ||
|
|
||
| // assert | ||
| result.Status.Should().Be(expected); | ||
| result.Exception.Should().BeNull(); | ||
| result.Description.Should().Be(description); | ||
| } | ||
|
|
||
| private static IHostApplicationLifetime CreateLifetime( | ||
| LifetimeState state, out string description, out HealthStatus status) | ||
| { | ||
| Mock<IHostApplicationLifetime> lifetime = new(); | ||
|
|
||
| int lifecycle = (int)state; | ||
|
|
||
| description = "Not Started"; | ||
| status = HealthStatus.Unhealthy; | ||
|
|
||
| if (lifecycle > 0) | ||
| { | ||
| description = null; | ||
| lifetime.Setup(m => m.ApplicationStarted).Returns(new CancellationToken(true)); | ||
| status = HealthStatus.Healthy; | ||
| } | ||
|
|
||
| if (lifecycle > 1) | ||
| { | ||
| description = "Stopping"; | ||
| lifetime.Setup(m => m.ApplicationStopping).Returns(new CancellationToken(true)); | ||
| status = HealthStatus.Unhealthy; | ||
| } | ||
|
|
||
| if (lifecycle > 2) | ||
| { | ||
| description = "Stopped"; | ||
| lifetime.Setup(m => m.ApplicationStopped).Returns(new CancellationToken(true)); | ||
| status = HealthStatus.Unhealthy; | ||
| } | ||
|
|
||
| return lifetime.Object; | ||
| } | ||
| } | ||
| } |
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.