Skip to content

Commit d1d9b97

Browse files
Miroslav Jezikrynowak
authored andcommitted
Add tags to HealthReportEntry if an exception occurs
1 parent 15d5ffe commit d1d9b97

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private async Task<HealthReportEntry> RunCheckAsync(IServiceScope scope, HealthC
125125
description: "A timeout occurred while running check.",
126126
duration: duration,
127127
exception: ex,
128-
data: null);
128+
data: null,
129+
tags: registration.Tags);
129130

130131
Log.HealthCheckError(_logger, registration, ex, duration);
131132
}
@@ -139,7 +140,8 @@ private async Task<HealthReportEntry> RunCheckAsync(IServiceScope scope, HealthC
139140
description: ex.Message,
140141
duration: duration,
141142
exception: ex,
142-
data: null);
143+
data: null,
144+
tags: registration.Tags);
143145

144146
Log.HealthCheckError(_logger, registration, ex, duration);
145147
}

src/HealthChecks/HealthChecks/test/DefaultHealthCheckServiceTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,47 @@ public async Task CheckAsync_RunsAllChecksAndAggregatesResultsAsync()
113113
});
114114
}
115115

116+
[Fact]
117+
public async Task CheckAsync_TagsArePresentInHealthReportEntryIfExceptionOccurs()
118+
{
119+
const string ExceptionMessage = "exception-message";
120+
const string OperationCancelledMessage = "operation-cancelled-message";
121+
var exceptionTags = new[] { "unhealthy-check-tag" };
122+
var operationExceptionTags = new[] { "degraded-check-tag" };
123+
124+
// Arrange
125+
var service = CreateHealthChecksService(b =>
126+
{
127+
b.AddAsyncCheck("ExceptionCheck", _ => throw new Exception(ExceptionMessage), exceptionTags);
128+
b.AddAsyncCheck("OperationExceptionCheck", _ => throw new OperationCanceledException(OperationCancelledMessage), operationExceptionTags);
129+
});
130+
131+
// Act
132+
var results = await service.CheckHealthAsync();
133+
134+
// Assert
135+
Assert.Collection(
136+
results.Entries.OrderBy(kvp => kvp.Key),
137+
actual =>
138+
{
139+
Assert.Equal("ExceptionCheck", actual.Key);
140+
Assert.Equal(ExceptionMessage, actual.Value.Description);
141+
Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status);
142+
Assert.Equal(ExceptionMessage, actual.Value.Exception.Message);
143+
Assert.Empty(actual.Value.Data);
144+
Assert.Equal(actual.Value.Tags, exceptionTags);
145+
},
146+
actual =>
147+
{
148+
Assert.Equal("OperationExceptionCheck", actual.Key);
149+
Assert.Equal("A timeout occurred while running check.", actual.Value.Description);
150+
Assert.Equal(HealthStatus.Unhealthy, actual.Value.Status);
151+
Assert.Equal(OperationCancelledMessage, actual.Value.Exception.Message);
152+
Assert.Empty(actual.Value.Data);
153+
Assert.Equal(actual.Value.Tags, operationExceptionTags);
154+
});
155+
}
156+
116157
[Fact]
117158
public async Task CheckAsync_RunsFilteredChecksAndAggregatesResultsAsync()
118159
{

0 commit comments

Comments
 (0)