Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload to AddAzureBlobStorage to supply your own BlobServiceClient from delegate #1845

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,48 @@ public static IHealthChecksBuilder AddAzureBlobStorage(
timeout));
}

/// <summary>
/// Add a health check for Azure Blob Storage.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="clientFactory">Delegate for creating a <see cref="BlobServiceClient"/>.</param>
/// <param name="configureOptions">Delegate for configuring the health check. Optional.</param>
/// <param name="name">The health check name. Optional. If <see langword="null"/> the type name 'azureblob' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <see langword="null"/> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddAzureBlobStorage(
this IHealthChecksBuilder builder,
Func<IServiceProvider, BlobServiceClient> clientFactory,
Action<AzureBlobStorageHealthCheckOptions>? configureOptions = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
name ?? AZUREBLOB_NAME,
sp =>
{
var options = new AzureBlobStorageHealthCheckOptions();
configureOptions?.Invoke(options);
return new AzureBlobStorageHealthCheck(clientFactory(sp), options);
},
failureStatus,
tags,
timeout));
}

/// <summary>
/// Add a health check for Azure Blob Storage.
/// </summary>
/// <remarks>
/// A <see cref="BlobServiceClient"/> service must be registered in the service container.
/// A <see cref="BlobServiceClient"/> service must be registered in the service container. For named instances
/// you may use other overload with <see cref="Func<IServiceProvider, BlobServiceClient>"/> argument.
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="configureOptions">Delegate for configuring the health check. Optional.</param>
Expand Down Expand Up @@ -159,6 +196,42 @@ public static IHealthChecksBuilder AddAzureBlobStorage(
timeout));
}

/// <summary>
/// Add a health check for Azure Blob Storage.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="clientFactory">Delegate for creating a <see cref="BlobServiceClient"/>. Optional.</param>
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="configureOptions">Delegate for configuring the health check. Optional.</param>
/// <param name="name">The health check name. Optional. If <see langword="null"/> the type name 'azureblob' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <see langword="null"/> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddAzureBlobStorage(
this IHealthChecksBuilder builder,
Func<IServiceProvider, BlobServiceClient> clientFactory,
Action<IServiceProvider, AzureBlobStorageHealthCheckOptions>? configureOptions = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? AZUREBLOB_NAME,
sp =>
{
var options = new AzureBlobStorageHealthCheckOptions();
configureOptions?.Invoke(sp, options);
return new AzureBlobStorageHealthCheck(clientFactory(sp), options);
},
failureStatus,
tags,
timeout));
}

/// <summary>
/// Add a health check for an Azure file share.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ public void add_health_check_with_client_from_service_provider(string? container
registration.FailureStatus.ShouldBe(failureStatus ?? HealthStatus.Unhealthy);
check.ShouldBeOfType<AzureBlobStorageHealthCheck>();
}

sungam3r marked this conversation as resolved.
Show resolved Hide resolved
[Theory]
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
[InlineData(null, null, null)]
[InlineData("container", null, null)]
[InlineData(null, "my-azureblob-group", null)]
[InlineData(null, null, HealthStatus.Degraded)]
[InlineData("container", "my-azureblob-group", HealthStatus.Degraded)]
public void add_health_check_with_client_from_delegate(string? containerName, string? registrationName, HealthStatus? failureStatus)
{
using var serviceProvider = new ServiceCollection()
.AddHealthChecks()
.AddAzureBlobStorage(
clientFactory: sp => Substitute.For<BlobServiceClient>(),
o => o.ContainerName = containerName,
name: registrationName,
failureStatus: failureStatus)
.Services
.BuildServiceProvider();

var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.ShouldBe(registrationName ?? "azureblob");
registration.FailureStatus.ShouldBe(failureStatus ?? HealthStatus.Unhealthy);
check.ShouldBeOfType<AzureBlobStorageHealthCheck>();
}

[Theory]
[InlineData(null, null, null)]
Expand Down Expand Up @@ -118,4 +146,34 @@ public void add_health_check_with_client_from_service_provider_and_advanced_dele
registration.FailureStatus.ShouldBe(failureStatus ?? HealthStatus.Unhealthy);
check.ShouldBeOfType<AzureBlobStorageHealthCheck>();
}

sungam3r marked this conversation as resolved.
Show resolved Hide resolved
[Theory]
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
[InlineData(null, null, null)]
[InlineData("container", null, null)]
[InlineData(null, "my-azureblob-group", null)]
[InlineData(null, null, HealthStatus.Degraded)]
[InlineData("container", "my-azureblob-group", HealthStatus.Degraded)]
public void add_health_check_with_client_from_delegate_and_advanced_delegate(string? containerName, string? registrationName, HealthStatus? failureStatus)
{
using var serviceProvider = new ServiceCollection()
.AddHealthChecks()
.AddAzureBlobStorage(
clientFactory: sp => Substitute.For<BlobServiceClient>(),
(sp, o) => o.ContainerName = containerName,
name: registrationName,
failureStatus: failureStatus)
.Services
.BuildServiceProvider();

var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.ShouldBe(registrationName ?? "azureblob");
registration.FailureStatus.ShouldBe(failureStatus ?? HealthStatus.Unhealthy);
check.ShouldBeOfType<AzureBlobStorageHealthCheck>();
}


sungam3r marked this conversation as resolved.
Show resolved Hide resolved
}