-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added health endpoint and application insights configuration (#60)
* Added health endpoint and application insights configuration * Added health check test --------- Co-authored-by: acn-dgopa <acn-dgopa@dev-acn-tje-14>
- Loading branch information
Showing
7 changed files
with
303 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Altinn.Auth.AuditLog.Core.Models | ||
{ | ||
/// <summary> | ||
/// General configuration settings | ||
/// </summary> | ||
public class KeyVaultSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the secret uri | ||
/// </summary> | ||
public string SecretUri { get; set; } | ||
Check warning on line 17 in src/Altinn.Auth.AuditLog.Core/Models/KeyVaultSettings.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
22 changes: 22 additions & 0 deletions
22
src/Altinn.Auth.AuditLog/Configuration/CustomTelemetryInitializer.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,22 @@ | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
|
||
namespace Altinn.Auth.AuditLog.Configuration | ||
{ | ||
/// <summary> | ||
/// Set up custom telemetry for Application Insights | ||
/// </summary> | ||
public class CustomTelemetryInitializer : ITelemetryInitializer | ||
{ | ||
/// <summary> | ||
/// Custom TelemetryInitializer that sets some specific values for the component | ||
/// </summary> | ||
public void Initialize(ITelemetry telemetry) | ||
{ | ||
if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName)) | ||
{ | ||
telemetry.Context.Cloud.RoleName = "auth-auditlog"; | ||
} | ||
} | ||
} | ||
} |
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,23 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Altinn.Auth.AuditLog.Health | ||
{ | ||
/// <summary> | ||
/// Health check service configured in startup. See https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks | ||
/// Listen to | ||
/// </summary> | ||
public class HealthCheck : IHealthCheck | ||
{ | ||
/// <summary> | ||
/// Verifies the health status | ||
/// </summary> | ||
/// <param name="context">The healtcheck context</param> | ||
/// <param name="cancellationToken">The cancellationtoken</param> | ||
/// <returns>The health check result</returns> | ||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
return Task.FromResult( | ||
HealthCheckResult.Healthy("A healthy result.")); | ||
} | ||
} | ||
} |
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,47 @@ | ||
using Microsoft.ApplicationInsights.Channel; | ||
using Microsoft.ApplicationInsights.DataContracts; | ||
using Microsoft.ApplicationInsights.Extensibility; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Altinn.Auth.AuditLog.Health | ||
{ | ||
/// <summary> | ||
/// Filter to exclude health check request from Application Insights | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class HealthTelemetryFilter : ITelemetryProcessor | ||
{ | ||
private ITelemetryProcessor Next { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HealthTelemetryFilter"/> class. | ||
/// </summary> | ||
public HealthTelemetryFilter(ITelemetryProcessor next) | ||
{ | ||
Next = next; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Process(ITelemetry item) | ||
{ | ||
if (ExcludeItemTelemetry(item)) | ||
{ | ||
return; | ||
} | ||
|
||
Next.Process(item); | ||
} | ||
|
||
private bool ExcludeItemTelemetry(ITelemetry item) | ||
{ | ||
RequestTelemetry request = item as RequestTelemetry; | ||
|
||
if (request != null && request.Url.ToString().EndsWith("/health/")) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/test/Altinn.Auth.AuditLog.Tests/Health/HealthCheckTests.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,57 @@ | ||
using Altinn.Auth.AuditLog.Health; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Altinn.Auth.AuditLog.Tests.Health | ||
{ | ||
/// <summary> | ||
/// Health check | ||
/// </summary> | ||
public class HealthCheckTests : IClassFixture<CustomWebApplicationFactory<HealthCheck>> | ||
{ | ||
private readonly CustomWebApplicationFactory<HealthCheck> _factory; | ||
|
||
/// <summary> | ||
/// Default constructor | ||
/// </summary> | ||
/// <param name="fixture">The web application fixture</param> | ||
public HealthCheckTests(CustomWebApplicationFactory<HealthCheck> fixture) | ||
{ | ||
_factory = fixture; | ||
} | ||
|
||
/// <summary> | ||
/// Verify that component responds on health check | ||
/// </summary> | ||
/// <returns></returns> | ||
[Fact] | ||
public async Task VerifyHealthCheck_OK() | ||
{ | ||
HttpClient client = GetTestClient(); | ||
|
||
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "/health"); | ||
|
||
HttpResponseMessage response = await client.SendAsync(httpRequestMessage); | ||
string content = await response.Content.ReadAsStringAsync(); | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
} | ||
|
||
private HttpClient GetTestClient() | ||
{ | ||
HttpClient client = _factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.ConfigureTestServices(services => | ||
{ | ||
}); | ||
}).CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); | ||
|
||
return client; | ||
} | ||
} | ||
} |