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

[Nats] Add overload for NatsOptions with IServiceProvider #2024

Merged
merged 2 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -48,4 +48,44 @@ public static IHealthChecksBuilder AddNats(
tags,
timeout));
}

/// <summary>
/// Add a health check for Nats.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="setup">The factory to configure the Nats setup.</param>
/// <param name="name">
/// The health check name.
/// Optional.
/// If <see langword="null"/> the type name 'nats' 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 System.TimeSpan representing the timeout of the check.</param>
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddNats(
this IHealthChecksBuilder builder,
Action<IServiceProvider, NatsOptions>? setup,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp =>
{
var options = new NatsOptions();
setup?.Invoke(sp, options);

return new NatsHealthCheck(options);
},
failureStatus,
tags,
timeout));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ public void add_health_check_when_demo_instance_properly_configured() =>
setup => setup.Url = DemoConnectionString,
check => check.ShouldBeOfType<NatsHealthCheck>());

[Fact]
public void add_health_check_with_service_provider()
{
var services = new ServiceCollection();

services
.AddSingleton<IDependency, Dependency>()
.AddHealthChecks()
.AddNats((sp, setup) => setup.Url = sp.GetRequiredService<IDependency>().ConnectionString);

var serviceProvider = services.BuildServiceProvider();
sungam3r marked this conversation as resolved.
Show resolved Hide resolved
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
registration.Name.ShouldBe(NatsName);

var check = registration.Factory(serviceProvider);

check.ShouldBeOfType<NatsHealthCheck>();
}

private void RegistrationFact(Action<NatsOptions> setup, Action<IHealthCheck> assert, string? name = null)
{
var services = new ServiceCollection();
Expand All @@ -37,4 +58,14 @@ private void RegistrationFact(Action<NatsOptions> setup, Action<IHealthCheck> as
var check = registration.Factory(serviceProvider);
assert(check);
}

private interface IDependency
{
string ConnectionString { get; }
}

private class Dependency : IDependency
{
public string ConnectionString => DemoConnectionString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ namespace Microsoft.Extensions.DependencyInjection
public static class NatsHealthCheckBuilderExtensions
{
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNats(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Action<HealthChecks.Nats.NatsOptions>? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddNats(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Action<System.IServiceProvider, HealthChecks.Nats.NatsOptions>? setup, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { }
}
}