Skip to content

Commit dee7f36

Browse files
add AlwaysHealthy health check when provider is disabled
1 parent 3d3c4d7 commit dee7f36

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using System.Threading.Tasks;
6+
using System.Threading;
7+
8+
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
9+
{
10+
internal class AlwaysHealthyHealthCheck : IHealthCheck
11+
{
12+
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
13+
{
14+
return Task.FromResult(HealthCheckResult.Healthy());
15+
}
16+
}
17+
}

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationExtensions.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Extensions.DependencyInjection.Extensions;
88
using System;
99
using System.Collections.Generic;
10-
using System.Security;
1110

1211
namespace Microsoft.Extensions.Configuration
1312
{
@@ -16,19 +15,7 @@ namespace Microsoft.Extensions.Configuration
1615
/// </summary>
1716
public static class AzureAppConfigurationExtensions
1817
{
19-
private const string DisableProviderEnvironmentVariable = "AZURE_APP_CONFIGURATION_PROVIDER_DISABLED";
20-
private static readonly bool _isProviderDisabled = IsProviderDisabled();
21-
22-
private static bool IsProviderDisabled()
23-
{
24-
try
25-
{
26-
return bool.TryParse(Environment.GetEnvironmentVariable(DisableProviderEnvironmentVariable), out bool disabled) ? disabled : false;
27-
}
28-
catch (SecurityException) { }
29-
30-
return false;
31-
}
18+
private static readonly bool _isProviderDisabled = Utils.IsProviderDisabled();
3219

3320
/// <summary>
3421
/// Adds key-value data from an Azure App Configuration store to a configuration builder using its connection string.

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationHealthChecksBuilderExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Microsoft.Extensions.DependencyInjection
1414
/// </summary>
1515
public static class AzureAppConfigurationHealthChecksBuilderExtensions
1616
{
17+
private static readonly bool _isProviderDisabled = Utils.IsProviderDisabled();
18+
1719
/// <summary>
1820
/// Add a health check for Azure App Configuration to given <paramref name="builder"/>.
1921
/// </summary>
@@ -32,6 +34,17 @@ public static IHealthChecksBuilder AddAzureAppConfiguration(
3234
IEnumerable<string> tags = default,
3335
TimeSpan? timeout = default)
3436
{
37+
if (_isProviderDisabled)
38+
{
39+
// If the provider is disabled, register a health check that always reports healthy.
40+
return builder.Add(new HealthCheckRegistration(
41+
name ?? HealthCheckConstants.HealthCheckRegistrationName,
42+
sp => new AlwaysHealthyHealthCheck(),
43+
failureStatus,
44+
tags,
45+
timeout));
46+
}
47+
3548
return builder.Add(new HealthCheckRegistration(
3649
name ?? HealthCheckConstants.HealthCheckRegistrationName,
3750
sp => new AzureAppConfigurationHealthCheck(

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/EnvironmentVariables.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ internal static class EnvironmentVariables
2121
/// The value of this variable is a boolean string, e.g. "true" or "false".
2222
/// </summary>
2323
public const string DisableRequestTracing = "AZURE_APP_CONFIGURATION_TRACING_DISABLED";
24+
25+
/// <summary>
26+
/// Environment variable to disable Azure App Configuration provider.
27+
/// The value of this variable is a boolean string, e.g. "true" or "false".
28+
/// </summary>
29+
public const string DisableAppConfigurationProvider = "AZURE_APP_CONFIGURATION_PROVIDER_DISABLED";
2430
}
2531
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
//
4+
5+
using System;
6+
using System.Security;
7+
8+
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
9+
{
10+
internal static class Utils
11+
{
12+
public static bool IsProviderDisabled()
13+
{
14+
try
15+
{
16+
return bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariables.DisableAppConfigurationProvider), out bool disabled) ? disabled : false;
17+
}
18+
catch (SecurityException) { }
19+
20+
return false;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)