Skip to content

Commit

Permalink
Make HealthCheck compatible with ALV (#884)
Browse files Browse the repository at this point in the history
* Make HealthCheck compatible with ALV

* Add Unit Test for V3 Health conversion
  • Loading branch information
hananiel authored Jul 18, 2022
1 parent 1bc5589 commit bc267da
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 60 deletions.
13 changes: 3 additions & 10 deletions src/Management/Management.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B86A4082-6F42-4BFC-A1F2-83CC422AD493}"
EndProject
Expand Down Expand Up @@ -84,9 +84,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Management.Kuberne
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Extensions.Logging.DynamicSerilogCore", "..\Logging\src\DynamicSerilogCore\Steeltoe.Extensions.Logging.DynamicSerilogCore.csproj", "{D9250B38-5792-4534-BC15-E345E95E68F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.OpenTelemetry.Test", "test\OpenTelemetry.Test\Steeltoe.OpenTelemetry.Test.csproj", "{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Steeltoe.Common.Utils", "..\Common\src\Common.Utils\Steeltoe.Common.Utils.csproj", "{821340BF-7830-4F73-A1C9-E38065152482}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common.Utils", "..\Common\src\Common.Utils\Steeltoe.Common.Utils.csproj", "{821340BF-7830-4F73-A1C9-E38065152482}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -214,10 +212,6 @@ Global
{D9250B38-5792-4534-BC15-E345E95E68F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9250B38-5792-4534-BC15-E345E95E68F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9250B38-5792-4534-BC15-E345E95E68F1}.Release|Any CPU.Build.0 = Release|Any CPU
{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2}.Release|Any CPU.Build.0 = Release|Any CPU
{821340BF-7830-4F73-A1C9-E38065152482}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{821340BF-7830-4F73-A1C9-E38065152482}.Debug|Any CPU.Build.0 = Debug|Any CPU
{821340BF-7830-4F73-A1C9-E38065152482}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -257,7 +251,6 @@ Global
{B42F1632-D935-4CCA-9CB8-2F346B21C8E3} = {B86A4082-6F42-4BFC-A1F2-83CC422AD493}
{9C4D7E41-7CED-4463-AD08-1A95BBF2D7DD} = {8B4CE682-9C0B-4849-813B-E43BE098FCE2}
{D9250B38-5792-4534-BC15-E345E95E68F1} = {402D8CB5-C3C3-47E0-BEB3-63D0975E5109}
{7CF527D8-E1A3-4A4F-8DB8-0607706DC3C2} = {8B4CE682-9C0B-4849-813B-E43BE098FCE2}
{821340BF-7830-4F73-A1C9-E38065152482} = {402D8CB5-C3C3-47E0-BEB3-63D0975E5109}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
11 changes: 10 additions & 1 deletion src/Management/src/EndpointBase/ActuatorMediaTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using Steeltoe.Management.Endpoint.ThreadDump;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -13,6 +14,8 @@ public static class ActuatorMediaTypes

public static readonly string V2_JSON = "application/vnd.spring-boot.actuator.v2+json";

public static readonly string V3_JSON = "application/vnd.spring-boot.actuator.v3+json";

public static readonly string APP_JSON = "application/json";

public static readonly string ANY = "*/*";
Expand All @@ -32,7 +35,13 @@ public static string GetContentHeaders(List<string> acceptHeaders, MediaTypeVers

private static string GetContentTypeHeaderForVersion(MediaTypeVersion version = MediaTypeVersion.V2)
{
return version == MediaTypeVersion.V2 ? V2_JSON : V1_JSON;
return version switch
{
MediaTypeVersion.V1 => V1_JSON,
MediaTypeVersion.V2 => V2_JSON,
MediaTypeVersion.V3 => V3_JSON,
_ => V2_JSON
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected HealthCheckResult AddChecksSetStatus(HealthCheckResult result, Concurr
result.Status = healthCheck.Value.Status;
}

result.Details.Add(healthCheck.Key, healthCheck.Value.Details);
result.Details.Add(healthCheck.Key, healthCheck.Value);
}

return result;
Expand Down
10 changes: 9 additions & 1 deletion src/Management/src/EndpointBase/Health/HealthConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using Steeltoe.Common.HealthChecks;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -33,7 +34,14 @@ public override void Write(Utf8JsonWriter writer, HealthEndpointResponse value,
foreach (var detail in health.Details)
{
writer.WritePropertyName(detail.Key);
JsonSerializer.Serialize(writer, detail.Value, options);
if (detail.Value is HealthCheckResult detailValue)
{
JsonSerializer.Serialize(writer, detailValue.Details, options);
}
else
{
JsonSerializer.Serialize(writer, detail.Value, options);
}
}

writer.WriteEndObject();
Expand Down
46 changes: 46 additions & 0 deletions src/Management/src/EndpointBase/Health/HealthConverterV3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Steeltoe.Management.Endpoint.Health
{
public class HealthConverterV3 : JsonConverter<HealthEndpointResponse>
{
public override HealthEndpointResponse Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, HealthEndpointResponse value, JsonSerializerOptions options)
{
writer.WriteStartObject();
if (value is { } health)
{
writer.WriteString("status", health.Status.ToString());
if (!string.IsNullOrEmpty(health.Description))
{
writer.WriteString("description", health.Description);
}

if (health.Details != null && health.Details.Count > 0)
{
writer.WritePropertyName("components");
writer.WriteStartObject();
foreach (var detail in health.Details)
{
writer.WritePropertyName(detail.Key);
JsonSerializer.Serialize(writer, detail.Value, options);
}

writer.WriteEndObject();
}
}

writer.WriteEndObject();
}
}
}
3 changes: 2 additions & 1 deletion src/Management/src/EndpointBase/MediaTypeVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Steeltoe.Management.Endpoint
public enum MediaTypeVersion
{
V1,
V2
V2,
V3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Steeltoe.Management.Endpoint.Middleware
{
Expand Down Expand Up @@ -74,7 +75,13 @@ internal JsonSerializerOptions GetSerializerOptions(JsonSerializerOptions serial
{
serializerOptions ??= new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
serializerOptions.IgnoreNullValues = true;
if (serializerOptions.Converters?.Any(c => c is HealthConverter) != true)

if (serializerOptions.Converters?.Any(c => c is JsonStringEnumConverter) != true)
{
serializerOptions.Converters.Add(new JsonStringEnumConverter());
}

if (serializerOptions.Converters?.Any(c => c is HealthConverter or HealthConverterV3) != true)
{
serializerOptions.Converters.Add(new HealthConverter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(EFCoreTestVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EFCoreTestVersion)" />
<PackageReference Include="NSubstitute" Version="4.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ public async Task HealthActuator_ReturnsDetails()
Assert.Contains("diskSpace", health["details"].ToString());
}

[Fact]
public async Task HealthActuatorV3_ReturnsDetails()
{
var settings = new Dictionary<string, string>(appSettings) { { "management:endpoints:customjsonconverters:0", typeof(HealthConverterV3).FullName } };
var builder = new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureAppConfiguration((context, config) => config.AddInMemoryCollection(settings));

using var server = new TestServer(builder);
var client = server.CreateClient();
var result = await client.GetAsync("http://localhost/cloudfoundryapplication/health");
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
var json = await result.Content.ReadAsStringAsync();
Assert.NotNull(json);

// {"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":1003588939776,"free":597722619904,"threshold":10485760,"status":"UP"}},"readiness":{"status":"UNKNOWN","description":"Failed to get current availability state","details":{}}}}
var health = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
Assert.NotNull(health);
Assert.True(health.ContainsKey("status"), "Health should contain key: status");
Assert.False(health.ContainsKey("details"), "Health should not contain key: details");
Assert.True(health.ContainsKey("components"), "Health should contain key: components");
var componentString = health["components"].ToString() ?? string.Empty;
Assert.Contains("diskSpace", componentString);
Assert.Contains("details", componentString);
}

[Fact]
public async Task HealthActuator_ReturnsMicrosoftHealthDetails()
{
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions src/Management/test/OpenTelemetry.Test/xunit.runner.json

This file was deleted.

7 changes: 0 additions & 7 deletions src/Steeltoe.All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Extensions.Configu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Extensions.Configuration.SpringBootCore.Test", "Configuration\test\SpringBootCore.Test\Steeltoe.Extensions.Configuration.SpringBootCore.Test.csproj", "{5D86C95F-A0C3-417E-A9E3-4426927E2897}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.OpenTelemetry.Test", "Management\test\OpenTelemetry.Test\Steeltoe.OpenTelemetry.Test.csproj", "{9F40820C-82C9-4AC2-999F-87D3F48E6636}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_build", "_build", "{656A7881-FE4F-4EE0-81C8-DA1DE6CDF29E}"
ProjectSection(SolutionItems) = preProject
..\azure-pipelines.yml = ..\azure-pipelines.yml
Expand Down Expand Up @@ -909,10 +907,6 @@ Global
{5D86C95F-A0C3-417E-A9E3-4426927E2897}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D86C95F-A0C3-417E-A9E3-4426927E2897}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D86C95F-A0C3-417E-A9E3-4426927E2897}.Release|Any CPU.Build.0 = Release|Any CPU
{9F40820C-82C9-4AC2-999F-87D3F48E6636}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F40820C-82C9-4AC2-999F-87D3F48E6636}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F40820C-82C9-4AC2-999F-87D3F48E6636}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F40820C-82C9-4AC2-999F-87D3F48E6636}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1055,7 +1049,6 @@ Global
{15BDC5D8-7594-4506-B81A-A60D5DF1947C} = {283EAD31-6CC1-4306-A0E8-AB60F6CB3FD8}
{B748B8B7-D6B8-40AC-AD19-73B6213CA363} = {4AB95F47-0C93-4C88-B87F-231262CD0E89}
{5D86C95F-A0C3-417E-A9E3-4426927E2897} = {4AB95F47-0C93-4C88-B87F-231262CD0E89}
{9F40820C-82C9-4AC2-999F-87D3F48E6636} = {8BD7D5E5-D887-4BD7-9F42-725A8714F7BC}
{FC49CC5C-EFB6-4B08-AE63-05364EB27021} = {656A7881-FE4F-4EE0-81C8-DA1DE6CDF29E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down

0 comments on commit bc267da

Please sign in to comment.