This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1st version of healthcheck & webstatus
- Loading branch information
Showing
104 changed files
with
24,876 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
...uildingBlocks/HealthChecks/src/Microsoft.AspNetCore.HealthChecks/HealthCheckMiddleware.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,68 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.HealthChecks; | ||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.AspNetCore.HealthChecks | ||
{ | ||
public class HealthCheckMiddleware | ||
{ | ||
private RequestDelegate _next; | ||
private string _path; | ||
private int? _port; | ||
private IHealthCheckService _service; | ||
|
||
public HealthCheckMiddleware(RequestDelegate next, IHealthCheckService service, int port) | ||
{ | ||
_port = port; | ||
_service = service; | ||
_next = next; | ||
} | ||
|
||
public HealthCheckMiddleware(RequestDelegate next, IHealthCheckService service, string path) | ||
{ | ||
_path = path; | ||
_service = service; | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (IsHealthCheckRequest(context)) | ||
{ | ||
var result = await _service.CheckHealthAsync(); | ||
var status = result.CheckStatus; | ||
|
||
if (status != CheckStatus.Healthy) | ||
context.Response.StatusCode = 503; | ||
|
||
context.Response.Headers.Add("content-type", "application/json"); | ||
await context.Response.WriteAsync(JsonConvert.SerializeObject(new { status = status.ToString() })); | ||
return; | ||
} | ||
else | ||
{ | ||
await _next.Invoke(context); | ||
} | ||
} | ||
|
||
private bool IsHealthCheckRequest(HttpContext context) | ||
{ | ||
if (_port.HasValue) | ||
{ | ||
var connInfo = context.Features.Get<IHttpConnectionFeature>(); | ||
if (connInfo.LocalPort == _port) | ||
return true; | ||
} | ||
|
||
if (context.Request.Path == _path) | ||
return true; | ||
|
||
return false; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...dingBlocks/HealthChecks/src/Microsoft.AspNetCore.HealthChecks/HealthCheckStartupFilter.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,38 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace Microsoft.AspNetCore.HealthChecks | ||
{ | ||
public class HealthCheckStartupFilter : IStartupFilter | ||
{ | ||
private string _path; | ||
private int? _port; | ||
|
||
public HealthCheckStartupFilter(int port) | ||
{ | ||
_port = port; | ||
} | ||
|
||
public HealthCheckStartupFilter(string path) | ||
{ | ||
_path = path; | ||
} | ||
|
||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
return app => | ||
{ | ||
if (_port.HasValue) | ||
app.UseMiddleware<HealthCheckMiddleware>(_port); | ||
else | ||
app.UseMiddleware<HealthCheckMiddleware>(_path); | ||
next(app); | ||
}; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../HealthChecks/src/Microsoft.AspNetCore.HealthChecks/HealthCheckWebHostBuilderExtension.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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
public static class HealthCheckWebHostBuilderExtension | ||
{ | ||
public static IWebHostBuilder UseHealthChecks(this IWebHostBuilder builder, int port) | ||
{ | ||
Guard.ArgumentValid(port > 0 && port < 65536, nameof(port), "Port must be a value between 1 and 65535"); | ||
|
||
builder.ConfigureServices(services => | ||
{ | ||
var existingUrl = builder.GetSetting(WebHostDefaults.ServerUrlsKey); | ||
builder.UseSetting(WebHostDefaults.ServerUrlsKey, $"{existingUrl};http://localhost:{port}"); | ||
services.AddSingleton<IStartupFilter>(new HealthCheckStartupFilter(port)); | ||
}); | ||
return builder; | ||
} | ||
|
||
public static IWebHostBuilder UseHealthChecks(this IWebHostBuilder builder, string path) | ||
{ | ||
Guard.ArgumentNotNull(nameof(path), path); | ||
// REVIEW: Is there a better URL path validator somewhere? | ||
Guard.ArgumentValid(!path.Contains("?"), nameof(path), "Path cannot contain query string values"); | ||
Guard.ArgumentValid(path.StartsWith("/"), nameof(path), "Path should start with /"); | ||
|
||
builder.ConfigureServices(services => services.AddSingleton<IStartupFilter>(new HealthCheckStartupFilter(path))); | ||
return builder; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...Blocks/HealthChecks/src/Microsoft.AspNetCore.HealthChecks/HealthCheckWebHostExtensions.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,38 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Extensions.HealthChecks; | ||
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
public static class HealthCheckWebHostExtensions | ||
{ | ||
private const int DEFAULT_TIMEOUT_SECONDS = 300; | ||
|
||
public static void RunWhenHealthy(this IWebHost webHost) | ||
{ | ||
webHost.RunWhenHealthy(TimeSpan.FromSeconds(DEFAULT_TIMEOUT_SECONDS)); | ||
} | ||
|
||
public static void RunWhenHealthy(this IWebHost webHost, TimeSpan timeout) | ||
{ | ||
var healthChecks = webHost.Services.GetService(typeof(IHealthCheckService)) as IHealthCheckService; | ||
|
||
var loops = 0; | ||
do | ||
{ | ||
var checkResult = healthChecks.CheckHealthAsync().Result; | ||
if (checkResult.CheckStatus == CheckStatus.Healthy) | ||
{ | ||
webHost.Run(); | ||
break; | ||
} | ||
|
||
System.Threading.Thread.Sleep(1000); | ||
loops++; | ||
|
||
} while (loops < timeout.TotalSeconds); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...althChecks/src/Microsoft.AspNetCore.HealthChecks/Microsoft.AspNetCore.HealthChecks.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\common\Guard.cs" Link="Internal\Guard.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.0.2" /> | ||
<ProjectReference Include="..\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
45 changes: 45 additions & 0 deletions
45
...althChecks/src/Microsoft.Extensions.HealthChecks.Data/HealthCheckBuilderDataExtensions.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,45 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace Microsoft.Extensions.HealthChecks | ||
{ | ||
public static class HealthCheckBuilderDataExtensions | ||
{ | ||
public static HealthCheckBuilder AddSqlCheck(this HealthCheckBuilder builder, string name, string connectionString) | ||
{ | ||
builder.AddCheck($"SqlCheck({name})", async () => | ||
{ | ||
try | ||
{ | ||
//TODO: There is probably a much better way to do this. | ||
using (var connection = new SqlConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
using (var command = connection.CreateCommand()) | ||
{ | ||
command.CommandType = CommandType.Text; | ||
command.CommandText = "SELECT 1"; | ||
var result = (int)await command.ExecuteScalarAsync().ConfigureAwait(false); | ||
if (result == 1) | ||
{ | ||
return HealthCheckResult.Healthy($"SqlCheck({name}): Healthy"); | ||
} | ||
return HealthCheckResult.Unhealthy($"SqlCheck({name}): Unhealthy"); | ||
} | ||
} | ||
} | ||
catch(Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"SqlCheck({name}): Exception during check: {ex.GetType().FullName}"); | ||
} | ||
}); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../src/Microsoft.Extensions.HealthChecks.Data/Microsoft.Extensions.HealthChecks.Data.csproj
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard1.3</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\common\Guard.cs" Link="Internal\Guard.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
13 changes: 13 additions & 0 deletions
13
src/BuildingBlocks/HealthChecks/src/Microsoft.Extensions.HealthChecks/CheckStatus.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,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Extensions.HealthChecks | ||
{ | ||
public enum CheckStatus | ||
{ | ||
Unknown, | ||
Unhealthy, | ||
Healthy, | ||
Warning | ||
} | ||
} |
Oops, something went wrong.