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

Update to latest #4013

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,8 +3,8 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Diagnostics.HealthChecks;
Expand All @@ -20,13 +20,20 @@ public static partial class CoreHealthChecksExtensions
/// <param name="builder">The builder to add the provider to.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="builder" /> is <see langword="null" />.</exception>
/// <remarks>The application's lifecycle is tracked through <see cref="IHostApplicationLifetime"/>.</remarks>
public static IHealthChecksBuilder AddApplicationLifecycleHealthCheck(this IHealthChecksBuilder builder)
{
_ = Throw.IfNull(builder);
=> Throw.IfNull(builder)
.AddCheck<ApplicationLifecycleHealthCheck>("ApplicationLifecycleHealthCheck");

return builder.AddCheck<ApplicationLifecycleHealthCheck>("ApplicationLifecycleHealthCheck");
}
/// <summary>
/// Registers a health check provider that's tied to the application's lifecycle.
/// </summary>
/// <param name="builder">The builder to add the provider to.</param>
/// <param name="tags">A list of tags that can be used to filter health checks.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
[Experimental]
public static IHealthChecksBuilder AddApplicationLifecycleHealthCheck(this IHealthChecksBuilder builder, params string[] tags)
=> Throw.IfNull(builder)
.AddCheck<ApplicationLifecycleHealthCheck>("ApplicationLifecycleHealthCheck", tags: Throw.IfNull(tags));

