-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-design Azure IoTHub health checks to allow for managed identity an…
…d other features (#2216) Co-authored-by: Adam Sitnik <[email protected]>
- Loading branch information
1 parent
b68d35a
commit 133ff15
Showing
11 changed files
with
312 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/HealthChecks.Azure.IoTHub/IoTHubRegistryManagerHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.Azure.Devices; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.Azure.IoTHub; | ||
|
||
public sealed class IoTHubRegistryManagerHealthCheck : IHealthCheck | ||
{ | ||
private readonly RegistryManager _registryManager; | ||
private readonly string? _readQuery; | ||
private readonly string? _writeDeviceId; | ||
|
||
public IoTHubRegistryManagerHealthCheck(RegistryManager registryManager, string? readQuery = default, string? writeDeviceId = default) | ||
{ | ||
_registryManager = Guard.ThrowIfNull(registryManager); | ||
|
||
if (string.IsNullOrEmpty(readQuery) && string.IsNullOrEmpty(writeDeviceId)) | ||
{ | ||
throw new ArgumentException("Either readQuery or writeDeviceId has to be provided"); | ||
} | ||
|
||
_readQuery = readQuery; | ||
_writeDeviceId = writeDeviceId; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
if (!string.IsNullOrEmpty(_writeDeviceId)) | ||
{ | ||
await ExecuteRegistryWriteCheckAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await ExecuteRegistryReadCheckAsync().ConfigureAwait(false); | ||
} | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
|
||
private async Task ExecuteRegistryReadCheckAsync() | ||
{ | ||
var query = _registryManager.CreateQuery(_readQuery!, 1); | ||
await query.GetNextAsJsonAsync().ConfigureAwait(false); | ||
} | ||
|
||
private async Task ExecuteRegistryWriteCheckAsync(CancellationToken cancellationToken) | ||
{ | ||
string deviceId = _writeDeviceId!; | ||
var device = await _registryManager.GetDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false); | ||
|
||
// in default implementation of configuration deviceId equals "health-check-registry-write-device-id" | ||
// if in previous health check device were not removed -- try remove it | ||
// if in previous health check device were added and removed -- try create and remove it | ||
if (device != null) | ||
{ | ||
await _registryManager.RemoveDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await _registryManager.AddDeviceAsync(new Device(deviceId), cancellationToken).ConfigureAwait(false); | ||
await _registryManager.RemoveDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/HealthChecks.Azure.IoTHub/IoTHubServiceClientHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.Azure.Devices; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.Azure.IoTHub; | ||
|
||
public sealed class IoTHubServiceClientHealthCheck : IHealthCheck | ||
{ | ||
private readonly ServiceClient _serviceClient; | ||
|
||
public IoTHubServiceClientHealthCheck(ServiceClient serviceClient) | ||
{ | ||
_serviceClient = Guard.ThrowIfNull(serviceClient); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await _serviceClient.GetServiceStatisticsAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
return HealthCheckResult.Healthy(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.