/// <summary>
        /// Add a health check for single uri.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uriProvider">Factory for providing the uri to check.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' will be used for the name.</param>
        /// <param name="failureStatus"></param>
        /// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
        /// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
        /// <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>
        /// <returns></returns>
        public static IHealthChecksBuilder AddUrlGroup(
            this IHealthChecksBuilder builder,
            Func <IServiceProvider, Uri> uriProvider,
            string name = null,
            HealthStatus?failureStatus = null,
            IEnumerable <string> tags  = null,
            TimeSpan?timeout           = null)
        {
            builder.Services.AddHttpClient();
            var registrationName = name ?? NAME;

            return(builder.Add(
                       new HealthCheckRegistration(
                           registrationName,
                           sp =>
            {
                var uri = uriProvider(sp);
                var uriHealthCheckOptions = new UriHealthCheckOptions().AddUri(uri, null);

                var httpClientFactory = sp.GetRequiredService <IHttpClientFactory>();

                return new UriHealthCheck(
                    uriHealthCheckOptions,
                    () => httpClientFactory.CreateClient(registrationName));
            },
                           failureStatus,
                           tags,
                           timeout)));
        }
        /// <summary>
        /// Add a health check for single uri.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uri">The uri to check.</param>
        /// <param name="httpMethod">The http method to use on check.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <param name="configureClient">An optional setup action to configure the Uris HealthCheck http client</param>
        /// <param name="configureHttpMessageHandler">An optional setup action to configure the Uris HealthCheck http client message handler</param>
        /// <returns>The specified <paramref name="builder"/>.</returns>
        public static IHealthChecksBuilder AddUrlGroup(
            this IHealthChecksBuilder builder,
            Uri uri,
            HttpMethod httpMethod,
            string?name = default,
            HealthStatus?failureStatus = default,
            IEnumerable <string>?tags  = default,
            TimeSpan?timeout           = default,
            Action <IServiceProvider, HttpClient>?configureClient = null,
            Func <IServiceProvider, HttpMessageHandler>?configureHttpMessageHandler = null)
        {
            var registrationName = name ?? NAME;

            ConfigureUrisClient(builder, configureClient, configureHttpMessageHandler, registrationName);

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp =>
            {
                var options = new UriHealthCheckOptions()
                              .AddUri(uri)
                              .UseHttpMethod(httpMethod);

                return CreateHealthCheck(sp, registrationName, options);
            },
                                   failureStatus,
                                   tags,
                                   timeout)));
        }
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uris">The collection of uri's to be checked.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, IEnumerable <Uri> uris, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            var options = UriHealthCheckOptions.CreateFromUris(uris);

            return(builder.Add(new HealthCheckRegistration(
                                   name ?? NAME,
                                   sp => new UriHealthCheck(options),
                                   failureStatus,
                                   tags)));
        }
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uris">The collection of uri's to be checked.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, IEnumerable <Uri> uris, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            builder.Services.AddHttpClient();

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp => CreateHealthCheck(sp, registrationName, UriHealthCheckOptions.CreateFromUris(uris)),
                                   failureStatus,
                                   tags)));
        }
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uriOptions">The action used to configured uri values and specified http methods to be checked.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Action <UriHealthCheckOptions> uriOptions, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            var options = new UriHealthCheckOptions();

            uriOptions?.Invoke(options);

            return(builder.Add(new HealthCheckRegistration(
                                   name ?? NAME,
                                   sp => new UriHealthCheck(options),
                                   failureStatus,
                                   tags)));
        }
        /// <summary>
        /// Add a health check for single uri.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uri">The uri to check.</param>
        /// <param name="httpMethod">The http method to use on check.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Uri uri, HttpMethod httpMethod, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            var options = new UriHealthCheckOptions();

            options.AddUri(uri);
            options.UseHttpMethod(httpMethod);

            return(builder.Add(new HealthCheckRegistration(
                                   name ?? NAME,
                                   sp => new UriHealthCheck(options),
                                   failureStatus,
                                   tags)));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uris">The collection of uri's to be checked.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <param name="configureClient">An optional setup action to configure the Uris HealthCheck http client</param>
        /// <param name="configureHttpMessageHandler">An optional setup action to configure the Uris HealthCheck http client message handler</param>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, IEnumerable <Uri> uris, string name = default,
                                                       HealthStatus?failureStatus = default, IEnumerable <string> tags = default, TimeSpan?timeout = default,
                                                       Action <IServiceProvider, HttpClient> configureClient = null, Func <IServiceProvider, HttpMessageHandler> configureHttpMessageHandler = null)
        {
            var registrationName = name ?? NAME;

            ConfigureUrisClient(builder, configureClient, configureHttpMessageHandler, registrationName);

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp => CreateHealthCheck(sp, registrationName, UriHealthCheckOptions.CreateFromUris(uris)),
                                   failureStatus,
                                   tags,
                                   timeout)));
        }
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uriOptions">The action used to configured uri values and specified http methods to be checked.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Action <UriHealthCheckOptions> uriOptions, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            builder.Services.AddHttpClient();

            var options = new UriHealthCheckOptions();

            uriOptions?.Invoke(options);

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp => CreateHealthCheck(sp, registrationName, options),
                                   failureStatus,
                                   tags)));
        }
        /// <summary>
        /// Add a health check for single uri.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uri">The uri to check.</param>
        /// <param name="httpMethod">The http method to use on check.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Uri uri, HttpMethod httpMethod, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default)
        {
            builder.Services.AddHttpClient();

            var options = new UriHealthCheckOptions();

            options.AddUri(uri);
            options.UseHttpMethod(httpMethod);

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp => CreateHealthCheck(sp, registrationName, options),
                                   failureStatus,
                                   tags)));
        }
        /// <summary>
        /// Add a health check for single uri.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uri">The uri to check.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Uri uri, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default, TimeSpan?timeout = default)
        {
            builder.Services.AddHttpClient();

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp =>
            {
                var options = new UriHealthCheckOptions()
                              .AddUri(uri);

                return CreateHealthCheck(sp, registrationName, options);
            },
                                   failureStatus,
                                   tags,
                                   timeout)));
        }
        /// <summary>
        /// Add a health check for multiple uri's.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uris">The collection of uri's to be checked.</param>
        /// <param name="httpMethod">The http method to be used.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
        public static IHealthChecksBuilder AddUrlGroupWithVersioning(this IHealthChecksBuilder builder, IEnumerable <Uri> uris, HttpMethod httpMethod, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default, TimeSpan?timeout = default)
        {
            builder.Services.AddHttpClient();

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp =>
            {
                var options = UriHealthCheckOptions
                              .CreateFromUris(uris)
                              .UseHttpMethod(httpMethod);

                return CreateHealthCheck(sp, registrationName, options);
            },
                                   failureStatus,
                                   tags,
                                   timeout)));
        }
        private static UriHealthCheck CreateHealthCheck <T>(IServiceProvider sp, string route)
            where T : ServiceDiscoveryClient
        {
            var serviceClient = sp.GetRequiredService <T>();

            if (serviceClient == null)
            {
                throw new NaosException($"Health: ServiceDiscovery client '{typeof(T)}' not found, please add with service.AddHttpClient<ServiceDiscoveryProxy>()");
            }

            var options = new UriHealthCheckOptions();
            var address = serviceClient.HttpClient?.BaseAddress?.ToString(); //.SubstringTill("servicediscovery");

            if (address.IsNullOrEmpty())
            {
                throw new NaosException($"Health: ServiceDiscovery client '{typeof(T)}' address not found, registration inactive (due to health) or missing from registry?");
            }

            options.AddUri(new Uri(new Uri(address), route));

            return(new UriHealthCheck(options, () => serviceClient.HttpClient));
        }
