提交 e8e10430 编写于 作者: 麦壳饼's avatar 麦壳饼

加入InfluxDB的健康检查

上级 56ad4792
using HealthChecks.InfluxDB;
using InfluxDB.Client;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
namespace Microsoft.Extensions.DependencyInjection
{
public static class InfluxDBHealthCheckBuilderExtensions
{
private const string NAME = "InfluxDB";
/// <summary>
/// Add a health check for InfluxDB services using connection string.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="influxConnectionString">The InfluxDB connection string to be used.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, string influxConnectionString, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(influxConnectionString));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB services using connection string.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="influxConnectionString">The InfluxDB connection string to be used.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, Uri influxConnectionString, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(influxConnectionString));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB V1.0 services using url.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="url"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="database"></param>
/// <param name="retentionPolicy"></param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDBV1(this IHealthChecksBuilder builder, string url, string username, char[] password, string database, string retentionPolicy, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(url, username, password, database, retentionPolicy));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB services using url with token.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="url"></param>
/// <param name="token"></param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, string url, string token, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(url, token));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB services using url.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="url"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, string url, string username, char[] password, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(url, username, password));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB services using <see cref="InfluxDBClientOptions"/> .
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="options"></param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, InfluxDBClientOptions options, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services
.AddSingleton(sp => new InfluxDBHealthCheck(options));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
/// <summary>
/// Add a health check for InfluxDB services using <see cref="InfluxDBClient"/> from service provider.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'InfluxDB' 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>
public static IHealthChecksBuilder AddInfluxDB(this IHealthChecksBuilder builder, string? name = default, HealthStatus? failureStatus = default, IEnumerable<string>? tags = default, TimeSpan? timeout = default)
{
builder.Services.AddSingleton(sp => new InfluxDBHealthCheck(sp.GetRequiredService<InfluxDBClient>()));
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => sp.GetRequiredService<InfluxDBHealthCheck>(),
failureStatus,
tags,
timeout));
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
<PackageTags>HealthCheck;Health;InfluxDB</PackageTags>
<Description>HealthChecks.InfluxDB is the health check package for InfluxDB.</Description>
<PackageId>IoTSharp.HealthChecks.InfluxDB</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="InfluxDB.Client" Version="4.2.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="6.0.5" />
</ItemGroup>
</Project>
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace HealthChecks.InfluxDB
{
public class InfluxDBHealthCheck
: IHealthCheck, IDisposable
{
private readonly InfluxDBClient _influxdb_client;
public InfluxDBHealthCheck(string url, string username, char[] password) => _influxdb_client = InfluxDBClientFactory.Create(url, username, password);
public InfluxDBHealthCheck(string url, string token) => _influxdb_client = InfluxDBClientFactory.Create(url, token);
public InfluxDBHealthCheck(InfluxDBClientOptions options) => _influxdb_client = InfluxDBClientFactory.Create(options);
public InfluxDBHealthCheck(string url, string username, char[] password, string database, string retentionPolicy) => _influxdb_client = InfluxDBClientFactory.CreateV1(url, username, password, database, retentionPolicy);
public InfluxDBHealthCheck(string influxDBConnectionString) => _influxdb_client = InfluxDBClientFactory.Create(influxDBConnectionString);
public InfluxDBHealthCheck(InfluxDBClient influxdb_client) => _influxdb_client = influxdb_client;
public InfluxDBHealthCheck(Uri influxDBConnectionString) => _influxdb_client = InfluxDBClientFactory.Create(influxDBConnectionString.ToString());
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var ready = await _influxdb_client.ReadyAsync();
var ping = await _influxdb_client.PingAsync();
return new HealthCheckResult(ping && ready.Status == Ready.StatusEnum.Ready ? HealthStatus.Healthy : context.Registration.FailureStatus, $"Status:{ready.Status} Started:{ready.Started} Up:{ready.Up}");
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
public void Dispose() => _influxdb_client.Dispose();
}
}
# InfluxDB Health Check
This health check verifies the ability to communicate with a InfluxDB server.
## Example Usage
With all of the following examples, you can additionally add the following parameters:
- `name`: The health check name. Default if not specified is `InfluxDB`.
- `failureStatus`: The `HealthStatus` that should be reported when the health check fails. Default is `HealthStatus.Unhealthy`.
- `tags`: A list of tags that can be used to filter sets of health checks.
- `timeout`: A `System.TimeSpan` representing the timeout of the check.
### Basic
This will create a new `InfluxDBClient` and reuse it on every request to get the health check result. Use
the extension method where you provide the `Uri` to connect with.
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddInfluxDB("http://localhost:8086/?org=iotsharp&bucket=iotsharp-bucket&token=iotsharp-token");
}
```
If you are sharing a single `InfluxDBClient` for every time a health check is requested,
you must ensure automatic recovery is enabled so that the `InfluxDBClient` can be re-established if lost.
```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<InfluxDBClient>(sp =>
{
return InfluxDBClientFactory.Create("http://localhost:8086/?org=iotsharp&bucket=iotsharp-bucket&token=iotsharp-token");
})
.AddHealthChecks()
.AddInfluxDB();
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册