Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nullable annotations for Consul/CosmosDb/DocumentDb/DynamoDb #932

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions samples/HealthChecks.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HealthChecks.UI.Client;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -64,8 +64,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
.UseEndpoints(config => config.MapDefaultControllerRoute());
}

public class RandomHealthCheck
: IHealthCheck
public class RandomHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
Expand Down
5 changes: 2 additions & 3 deletions samples/HealthChecks.UIAndApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HealthChecks.UI.Client;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -88,8 +88,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}
}

public class RandomHealthCheck
: IHealthCheck
public class RandomHealthCheck : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
Expand Down
3 changes: 1 addition & 2 deletions src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace HealthChecks.AzureServiceBus
{
public class AzureEventHubHealthCheck
: IHealthCheck
public class AzureEventHubHealthCheck : IHealthCheck
{
private const string ENTITY_PATH_SEGMENT = "EntityPath=";

Expand Down
3 changes: 1 addition & 2 deletions src/HealthChecks.AzureStorage/AzureBlobStorageHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace HealthChecks.AzureStorage
{
public class AzureBlobStorageHealthCheck
: IHealthCheck
public class AzureBlobStorageHealthCheck : IHealthCheck
{
private readonly TokenCredential? _azureCredential;
private readonly Uri? _blobServiceUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace HealthChecks.AzureStorage
{
public class AzureQueueStorageHealthCheck
: IHealthCheck
public class AzureQueueStorageHealthCheck : IHealthCheck
{
private readonly string? _connectionString;
private readonly string? _queueName;
Expand Down
14 changes: 11 additions & 3 deletions src/HealthChecks.Consul/ConsulOptions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
namespace HealthChecks.Consul
{
/// <summary>
/// Options for <see cref="ConsulHealthCheck"/>.
/// </summary>
public class ConsulOptions
{
public string HostName { get; set; }
public string HostName { get; set; } = null!;

public int Port { get; set; }

public bool RequireHttps { get; set; }

public bool RequireBasicAuthentication { get; set; }
public string Username { get; set; }
public string Password { get; set; }

public string? Username { get; set; }

public string? Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods to configure <see cref="ConsulHealthCheck"/>.
/// </summary>
public static class ConsulHealthCheckBuilderExtensions
{
private const string NAME = "consul";
Expand All @@ -24,7 +27,13 @@ public static class ConsulHealthCheckBuilderExtensions
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddConsul(this IHealthChecksBuilder builder, Action<ConsulOptions> setup, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
public static IHealthChecksBuilder AddConsul(
this IHealthChecksBuilder builder,
Action<ConsulOptions>? setup,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
builder.Services.AddHttpClient();

Expand All @@ -37,7 +46,7 @@ public static IHealthChecksBuilder AddConsul(this IHealthChecksBuilder builder,
timeout));
}

private static ConsulHealthCheck CreateHealthCheck(IServiceProvider sp, Action<ConsulOptions> setup, string name)
private static ConsulHealthCheck CreateHealthCheck(IServiceProvider sp, Action<ConsulOptions>? setup, string name)
{
var options = new ConsulOptions();
setup?.Invoke(options);
Expand Down
3 changes: 2 additions & 1 deletion src/HealthChecks.Consul/HealthChecks.Consul.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetFrameworkVersion);$(NetStandardVersion)</TargetFrameworks>
<PackageTags>HealthCheck;Consul</PackageTags>
<Description>HealthChecks.Consul is the health check package for Consul Server.</Description>
<Version>$(HealthCheckConsul)</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 7 additions & 6 deletions src/HealthChecks.CosmosDb/CosmosDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@

namespace HealthChecks.CosmosDb
{
public class CosmosDbHealthCheck
: IHealthCheck
public class CosmosDbHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<string, CosmosClient> _connections = new();

private readonly string _connectionString;
private readonly string _database;
private readonly IEnumerable<string> _containers;
private readonly string? _database;
private readonly IEnumerable<string>? _containers;

public CosmosDbHealthCheck(string connectionString)
: this(connectionString, default, default) { }
: this(connectionString, default, default)
{
}

public CosmosDbHealthCheck(string connectionString, string database)
: this(connectionString, database, default)
{
_database = database;
}

public CosmosDbHealthCheck(string connectionString, string database, IEnumerable<string> containers)
public CosmosDbHealthCheck(string connectionString, string? database, IEnumerable<string>? containers)
{
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_database = database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Microsoft.Extensions.DependencyInjection
{
{
/// <summary>
/// Extension methods to configure <see cref="CosmosDbHealthCheck"/> and <see cref="TableServiceHealthCheck"/>.
/// </summary>
public static class CosmosDbHealthCheckBuilderExtensions
{
private const string COSMOS_NAME = "cosmosdb";
Expand All @@ -29,10 +32,10 @@ public static class CosmosDbHealthCheckBuilderExtensions
public static IHealthChecksBuilder AddCosmosDb(
this IHealthChecksBuilder builder,
string connectionString,
string database = default,
string name = default,
string? database = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string> tags = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
Expand Down Expand Up @@ -61,11 +64,11 @@ public static IHealthChecksBuilder AddCosmosDb(
public static IHealthChecksBuilder AddCosmosDbCollection(
this IHealthChecksBuilder builder,
string connectionString,
string database = default,
IEnumerable<string> collections = default,
string name = default,
string? database = default,
IEnumerable<string>? collections = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string> tags = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
Expand Down Expand Up @@ -94,9 +97,9 @@ public static IHealthChecksBuilder AddAzureTable(
this IHealthChecksBuilder builder,
string connectionString,
string tableName,
string name = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string> tags = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
Expand Down Expand Up @@ -127,9 +130,9 @@ public static IHealthChecksBuilder AddAzureTable(
Uri endpoint,
TableSharedKeyCredential credentials,
string tableName,
string name = default,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string> tags = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
return builder.Add(new HealthCheckRegistration(
Expand Down
3 changes: 2 additions & 1 deletion src/HealthChecks.CosmosDb/HealthChecks.CosmosDb.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetFrameworkVersion);$(NetStandardVersion)</TargetFrameworks>
<PackageTags>HealthCheck;Azure;CosmosDb</PackageTags>
<Description>HealthChecks.CosmosDb is the health check package for Azure CosmosDb.</Description>
<Version>$(HealthCheckCosmosDb)</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 9 additions & 14 deletions src/HealthChecks.CosmosDb/TableServiceHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@

namespace HealthChecks.CosmosDb
{
public class TableServiceHealthCheck
: IHealthCheck
public class TableServiceHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<string, TableServiceClient> _connections = new();

private readonly string _connectionString;
private readonly string? _connectionString;
private readonly string _tableName;

private readonly Uri _endpoint;
private readonly TableSharedKeyCredential _credentials;
private readonly Uri? _endpoint;
private readonly TableSharedKeyCredential? _credentials;

public TableServiceHealthCheck(string connectionString, string tableName)
{
Expand All @@ -36,15 +35,14 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{

var tableServiceKey = _connectionString ?? _endpoint.ToString();
var tableServiceKey = _connectionString ?? _endpoint!.ToString();
if (!_connections.TryGetValue(tableServiceKey, out var tableServiceClient))
{
tableServiceClient = CreateTableServiceClient();

if (!_connections.TryAdd(tableServiceKey, tableServiceClient))
{
tableServiceClient = _connections[_connectionString];
tableServiceClient = _connections[tableServiceKey];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bugfix, wrong name

}
}
var tableClient = tableServiceClient.GetTableClient(_tableName);
Expand All @@ -64,12 +62,9 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context

private TableServiceClient CreateTableServiceClient()
{
if (!string.IsNullOrEmpty(_connectionString))
{
return new TableServiceClient(_connectionString);
}

return new TableServiceClient(_endpoint, _credentials);
return !string.IsNullOrEmpty(_connectionString)
? new TableServiceClient(_connectionString)
: new TableServiceClient(_endpoint, _credentials);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods to configure <see cref="DocumentDbHealthCheck"/>.
/// </summary>
public static class DocumentDbHealthCheckBuilderExtensions
{
private const string NAME = "documentdb";
Expand All @@ -22,7 +25,13 @@ public static class DocumentDbHealthCheckBuilderExtensions
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddDocumentDb(this IHealthChecksBuilder builder, Action<DocumentDbOptions> setup, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
public static IHealthChecksBuilder AddDocumentDb(
this IHealthChecksBuilder builder,
Action<DocumentDbOptions>? setup,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
var documentDbOptions = new DocumentDbOptions();
setup?.Invoke(documentDbOptions);
Expand Down
4 changes: 1 addition & 3 deletions src/HealthChecks.DocumentDb/DocumentDbHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

namespace HealthChecks.DocumentDb
{
public class DocumentDbHealthCheck
: IHealthCheck
public class DocumentDbHealthCheck : IHealthCheck
{
private static readonly ConcurrentDictionary<string, DocumentClient> _connections = new();
private readonly DocumentDbOptions _documentDbOptions = new();
Expand All @@ -23,7 +22,6 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{

if (!_connections.TryGetValue(_documentDbOptions.UriEndpoint, out var documentDbClient))
{
documentDbClient = new DocumentClient(new Uri(_documentDbOptions.UriEndpoint), _documentDbOptions.PrimaryKey);
Expand Down
8 changes: 6 additions & 2 deletions src/HealthChecks.DocumentDb/DocumentDbOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace HealthChecks.DocumentDb
{
/// <summary>
/// Options for <see cref="DocumentDbHealthCheck"/>.
/// </summary>
public class DocumentDbOptions
{
public string UriEndpoint { get; set; }
public string PrimaryKey { get; set; }
public string UriEndpoint { get; set; } = null!;

public string PrimaryKey { get; set; } = null!;
}
}
3 changes: 2 additions & 1 deletion src/HealthChecks.DocumentDb/HealthChecks.DocumentDb.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetFrameworkVersion);$(NetStandardVersion)</TargetFrameworks>
<PackageTags>HealthCheck;Azure;DocumentDb</PackageTags>
<Description>HealthChecks.DocumentDb is the health check package for Azure DocumentDb.</Description>
<Version>$(HealthCheckDocumentDb)</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods to configure <see cref="DynamoDbHealthCheck"/>.
/// </summary>
public static class DynamoDbHealthCheckBuilderExtensions
{
private const string NAME = "dynamodb";
Expand All @@ -22,7 +25,13 @@ public static class DynamoDbHealthCheckBuilderExtensions
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddDynamoDb(this IHealthChecksBuilder builder, Action<DynamoDBOptions> setup, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default, TimeSpan? timeout = default)
public static IHealthChecksBuilder AddDynamoDb(
this IHealthChecksBuilder builder,
Action<DynamoDBOptions>? setup,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
var options = new DynamoDBOptions();
setup?.Invoke(options);
Expand Down
Loading