This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHealthCheckContextExtensions.cs
163 lines (149 loc) · 5.29 KB
/
HealthCheckContextExtensions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System.Data;
using System.Data.SqlClient;
using System.Text;
using AspNetCore.Health.Internal;
using CoreFtp;
using MongoDB.Driver;
namespace AspNetCore.Health
{
public static class HealthCheckContextExtensions
{
public static HealthCheckContext AddUrlCheck(
this HealthCheckContext checkContext,
string url)
{
checkContext.Add(url, async () =>
{
try
{
var response = await HttpClientSingleton.Instance.GetAsync(url).ConfigureAwait(false);
return response.IsSuccessStatusCode
? HealthCheckResult.Healthy($"{url}")
: HealthCheckResult.Unhealthy($"{url}");
}
catch
{
return HealthCheckResult.Unhealthy($"{url}");
}
});
return checkContext;
}
public static HealthCheckContext AddUrlChecks(
this HealthCheckContext checkContext,
string[] urls,
string group)
{
checkContext.Add(group, async () =>
{
var successfulChecks = 0;
var description = new StringBuilder();
foreach (var url in urls)
{
try
{
var response = await HttpClientSingleton.Instance.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
successfulChecks++;
description.Append($"{url} OK");
}
else
{
description.Append($"{url} ERROR");
}
}
catch
{
description.Append($"{url} ERROR");
}
}
if (successfulChecks == urls.Length)
{
return HealthCheckResult.Healthy(description.ToString());
}
if (successfulChecks > 0)
{
return HealthCheckResult.Warning(description.ToString());
}
return HealthCheckResult.Unhealthy(description.ToString());
});
return checkContext;
}
public static HealthCheckContext AddFtp(
this HealthCheckContext checkContext,
string host,
string username,
string password,
int port,
FtpTransferMode mode,
string description)
{
checkContext.Add(host, async () =>
{
try
{
using (var ftpClient = new FtpClient(new FtpClientConfiguration
{
Host = host,
Username = username,
Password = password,
Port = port,
Mode = (CoreFtp.Enum.FtpTransferMode)mode
}))
{
await ftpClient.LoginAsync().ConfigureAwait(false);
}
return HealthCheckResult.Healthy(host);
}
catch
{
return HealthCheckResult.Unhealthy(host);
}
});
return checkContext;
}
public static HealthCheckContext AddSqlDatabase(this HealthCheckContext checkContext, IDbConnection dbConnection)
{
return AddSqlDatabase(checkContext, dbConnection.Database, dbConnection.ConnectionString);
}
public static HealthCheckContext AddSqlDatabase(this HealthCheckContext checkContext, string database, string connectionString)
{
SqlConnection connection = null;
checkContext.Add(database, async () =>
{
try
{
connection = new SqlConnection(connectionString);
await connection.OpenAsync();
return HealthCheckResult.Healthy(database);
}
catch
{
return HealthCheckResult.Unhealthy(database);
}
finally
{
connection?.Close();
}
});
return checkContext;
}
public static HealthCheckContext AddMongoDatabase(this HealthCheckContext checkContext, string database, string connectionString)
{
checkContext.Add(database, () =>
{
try
{
IMongoClient moClient = new MongoClient(connectionString);
var state = moClient.Cluster.Description.State;
return state == MongoDB.Driver.Core.Clusters.ClusterState.Connected ? HealthCheckResult.Healthy(database) : HealthCheckResult.Unhealthy(database);
}
catch
{
return HealthCheckResult.Unhealthy(database);
}
});
return checkContext;
}
}
}