Tags defined during health check registration are now available in th… (dotnet/extensions#1434)

* 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 8eaeab2a69
This commit is contained in:
viktorpeacock 2019-04-17 16:10:12 +01:00 committed by Ryan Nowak
parent 6867084489
commit f86f24b50a
4 changed files with 37 additions and 5 deletions

View File

@ -47,11 +47,13 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
private object _dummy; private object _dummy;
private int _dummyPrimitive; private int _dummyPrimitive;
public HealthReportEntry(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description, System.TimeSpan duration, System.Exception exception, System.Collections.Generic.IReadOnlyDictionary<string, object> data) { throw null; } public HealthReportEntry(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description, System.TimeSpan duration, System.Exception exception, System.Collections.Generic.IReadOnlyDictionary<string, object> data) { throw null; }
public HealthReportEntry(Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus status, string description, System.TimeSpan duration, System.Exception exception, System.Collections.Generic.IReadOnlyDictionary<string, object> data, System.Collections.Generic.IEnumerable<string> tags = null) { throw null; }
public System.Collections.Generic.IReadOnlyDictionary<string, object> Data { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public System.Collections.Generic.IReadOnlyDictionary<string, object> Data { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public string Description { [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.TimeSpan Duration { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public System.Exception Exception { [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 Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus Status { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public System.Collections.Generic.IEnumerable<string> Tags { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
} }
public enum HealthStatus public enum HealthStatus
{ {

View File

@ -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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Extensions.Diagnostics.HealthChecks namespace Microsoft.Extensions.Diagnostics.HealthChecks
{ {
@ -23,14 +24,31 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
/// <param name="exception">An <see cref="Exception"/> representing the exception that was thrown when checking for status (if any).</param> /// <param name="exception">An <see cref="Exception"/> representing the exception that was thrown when checking for status (if any).</param>
/// <param name="data">Additional key-value pairs describing the health of the component.</param> /// <param name="data">Additional key-value pairs describing the health of the component.</param>
public HealthReportEntry(HealthStatus status, string description, TimeSpan duration, Exception exception, IReadOnlyDictionary<string, object> data) public HealthReportEntry(HealthStatus status, string description, TimeSpan duration, Exception exception, IReadOnlyDictionary<string, object> data)
: this(status, description, duration, exception, data, null)
{
}
/// <summary>
/// Creates a new <see cref="HealthReportEntry"/> with the specified values for <paramref name="status"/>, <paramref name="exception"/>,
/// <paramref name="description"/>, and <paramref name="data"/>.
/// </summary>
/// <param name="status">A value indicating the health status of the component that was checked.</param>
/// <param name="description">A human-readable description of the status of the component that was checked.</param>
/// <param name="duration">A value indicating the health execution duration.</param>
/// <param name="exception">An <see cref="Exception"/> representing the exception that was thrown when checking for status (if any).</param>
/// <param name="data">Additional key-value pairs describing the health of the component.</param>
/// <param name="tags">Tags associated with the health check that generated the report entry.</param>
public HealthReportEntry(HealthStatus status, string description, TimeSpan duration, Exception exception, IReadOnlyDictionary<string, object> data, IEnumerable<string> tags = null)
{ {
Status = status; Status = status;
Description = description; Description = description;
Duration = duration; Duration = duration;
Exception = exception; Exception = exception;
Data = data ?? _emptyReadOnlyDictionary; Data = data ?? _emptyReadOnlyDictionary;
Tags = tags ?? Enumerable.Empty<string>();
} }
/// <summary> /// <summary>
/// Gets additional key-value pairs describing the health of the component. /// Gets additional key-value pairs describing the health of the component.
/// </summary> /// </summary>
@ -55,5 +73,10 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
/// Gets the health status of the component that was checked. /// Gets the health status of the component that was checked.
/// </summary> /// </summary>
public HealthStatus Status { get; } public HealthStatus Status { get; }
/// <summary>
/// Gets the tags associated with the health check.
/// </summary>
public IEnumerable<string> Tags { get; }
} }
} }

View File

@ -111,7 +111,8 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
description: result.Description, description: result.Description,
duration: duration, duration: duration,
exception: result.Exception, exception: result.Exception,
data: result.Data); data: result.Data,
tags: registration.Tags);
Log.HealthCheckEnd(_logger, registration, entry, duration); Log.HealthCheckEnd(_logger, registration, entry, duration);
Log.HealthCheckData(_logger, registration, entry); Log.HealthCheckData(_logger, registration, entry);

View File

@ -57,6 +57,9 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
const string UnhealthyMessage = "Halp!"; const string UnhealthyMessage = "Halp!";
const string HealthyMessage = "Everything is A-OK"; const string HealthyMessage = "Everything is A-OK";
var exception = new Exception("Things are pretty bad!"); var exception = new Exception("Things are pretty bad!");
var healthyCheckTags = new List<string> { "healthy-check-tag" };
var degradedCheckTags = new List<string> { "degraded-check-tag" };
var unhealthyCheckTags = new List<string> { "unhealthy-check-tag" };
// Arrange // Arrange
var data = new Dictionary<string, object>() var data = new Dictionary<string, object>()
@ -66,9 +69,9 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
var service = CreateHealthChecksService(b => var service = CreateHealthChecksService(b =>
{ {
b.AddAsyncCheck("HealthyCheck", _ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage, data))); b.AddAsyncCheck("HealthyCheck", _ => Task.FromResult(HealthCheckResult.Healthy(HealthyMessage, data)), healthyCheckTags);
b.AddAsyncCheck("DegradedCheck", _ => Task.FromResult(HealthCheckResult.Degraded(DegradedMessage))); b.AddAsyncCheck("DegradedCheck", _ => Task.FromResult(HealthCheckResult.Degraded(DegradedMessage)), degradedCheckTags);
b.AddAsyncCheck("UnhealthyCheck", _ => Task.FromResult(HealthCheckResult.Unhealthy(UnhealthyMessage, exception))); b.AddAsyncCheck("UnhealthyCheck", _ => Task.FromResult(HealthCheckResult.Unhealthy(UnhealthyMessage, exception)), unhealthyCheckTags);
}); });
// Act // Act
@ -84,6 +87,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
Assert.Equal(HealthStatus.Degraded, actual.Value.Status); Assert.Equal(HealthStatus.Degraded, actual.Value.Status);
Assert.Null(actual.Value.Exception); Assert.Null(actual.Value.Exception);
Assert.Empty(actual.Value.Data); Assert.Empty(actual.Value.Data);
Assert.Equal(actual.Value.Tags, degradedCheckTags);
}, },
actual => actual =>
{ {
@ -96,6 +100,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
Assert.Equal(DataKey, item.Key); Assert.Equal(DataKey, item.Key);
Assert.Equal(DataValue, item.Value); Assert.Equal(DataValue, item.Value);
}); });
Assert.Equal(actual.Value.Tags, healthyCheckTags);
}, },
actual => actual =>
{ {
@ -104,6 +109,7 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status); Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status);
Assert.Same(exception, actual.Value.Exception); Assert.Same(exception, actual.Value.Exception);
Assert.Empty(actual.Value.Data); Assert.Empty(actual.Value.Data);
Assert.Equal(actual.Value.Tags, unhealthyCheckTags);
}); });
} }