forked from NethermindEth/nethermind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeHealthChecks.cs
More file actions
68 lines (60 loc) · 2.51 KB
/
NodeHealthChecks.cs
File metadata and controls
68 lines (60 loc) · 2.51 KB
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
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Nethermind.Logging;
namespace Nethermind.HealthChecks
{
public class NodeHealthCheck : IHealthCheck
{
private readonly INodeHealthService _nodeHealthService;
private readonly ILogger _logger;
public NodeHealthCheck(
INodeHealthService nodeHealthService,
ILogManager logManager)
{
_nodeHealthService = nodeHealthService ?? throw new ArgumentNullException(nameof(nodeHealthService));
_logger = logManager.GetClassLogger();
}
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
CheckHealthResult healthResult = _nodeHealthService.CheckHealth();
if (_logger.IsTrace) _logger.Trace($"Checked health result. Healthy: {healthResult.Healthy}");
string description = FormatMessages(healthResult.Messages.Select(static x => x.LongMessage));
if (healthResult.Healthy)
return Task.FromResult(HealthCheckResult.Healthy(description, CreateData(healthResult)));
return Task.FromResult(HealthCheckResult.Unhealthy(description, null, CreateData(healthResult)));
}
catch (Exception ex)
{
return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, exception: ex));
}
}
private static IReadOnlyDictionary<string, object> CreateData(CheckHealthResult healthResult)
{
return new Dictionary<string, object>
{
{ nameof(healthResult.IsSyncing), healthResult.IsSyncing },
{ nameof(healthResult.Errors), healthResult.Errors }
};
}
private static string FormatMessages(IEnumerable<string> messages)
{
if (messages.Any(static x => !string.IsNullOrWhiteSpace(x)))
{
var joined = string.Join(". ", messages.Where(static x => !string.IsNullOrWhiteSpace(x)));
if (!string.IsNullOrWhiteSpace(joined))
{
return joined + ".";
}
}
return string.Empty;
}
}
}