This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
HealthResultsAsMetricsReporter.cs
98 lines (81 loc) · 3.83 KB
/
HealthResultsAsMetricsReporter.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
// <copyright file="HealthResultsAsMetricsReporter.cs" company="App Metrics Contributors">
// Copyright (c) App Metrics Contributors. All rights reserved.
// </copyright>
using System;
using System.Threading;
using System.Threading.Tasks;
using App.Metrics.Health.Logging;
#if !NETSTANDARD1_6
using App.Metrics.Health.Internal;
#endif
using App.Metrics.Health.Reporting.Metrics.Internal;
namespace App.Metrics.Health.Reporting.Metrics
{
public class HealthResultsAsMetricsReporter : IReportHealthStatus
{
private static readonly ILog Logger = LogProvider.For<HealthResultsAsMetricsReporter>();
private readonly IMetrics _metrics;
private readonly HealthAsMetricsOptions _healthAsMetricsOptions;
public HealthResultsAsMetricsReporter(IMetrics metrics)
: this(metrics, new HealthAsMetricsOptions())
{
}
public HealthResultsAsMetricsReporter(IMetrics metrics, HealthAsMetricsOptions healthAsMetricsOptions)
{
_healthAsMetricsOptions = healthAsMetricsOptions ?? throw new ArgumentNullException(nameof(healthAsMetricsOptions));
_metrics = metrics ?? throw new ArgumentNullException(nameof(metrics));
ReportInterval = healthAsMetricsOptions.ReportInterval > TimeSpan.Zero
? healthAsMetricsOptions.ReportInterval
: HealthConstants.Reporting.DefaultReportInterval;
Logger.Trace($"Using Metrics Reporter {this}. ReportInterval: {ReportInterval}");
}
/// <inheritdoc />
public TimeSpan ReportInterval { get; set; }
/// <inheritdoc />
public Task ReportAsync(HealthOptions options, HealthStatus status, CancellationToken cancellationToken = default)
{
if (!_healthAsMetricsOptions.Enabled || !options.Enabled)
{
Logger.Trace($"Health Status Reporter `{this}` disabled, not reporting.");
#if NETSTANDARD1_6
return Task.CompletedTask;
#else
return AppMetricsHealthTaskHelper.CompletedTask();
#endif
}
Logger.Trace($"Health Status Reporter `{this}` reporting health status.");
foreach (var healthResult in status.Results)
{
var tags = new MetricTags(HealthReportingConstants.TagKeys.HealthCheckName, healthResult.Name);
if (healthResult.Check.Status == HealthCheckStatus.Degraded)
{
_metrics.Measure.Gauge.SetValue(ApplicationHealthMetricRegistry.Checks, tags, HealthConstants.HealthScore.degraded);
}
else if (healthResult.Check.Status == HealthCheckStatus.Unhealthy)
{
_metrics.Measure.Gauge.SetValue(ApplicationHealthMetricRegistry.Checks, tags, HealthConstants.HealthScore.unhealthy);
}
else if (healthResult.Check.Status == HealthCheckStatus.Healthy)
{
_metrics.Measure.Gauge.SetValue(ApplicationHealthMetricRegistry.Checks, tags, HealthConstants.HealthScore.healthy);
}
}
var overallHealthStatus = HealthConstants.HealthScore.healthy;
if (status.Status == HealthCheckStatus.Unhealthy)
{
overallHealthStatus = HealthConstants.HealthScore.unhealthy;
}
else if (status.Status == HealthCheckStatus.Degraded)
{
overallHealthStatus = HealthConstants.HealthScore.degraded;
}
_metrics.Measure.Gauge.SetValue(ApplicationHealthMetricRegistry.HealthGauge, overallHealthStatus);
Logger.Trace($"Health Status Reporter `{this}` successfully reported health status.");
#if NETSTANDARD1_6
return Task.CompletedTask;
#else
return AppMetricsHealthTaskHelper.CompletedTask();
#endif
}
}
}