From f86f24b50a22105414b917e417f453992b986f3b Mon Sep 17 00:00:00 2001 From: viktorpeacock <44648849+viktorpeacock@users.noreply.github.com> Date: Wed, 17 Apr 2019 16:10:12 +0100 Subject: [PATCH] =?UTF-8?q?Tags=20defined=20during=20health=20check=20regi?= =?UTF-8?q?stration=20are=20now=20available=20in=20th=E2=80=A6=20(dotnet/e?= =?UTF-8?q?xtensions#1434)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Tags defined during health check registration are now available in the health report entries. * Additional constructor that accepts tags. Original constructor now calls a new one. * Update reference assemblies \n\nCommit migrated from https://github.com/dotnet/extensions/commit/8eaeab2a69bcb91acfedc7847a345f8b64994d89 --- ...ealthChecks.Abstractions.netstandard2.0.cs | 2 ++ .../Abstractions/src/HealthReportEntry.cs | 25 ++++++++++++++++++- .../src/DefaultHealthCheckService.cs | 3 ++- .../test/DefaultHealthCheckServiceTest.cs | 12 ++++++--- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/HealthChecks/Abstractions/ref/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.netstandard2.0.cs b/src/HealthChecks/Abstractions/ref/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.netstandard2.0.cs index 9ab497257e..b229ca0d53 100644 --- a/src/HealthChecks/Abstractions/ref/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.netstandard2.0.cs +++ b/src/HealthChecks/Abstractions/ref/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.netstandard2.0.cs @@ -47,11 +47,13 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks private object _dummy; private int _dummyPrimitive; public HealthReportEntry(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description, System.TimeSpan duration, System.Exception exception, System.Collections.Generic.IReadOnlyDictionary data) { throw null; } + public HealthReportEntry(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description, System.TimeSpan duration, System.Exception exception, System.Collections.Generic.IReadOnlyDictionary data, System.Collections.Generic.IEnumerable tags = null) { throw null; } public System.Collections.Generic.IReadOnlyDictionary Data { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public string Description { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public System.TimeSpan Duration { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public System.Exception Exception { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus Status { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } + public System.Collections.Generic.IEnumerable Tags { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } } public enum HealthStatus { diff --git a/src/HealthChecks/Abstractions/src/HealthReportEntry.cs b/src/HealthChecks/Abstractions/src/HealthReportEntry.cs index 6e7d6c6b8e..043c1414a4 100644 --- a/src/HealthChecks/Abstractions/src/HealthReportEntry.cs +++ b/src/HealthChecks/Abstractions/src/HealthReportEntry.cs @@ -1,8 +1,9 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; +using System.Linq; namespace Microsoft.Extensions.Diagnostics.HealthChecks { @@ -23,14 +24,31 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks /// An representing the exception that was thrown when checking for status (if any). /// Additional key-value pairs describing the health of the component. public HealthReportEntry(HealthStatus status, string description, TimeSpan duration, Exception exception, IReadOnlyDictionary data) + : this(status, description, duration, exception, data, null) + { + } + + /// + /// Creates a new with the specified values for , , + /// , and . + /// + /// A value indicating the health status of the component that was checked. + /// A human-readable description of the status of the component that was checked. + /// A value indicating the health execution duration. + /// An representing the exception that was thrown when checking for status (if any). + /// Additional key-value pairs describing the health of the component. + /// Tags associated with the health check that generated the report entry. + public HealthReportEntry(HealthStatus status, string description, TimeSpan duration, Exception exception, IReadOnlyDictionary data, IEnumerable tags = null) { Status = status; Description = description; Duration = duration; Exception = exception; Data = data ?? _emptyReadOnlyDictionary; + Tags = tags ?? Enumerable.Empty(); } + /// /// Gets additional key-value pairs describing the health of the component. /// @@ -55,5 +73,10 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks /// Gets the health status of the component that was checked. /// public HealthStatus Status { get; } + + /// + /// Gets the tags associated with the health check. + /// + public IEnumerable Tags { get; } } } diff --git a/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs b/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs index c2c9084e0a..d7ce9edb9e 100644 --- a/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs +++ b/src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs @@ -111,7 +111,8 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks description: result.Description, duration: duration, exception: result.Exception, - data: result.Data); + data: result.Data, + tags: registration.Tags); Log.HealthCheckEnd(_logger, registration, entry, duration); Log.HealthCheckData(_logger, registration, entry); diff --git a/src/HealthChecks/HealthChecks/test/DefaultHealthCheckServiceTest.cs b/src/HealthChecks/HealthChecks/test/DefaultHealthCheckServiceTest.cs index 38442edb93..50cf7ebeae 100644 --- a/src/HealthChecks/HealthChecks/test/DefaultHealthCheckServiceTest.cs +++ b/src/HealthChecks/HealthChecks/test/DefaultHealthCheckServiceTest.cs @@ -57,6 +57,9 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks const string UnhealthyMessage = "Halp!"; const string HealthyMessage = "Everything is A-OK"; var exception = new Exception("Things are pretty bad!"); + var healthyCheckTags = new List { "healthy-check-tag" }; + var degradedCheckTags = new List { "degraded-check-tag" }; + var unhealthyCheckTags = new List { "unhealthy-check-tag" }; // Arrange var data = new Dictionary() @@ -66,9 +69,9 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks var service = CreateHealthChecksService(b => { - b.AddAsyncCheck("HealthyCheck", _ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage, data))); - b.AddAsyncCheck("DegradedCheck", _ => Task.FromResult(HealthCheckResult.Degraded(DegradedMessage))); - b.AddAsyncCheck("UnhealthyCheck", _ => Task.FromResult(HealthCheckResult.Unhealthy(UnhealthyMessage, exception))); + b.AddAsyncCheck("HealthyCheck", _ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage, data)), healthyCheckTags); + b.AddAsyncCheck("DegradedCheck", _ => Task.FromResult(HealthCheckResult.Degraded(DegradedMessage)), degradedCheckTags); + b.AddAsyncCheck("UnhealthyCheck", _ => Task.FromResult(HealthCheckResult.Unhealthy(UnhealthyMessage, exception)), unhealthyCheckTags); }); // Act @@ -84,6 +87,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks Assert.Equal(HealthStatus.Degraded, actual.Value.Status); Assert.Null(actual.Value.Exception); Assert.Empty(actual.Value.Data); + Assert.Equal(actual.Value.Tags, degradedCheckTags); }, actual => { @@ -96,6 +100,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks Assert.Equal(DataKey, item.Key); Assert.Equal(DataValue, item.Value); }); + Assert.Equal(actual.Value.Tags, healthyCheckTags); }, actual => { @@ -104,6 +109,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status); Assert.Same(exception, actual.Value.Exception); Assert.Empty(actual.Value.Data); + Assert.Equal(actual.Value.Tags, unhealthyCheckTags); }); }