-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureBlobStorageHealthCheck.cs
67 lines (54 loc) · 2.85 KB
/
AzureBlobStorageHealthCheck.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Threading;
using System.Threading.Tasks;
using Bet.Extensions.AzureStorage;
using Bet.Extensions.AzureStorage.Options;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Bet.Extensions.HealthChecks.AzureStorage
{
public class AzureBlobStorageHealthCheck : IHealthCheck
{
private readonly IOptionsMonitor<StorageBlobOptions> _storageBlobOptionsMonitor;
private readonly IStorageBlob<StorageBlobOptions> _storageBlob;
private ILogger<AzureBlobStorageHealthCheck> _logger;
public AzureBlobStorageHealthCheck(
IStorageBlob<StorageBlobOptions> storageBlob,
IOptionsMonitor<StorageBlobOptions> storageBlobOptionsMonitor,
ILogger<AzureBlobStorageHealthCheck> logger)
{
_storageBlob = storageBlob ?? throw new ArgumentNullException(nameof(storageBlob));
_storageBlobOptionsMonitor = storageBlobOptionsMonitor ?? throw new ArgumentNullException(nameof(storageBlobOptionsMonitor));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var checkName = context.Registration.Name;
var options = _storageBlobOptionsMonitor.Get(checkName);
var fullCheckName = $"HealthCheck Name:{checkName}; Container Name:{options?.ContainerName}";
try
{
_logger.LogInformation("[HealthCheck][{checkName}] Container Name: {containerName}", checkName, options?.ContainerName);
var container = await _storageBlob.GetNamedContainer(checkName, cancellationToken).Value;
if (container != null)
{
var cloudBlobClient = container.ServiceClient;
var serviceProperties = await cloudBlobClient.GetServicePropertiesAsync(
new BlobRequestOptions(),
operationContext: null,
cancellationToken: cancellationToken);
return new HealthCheckResult(HealthStatus.Healthy, fullCheckName);
}
return new HealthCheckResult(context.Registration.FailureStatus, $"{checkName} failed to get {nameof(CloudStorageAccount)} instance for Container Name: {options?.ContainerName}");
}
catch (Exception ex)
{
_logger.LogError(ex, "[HealthCheck][{checkName}] Container Name: {containerName}", checkName, options?.ContainerName);
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}
}