/// <summary>
/// Registers a health check provider that's tied to the application's lifecycle.
Expand All @@ -35,12 +42,7 @@ public static IHealthChecksBuilder AddApplicationLifecycleHealthCheck(this IHeal
/// <param name="tags">A list of tags that can be used to filter health checks.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="builder" /> or <paramref name="tags"/> are <see langword="null" />.</exception>
/// <remarks>The application's lifecycle is tracked through <see cref="IHostApplicationLifetime"/>.</remarks>
public static IHealthChecksBuilder AddApplicationLifecycleHealthCheck(this IHealthChecksBuilder builder, IEnumerable<string> tags)
{
_ = Throw.IfNull(builder);
_ = Throw.IfNull(tags);

return builder.AddCheck<ApplicationLifecycleHealthCheck>("ApplicationLifecycleHealthCheck", tags: tags);
}
=> Throw.IfNull(builder)
.AddCheck<ApplicationLifecycleHealthCheck>("ApplicationLifecycleHealthCheck", tags: Throw.IfNull(tags));
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public static IHealthChecksBuilder AddManualHealthCheck(this IHealthChecksBuilde
.AddManualHealthCheckDependencies()
.AddCheck<ManualHealthCheckService>("ManualHealthCheck");

/// <summary>
/// Registers a health check provider that enables manual control of the application's health.
/// </summary>
/// <param name="builder">The builder to add the provider to.</param>
/// <param name="tags">A list of tags that can be used to filter health checks.</param>
/// <returns>The value of <paramref name="builder"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="builder" /> or <paramref name="tags"/> are <see langword="null" />.</exception>
public static IHealthChecksBuilder AddManualHealthCheck(this IHealthChecksBuilder builder, params string[] tags)
=> Throw.IfNull(builder)
.AddManualHealthCheckDependencies()
.AddCheck<ManualHealthCheckService>("ManualHealthCheck", tags: Throw.IfNull(tags));

/// <summary>
/// Registers a health check provider that enables manual control of the application's health.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Telemetry.Metering;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.Diagnostics.HealthChecks;

public static partial class CoreHealthChecksExtensions
{
/// <summary>
/// Registers a health status publisher which emits logs and metrics tracking the application's health.
/// Registers a health check publisher which emits telemetry representing the application's health.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the publisher to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
/// <param name="services">The dependency injection container to add the publisher to.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="services" /> is <see langword="null" />.</exception>
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services)
=> services
=> Throw.IfNull(services)
.RegisterMetering()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();

/// <summary>
/// Registers a health check publisher which emits telemetry representing the application's health.
/// </summary>
/// <param name="services">The dependency injection container to add the publisher to.</param>
/// <param name="section">Configuration for <see cref="TelemetryHealthCheckPublisherOptions"/>.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="services" /> or <paramref name="section"/> are <see langword="null" />.</exception>
[Experimental]
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, IConfigurationSection section)
=> Throw.IfNull(services)
.Configure<TelemetryHealthCheckPublisherOptions>(Throw.IfNull(section))
.RegisterMetering()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();

/// <summary>
/// Registers a health check publisher which emits telemetry representing the application's health.
/// </summary>
/// <param name="services">The dependency injection container to add the publisher to.</param>
/// <param name="configure">Configuration for <see cref="TelemetryHealthCheckPublisherOptions"/>.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="services" /> or <paramref name="configure"/> are <see langword="null" />.</exception>
[Experimental]
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, Action<TelemetryHealthCheckPublisherOptions> configure)
=> Throw.IfNull(services)
.Configure(Throw.IfNull(configure))
.RegisterMetering()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<UseEnumStringsGenerator>true</UseEnumStringsGenerator>
<InjectSharedTextFormatting>true</InjectSharedTextFormatting>
<InjectSharedPools>true</InjectSharedPools>
<InjectExperimentalAttributeOnLegacy>true</InjectExperimentalAttributeOnLegacy>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddApplicationLifecycleHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder);",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddApplicationLifecycleHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, params string[] tags);",
"Stage": "Experimental"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddApplicationLifecycleHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Collections.Generic.IEnumerable<string> tags);",
"Stage": "Stable"
Expand All @@ -29,6 +33,10 @@
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddManualHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder);",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddManualHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, params string[] tags);",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddManualHealthCheck(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, System.Collections.Generic.IEnumerable<string> tags);",
"Stage": "Stable"
Expand All @@ -37,6 +45,14 @@
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddTelemetryHealthCheckPublisher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services);",
"Stage": "Stable"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddTelemetryHealthCheckPublisher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfigurationSection section);",
"Stage": "Experimental"
},
{
"Member": "static Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.AddTelemetryHealthCheckPublisher(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Diagnostics.HealthChecks.TelemetryHealthCheckPublisherOptions> configure);",
"Stage": "Experimental"
},
{
"Member": "static void Microsoft.Extensions.Diagnostics.HealthChecks.CoreHealthChecksExtensions.ReportHealthy(this Microsoft.Extensions.Diagnostics.HealthChecks.IManualHealthCheck manualHealthCheck);",
"Stage": "Stable"
Expand Down Expand Up @@ -80,6 +96,22 @@
"Stage": "Stable"
}
]
},
{
"Type": "class Microsoft.Extensions.Diagnostics.HealthChecks.TelemetryHealthCheckPublisherOptions",
"Stage": "Experimental",
"Methods": [
{
"Member": "Microsoft.Extensions.Diagnostics.HealthChecks.TelemetryHealthCheckPublisherOptions.TelemetryHealthCheckPublisherOptions();",
"Stage": "Experimental"
}
],
"Properties": [
{
"Member": "bool Microsoft.Extensions.Diagnostics.HealthChecks.TelemetryHealthCheckPublisherOptions.LogOnlyUnhealthy { get; set; }",
"Stage": "Experimental"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Telemetry.Metering;
using Microsoft.Shared.Diagnostics;
using Microsoft.Shared.Pools;
Expand All @@ -18,14 +19,19 @@ internal sealed class TelemetryHealthCheckPublisher : IHealthCheckPublisher
private readonly HealthCheckReportCounter _healthCheckReportCounter;
private readonly UnhealthyHealthCheckCounter _unhealthyHealthCheckCounter;
private readonly ILogger _logger;
private readonly bool _logOnlyUnhealthy;

/// <summary>
/// Initializes a new instance of the <see cref="TelemetryHealthCheckPublisher"/> class.
/// </summary>
/// <param name="meter">The meter.</param>
/// <param name="logger">The logger.</param>
public TelemetryHealthCheckPublisher(Meter<TelemetryHealthCheckPublisher> meter, ILogger<TelemetryHealthCheckPublisher> logger)
/// <param name="options">Creation options.</param>
public TelemetryHealthCheckPublisher(Meter<TelemetryHealthCheckPublisher> meter, ILogger<TelemetryHealthCheckPublisher> logger, IOptions<TelemetryHealthCheckPublisherOptions> options)
{
var value = Throw.IfMemberNull(options, options.Value);
_logOnlyUnhealthy = Throw.IfMemberNull(options, options.Value.LogOnlyUnhealthy);

_logger = logger;
_healthCheckReportCounter = Metric.CreateHealthCheckReportCounter(meter);
_unhealthyHealthCheckCounter = Metric.CreateUnhealthyHealthCheckCounter(meter);
Expand All @@ -43,7 +49,11 @@ public Task PublishAsync(HealthReport report, CancellationToken cancellationToke

if (report.Status == HealthStatus.Healthy)
{
Log.Healthy(_logger, report.Status);
if (!_logOnlyUnhealthy)
{
Log.Healthy(_logger, report.Status);
}

_healthCheckReportCounter.RecordMetric(true, report.Status);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.Diagnostics.HealthChecks;

/// <summary>
/// Options for the telemetry health check publisher.
/// </summary>
[Experimental]
public class TelemetryHealthCheckPublisherOptions
{
/// <summary>
/// Gets or sets a value indicating whether to log only when unhealthy reports are received. Set to false to always log.
/// </summary>
/// <remarks>
/// Default set to false.
/// </remarks>
[Experimental]
public bool LogOnlyUnhealthy { get; set; }
}
Loading