Skip to content

Commit

Permalink
Add configuration to filter health checks (#3357)
Browse files Browse the repository at this point in the history
* add configuration to filter health checks; fix local testing for external store health check

* revert appsettings change

* update to use comma seperated string so the value is parsable in k8s config

* update to read only list
  • Loading branch information
jnlycklama committed Feb 12, 2024
1 parent 90ea691 commit c72aa5c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using EnsureThat;
using Microsoft.ApplicationInsights.Extensibility;
Expand Down Expand Up @@ -68,13 +70,22 @@ public static IDicomServerBuilder AddBackgroundWorkers(this IDicomServerBuilder
serverBuilder.Services.AddHostedService<StartContentLengthBackFillBackgroundService>();
}

HealthCheckPublisherConfiguration healthCheckPublisherConfiguration = new HealthCheckPublisherConfiguration();
configuration.GetSection(HealthCheckPublisherConfiguration.SectionName).Bind(healthCheckPublisherConfiguration);
IReadOnlyList<string> excludedHealthCheckNames = healthCheckPublisherConfiguration.GetListOfExcludedHealthCheckNames();

serverBuilder.Services
.AddCustomerKeyValidationBackgroundService(options => configuration
.GetSection(CustomerManagedKeyOptions.CustomerManagedKey)
.Bind(options))
.AddHealthCheckCachePublisher(options => configuration
.GetSection("HealthCheckPublisher")
.Bind(options));
.AddHealthCheckCachePublisher(options =>
{
configuration
.GetSection(HealthCheckPublisherConfiguration.SectionName)
.Bind(options);
options.Predicate = (check) => !excludedHealthCheckNames.Contains(check.Name);
});

return serverBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Storage.Blobs;
using EnsureThat;
using Microsoft.Health.Dicom.Blob.Utilities;

Expand All @@ -24,11 +25,23 @@ internal class ExternalStoreHealthExpiryHttpPipelinePolicy : HttpPipelinePolicy
public ExternalStoreHealthExpiryHttpPipelinePolicy(ExternalBlobDataStoreConfiguration externalStoreOptions)
{
_externalStoreOptions = EnsureArg.IsNotNull(externalStoreOptions, nameof(externalStoreOptions));
EnsureArg.IsNotNull(externalStoreOptions.BlobContainerUri, nameof(externalStoreOptions.BlobContainerUri));
EnsureArg.IsNotNull(externalStoreOptions.StorageDirectory, nameof(externalStoreOptions.StorageDirectory));
EnsureArg.IsNotNull(externalStoreOptions.HealthCheckFilePath, nameof(externalStoreOptions.HealthCheckFilePath));

UriBuilder uriBuilder = new UriBuilder(_externalStoreOptions.BlobContainerUri);
Uri blobUri;

if (_externalStoreOptions.BlobContainerUri != null)
{
blobUri = _externalStoreOptions.BlobContainerUri;
}
else
{
// For local testing with Azurite
BlobContainerClient blobContainerClient = new BlobContainerClient(_externalStoreOptions.ConnectionString, _externalStoreOptions.ContainerName);
blobUri = blobContainerClient.Uri;
}

UriBuilder uriBuilder = new UriBuilder(blobUri);
uriBuilder.Path = Path.Combine(uriBuilder.Path, _externalStoreOptions.StorageDirectory, _externalStoreOptions.HealthCheckFilePath);

string healthCheckPathRegex = Regex.Escape(uriBuilder.Uri.AbsoluteUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ public static IDicomServerBuilder AddBlobDataStores(this IDicomServerBuilder ser
serverBuilder.Services
.AddPersistence<IFileStore, BlobFileStore>();

if (featureConfiguration.EnableExternalStoreHealthCheck)
{
serverBuilder.Services
.AddHealthChecks()
.AddCheck<DicomConnectedStoreHealthCheck>("DcmHealthCheck");
}
serverBuilder.Services
.AddHealthChecks()
.AddCheck<DicomConnectedStoreHealthCheck>("DcmHealthCheck");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ public class FeatureConfiguration
/// Disables all async operations
/// </summary>
public bool DisableOperations { get; set; }

/// <summary>
/// Enabled the health check for external store
/// </summary>
public bool EnableExternalStoreHealthCheck { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace Microsoft.Health.Dicom.Core.Configs;

public class HealthCheckPublisherConfiguration
{
public const string SectionName = "HealthCheckPublisher";

/// <summary>
/// A comma separated list of health check names to exclude.
/// Example: "DcmHealthCheck,MetadataHealthCheck"
/// </summary>
public string ExcludedHealthCheckNames { get; set; }

public IReadOnlyList<string> GetListOfExcludedHealthCheckNames()
{
return ExcludedHealthCheckNames?.Split(',') ?? Array.Empty<string>();
}
}
6 changes: 4 additions & 2 deletions src/Microsoft.Health.Dicom.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
"EnableFullDicomItemValidation": false,
"EnableOhifViewer": true,
"EnableExternalStore": false,
"DisableOperations": false,
"EnableExternalStoreHealthCheck": false
"DisableOperations": false
},
"Services": {
"DeletedInstanceCleanup": {
Expand Down Expand Up @@ -172,5 +171,8 @@
"SchemaOptions": {
"AutomaticUpdatesEnabled": true
}
},
"HealthCheckPublisher": {
"ExcludedHealthCheckNames": ""
}
}

0 comments on commit c72aa5c

Please sign in to comment.