Skip to content

Commit

Permalink
feat(Publisher.ApplicationInsights): add connection string support (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfrk authored Dec 29, 2022
1 parent 634ca9a commit c5af010
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ internal class ApplicationInsightsPublisher : IHealthCheckPublisher
private static TelemetryClient? _client;
private static readonly object _syncRoot = new object();
private readonly TelemetryConfiguration? _telemetryConfiguration;
private readonly string? _connectionString;
private readonly string? _instrumentationKey;
private readonly bool _saveDetailedReport;
private readonly bool _excludeHealthyReports;

public ApplicationInsightsPublisher(
IOptions<TelemetryConfiguration>? telemetryConfiguration,
string? connectionString = default,
string? instrumentationKey = default,
bool saveDetailedReport = false,
bool excludeHealthyReports = false)
{
_telemetryConfiguration = telemetryConfiguration?.Value;
_connectionString = connectionString;
_instrumentationKey = instrumentationKey;
_saveDetailedReport = saveDetailedReport;
_excludeHealthyReports = excludeHealthyReports;
Expand Down Expand Up @@ -110,12 +113,14 @@ private TelemetryClient GetOrCreateTelemetryClient()
{
if (_client == null)
{
//override instrumentation key or use default telemetry
//configuration active on the project.
// Create TelemetryConfiguration
// Hierachy: _connectionString > _instrumentationKey > _telemetryConfiguration
var configuration = string.IsNullOrWhiteSpace(_connectionString)
? string.IsNullOrWhiteSpace(_instrumentationKey)
? _telemetryConfiguration
: new TelemetryConfiguration(_instrumentationKey)
: new TelemetryConfiguration { ConnectionString = _connectionString };

var configuration = string.IsNullOrWhiteSpace(_instrumentationKey)
? _telemetryConfiguration
: new TelemetryConfiguration(_instrumentationKey);

_client = new TelemetryClient(configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public static class ApplicationInsightsHealthCheckBuilderExtensions
/// indicating the health check status (1 - Healthy, 0 - Unhealthy) and the total time the health check took to execute in milliseconds.
/// </remarks>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="instrumentationKey">Specified Application Insights instrumentation key. Optional. If <c>null</c> TelemetryConfiguration.Active is used.</param>
/// <param name="connectionString">Specified Application Insights connection string. Optional. If <c>null</c> <paramref name="instrumentationKey"/> is used.</param>
/// <param name="instrumentationKey">Specified Application Insights instrumentation key. Optional. If <c>null</c> <see cref="TelemetryConfiguration"/> is resolved from DI container.</param>
/// <param name="saveDetailedReport">Specifies if save an Application Insights event for each HealthCheck or just save one event with the global status for all the HealthChecks. Optional</param>
/// <param name="excludeHealthyReports">Specifies if save an Application Insights event only for reports indicating an unhealthy status</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddApplicationInsightsPublisher(
this IHealthChecksBuilder builder,
string? connectionString = default,
string? instrumentationKey = default,
bool saveDetailedReport = false,
bool excludeHealthyReports = false)
Expand All @@ -29,7 +31,7 @@ public static IHealthChecksBuilder AddApplicationInsightsPublisher(
.AddSingleton<IHealthCheckPublisher>(sp =>
{
var telemetryConfigurationOptions = sp.GetService<IOptions<TelemetryConfiguration>>();
return new ApplicationInsightsPublisher(telemetryConfigurationOptions, instrumentationKey, saveDetailedReport, excludeHealthyReports);
return new ApplicationInsightsPublisher(telemetryConfigurationOptions, connectionString, instrumentationKey, saveDetailedReport, excludeHealthyReports);
});

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ public void add_healthcheck_when_properly_configured_with_instrumentation_key_pa
var services = new ServiceCollection();
services
.AddHealthChecks()
.AddApplicationInsightsPublisher("telemetrykey");
.AddApplicationInsightsPublisher(instrumentationKey: "telemetrykey");

using var serviceProvider = services.BuildServiceProvider();
var publisher = serviceProvider.GetService<IHealthCheckPublisher>();

Assert.NotNull(publisher);
}

[Fact]
public void add_healthcheck_when_properly_configured_with_connection_string_parameter()
{
var services = new ServiceCollection();
services
.AddHealthChecks()
.AddApplicationInsightsPublisher(connectionString: "InstrumentationKey=telemetrykey;EndpointSuffix=example.com;");

using var serviceProvider = services.BuildServiceProvider();
var publisher = serviceProvider.GetService<IHealthCheckPublisher>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class ApplicationInsightsHealthCheckBuilderExtensions
{
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddApplicationInsightsPublisher(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string? instrumentationKey = null, bool saveDetailedReport = false, bool excludeHealthyReports = false) { }
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddApplicationInsightsPublisher(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string? connectionString = null, string? instrumentationKey = null, bool saveDetailedReport = false, bool excludeHealthyReports = false) { }
}
}

0 comments on commit c5af010

Please sign in to comment.