-
Notifications
You must be signed in to change notification settings - Fork 728
/
CommonHealthChecksExtensions.TelemetryPublisher.cs
53 lines (48 loc) · 3.04 KB
/
CommonHealthChecksExtensions.TelemetryPublisher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.DependencyInjection;
public static partial class CommonHealthChecksExtensions
{
/// <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>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services" /> is <see langword="null" />.</exception>
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services)
=> Throw.IfNull(services)
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.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"><paramref name="services" /> or <paramref name="section"/> is <see langword="null" />.</exception>
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, IConfigurationSection section)
=> Throw.IfNull(services)
.Configure<TelemetryHealthCheckPublisherOptions>(Throw.IfNull(section))
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.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"><paramref name="services" /> or <paramref name="configure"/> is <see langword="null" />.</exception>
public static IServiceCollection AddTelemetryHealthCheckPublisher(this IServiceCollection services, Action<TelemetryHealthCheckPublisherOptions> configure)
=> Throw.IfNull(services)
.Configure(Throw.IfNull(configure))
.AddMetrics()
.AddSingleton<HealthCheckMetrics>()
.AddSingleton<IHealthCheckPublisher, TelemetryHealthCheckPublisher>();
}