Exemplo n.º 13
0
        /// <summary>
        /// Add a health check for single uri, validate the response content is expected using <paramref name="contentCheckFunc"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
        /// <param name="uri">The uri to check.</param>
        /// <param name="httpMethod">The http method to use on check.</param>
        /// <param name="contentCheckFunc">
        /// A function that checks the HttpContent of the response.
        /// Should return a <see cref="ExpectedContentResult" /> if valid,
        /// or a <see cref="UnexpectedContentResult" /> if invalid.
        /// </param>
        /// <param name="uriOptionsSetup">A way to configure the options that will be used for the Uri. Optional.</param>
        /// <param name="name">The health check name. Optional. If <c>null</c> the type name 'uri-group' 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 <c>null</c> 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>
        /// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
        public static IHealthChecksBuilder AddUrlGroup(this IHealthChecksBuilder builder, Uri uri, HttpMethod httpMethod,
                                                       Func <HttpContent, Task <ContentCheckResult> > contentCheckFunc,
                                                       Action <IUriOptions> uriOptionsSetup = null,
                                                       string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default, TimeSpan?timeout = default)
        {
            builder.Services.AddHttpClient();

            var registrationName = name ?? NAME;

            return(builder.Add(new HealthCheckRegistration(
                                   registrationName,
                                   sp =>
            {
                var options = new UriHealthCheckOptions()
                              .AddUri(uri, uriOptionsSetup)
                              .UseHttpMethod(httpMethod)
                              .ExpectContent(contentCheckFunc);

                return 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(name)));
        }
 public UriHealthCheck(UriHealthCheckOptions options, Func <HttpClient> httpClientFactory)
 {
     _options           = options ?? throw new ArgumentNullException(nameof(options));
     _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
 }
 public UriHealthCheck(UriHealthCheckOptions options)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
 }