Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
IoTSharp.AspNetCore.HealthChecks
提交
e8e10430
I
IoTSharp.AspNetCore.HealthChecks
项目概览
jobily
/
IoTSharp.AspNetCore.HealthChecks
8 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
IoTSharp.AspNetCore.HealthChecks
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e8e10430
编写于
6月 10, 2022
作者:
麦壳饼
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
加入InfluxDB的健康检查
上级
56ad4792
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
303 addition
and
0 deletion
+303
-0
src/HealthChecks.InfluxDB/DependencyInjection/InfluxDBHealthCheckBuilderExtensions.cs
...pendencyInjection/InfluxDBHealthCheckBuilderExtensions.cs
+200
-0
src/HealthChecks.InfluxDB/HealthChecks.InfluxDB.csproj
src/HealthChecks.InfluxDB/HealthChecks.InfluxDB.csproj
+16
-0
src/HealthChecks.InfluxDB/InfluxDBHealthCheck.cs
src/HealthChecks.InfluxDB/InfluxDBHealthCheck.cs
+47
-0
src/HealthChecks.InfluxDB/README.md
src/HealthChecks.InfluxDB/README.md
+40
-0
未找到文件。
src/HealthChecks.InfluxDB/DependencyInjection/InfluxDBHealthCheckBuilderExtensions.cs
0 → 100644
浏览文件 @
e8e10430
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
));
}
}
}
src/HealthChecks.InfluxDB/HealthChecks.InfluxDB.csproj
0 → 100644
浏览文件 @
e8e10430
<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>
src/HealthChecks.InfluxDB/InfluxDBHealthCheck.cs
0 → 100644
浏览文件 @
e8e10430
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
();
}
}
src/HealthChecks.InfluxDB/README.md
0 → 100644
浏览文件 @
e8e10430
# 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录