Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude reports that indicate a healthy status #143

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -21,14 +22,20 @@ class ApplicationInsightsPublisher
private static TelemetryClient _client;
private static object sync_root = new object();
private readonly bool _saveDetailedReport;
private readonly bool _excludeHealthyReports;

public ApplicationInsightsPublisher(string instrumentationKey = default, bool saveDetailedReport = false)
public ApplicationInsightsPublisher(string instrumentationKey = default, bool saveDetailedReport = false, bool excludeHealthyReports = false)
{
_instrumentationKey = instrumentationKey;
_saveDetailedReport = saveDetailedReport;
_excludeHealthyReports = excludeHealthyReports;
}

public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
{
if (report.Status == HealthStatus.Healthy && _excludeHealthyReports)
return Task.CompletedTask;

var client = GetOrCreateTelemetryClient();

if (_saveDetailedReport)
Expand All @@ -39,11 +46,13 @@ public Task PublishAsync(HealthReport report, CancellationToken cancellationToke
{
SaveGeneralizedReport(report, client);
}

return Task.CompletedTask;
}
private static void SaveDetailedReport(HealthReport report, TelemetryClient client)

private void SaveDetailedReport(HealthReport report, TelemetryClient client)
{
foreach (var reportEntry in report.Entries)
foreach (var reportEntry in report.Entries.Where(entry => !_excludeHealthyReports || entry.Value.Status != HealthStatus.Healthy))
{
client.TrackEvent($"{EVENT_NAME}:{reportEntry.Key}",
properties: new Dictionary<string, string>()
Expand All @@ -59,6 +68,7 @@ private static void SaveDetailedReport(HealthReport report, TelemetryClient clie
});
}
}

private static void SaveGeneralizedReport(HealthReport report, TelemetryClient client)
{
client.TrackEvent(EVENT_NAME,
Expand All @@ -73,6 +83,7 @@ private static void SaveGeneralizedReport(HealthReport report, TelemetryClient c
{ METRIC_DURATION_NAME,report.TotalDuration.TotalMilliseconds}
});
}

TelemetryClient GetOrCreateTelemetryClient()
{
if (_client == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public static class ApplicationInsightsHealthCheckBuilderExtensions
/// </remarks>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="instrumentationKey">Specified Application Insights instrumentation key. Optional. If <c>null</c> TelemetryConfiguration.Active is used.</param>
/// <param name="saveDetailedReport">Specifies if save an Application Isnghts event for each HealthCheck or just save one event with the global status for all the HealthChecks. Optional: If <c>true</c> saves an Application Insights event for each HEalthCheck</c></param>
/// <param name="saveDetailedReport">Specifies if save an Application Insights event for each HealthCheck or just save one event with the global status for all the HealthChecks. Optional: If <c>true</c> saves an Application Insights event for each HealthCheck</c></param>
/// <param name="excludeHealthyReports">Specifies if save an Application Insights event only for reports indicating an unhealthy status</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
public static IHealthChecksBuilder AddApplicationInsightsPublisher(this IHealthChecksBuilder builder, string instrumentationKey = default, bool saveDetailedReport = false)
public static IHealthChecksBuilder AddApplicationInsightsPublisher(this IHealthChecksBuilder builder, string instrumentationKey = default, bool saveDetailedReport = false, bool excludeHealthyReports = false)
{
builder.Services
.AddSingleton<IHealthCheckPublisher>(sp =>
{
return new ApplicationInsightsPublisher(instrumentationKey, saveDetailedReport);
return new ApplicationInsightsPublisher(instrumentationKey, saveDetailedReport, excludeHealthyReports);
});

return builder;
Expand Down