Skip to content

If needed... Extensions for Health checks #28

@majco333

Description

@majco333

Add extensions for health checks:

  • proxy support
  • json output support
  • add tests

Notes:
Report and writer
Tests

public static class UrisHealthCheckBuilderExtensions
{
///


/// Add a health check for single uri.
///

/// The .
/// The uri to check.
/// The health check name. Optional. If null the type name 'uri-group' will be used for the name.
///
/// The that should be reported when the health check fails. If null then
/// the default status of will be reported.
///
/// A list of tags that can be used to filter sets of health checks.
/// The proxy for http request. Optional
/// The .
public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Uri uri, string name, Uri proxy,
HealthStatus? failureStatus = null, IEnumerable tags = null)
{
if (proxy == null)
{
builder.Services.AddHttpClient("HealthCheck");
}
else
{
builder.Services
.AddHttpClient("HealthCheck")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
Proxy = new WebProxy(proxy)
});
}

        var options = new UriHealthCheckOptions();
        options.AddUri(uri);

        var registrationName = name;
        return builder.Add(new HealthCheckRegistration(
            registrationName,
            sp => CreateHealthCheck(sp, registrationName, options),
            failureStatus,
            tags));
    }

    private static UriHealthCheck CreateHealthCheck(IServiceProvider sp, string name, UriHealthCheckOptions options)
    {
        var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
        return new UriHealthCheck(options, () => httpClientFactory.CreateClient("HealthCheck"));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions