From ce73a2e9072f65fb7d025cdec745bf7d35ccd7b4 Mon Sep 17 00:00:00 2001 From: Robin-Manuel Thiel Date: Sun, 10 Dec 2023 17:30:26 +0100 Subject: [PATCH 1/3] Adding Application Insights with an Empty String should be optional but allowed Fixes #90 --- .../Monitoring/MonitoringEnvironment.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs index 58d06ed..4ac1a9d 100644 --- a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs +++ b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs @@ -49,13 +49,41 @@ public MonitoringEnvironment(string serviceName, string serviceNamespace, string ServiceVersion = serviceVersion; } + /// + /// Adds an Application Insights exporter to publish metrics to. + /// Fails, if the Connection String is null or empty. + /// + /// The connection string to export to. + /// The sampling ratio to use. public MonitoringEnvironment WithApplicationInsights(string connectionString, float samplingRatio = 1f) { + if (string.IsNullOrEmpty(connectionString)) + { + throw new ArgumentException("The connection string must not be null or empty.", nameof(connectionString)); + } + ApplicationInsightsConnectionString = connectionString; ApplicationInsightsSamplingRatio = samplingRatio; return this; } + /// + /// Adds an Application Insights exporter to publish metrics to. + /// Skips, if the Connection String is null or empty. + /// + /// The connection string to export to. + /// The sampling ratio to use. + public MonitoringEnvironment WithOptionalApplicationInsights(string? connectionString, float samplingRatio = 1f) + { + if (string.IsNullOrEmpty(connectionString)) + { + ApplicationInsightsConnectionString = connectionString; + ApplicationInsightsSamplingRatio = samplingRatio; + } + + return this; + } + /// /// Adds an OpenTelemetry protocol (OTLP) exporter (e.g. Jaeger) to publish metrics to. /// From 18cb8402e371adf2cb432096b28e466f3cf6e270 Mon Sep 17 00:00:00 2001 From: Robin-Manuel Thiel Date: Sun, 10 Dec 2023 17:32:17 +0100 Subject: [PATCH 2/3] Make UseApplicationInsights more explicit --- src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs index 4ac1a9d..5a2e3e1 100644 --- a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs +++ b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs @@ -12,7 +12,7 @@ public class MonitoringEnvironment public string ServiceVersion { get; } public string ApplicationInsightsConnectionString { get; private set; } public float ApplicationInsightsSamplingRatio { get; private set; } - public bool UseApplicationInsights => !string.IsNullOrEmpty(ApplicationInsightsConnectionString); + public bool UseApplicationInsights { get; private set; } public Uri? OtlpExportEndpoint { get; private set; } public bool UseOtlpExporter => OtlpExportEndpoint != null; public bool UsePrometheus { get; private set; } @@ -64,6 +64,7 @@ public MonitoringEnvironment WithApplicationInsights(string connectionString, fl ApplicationInsightsConnectionString = connectionString; ApplicationInsightsSamplingRatio = samplingRatio; + UseApplicationInsights = true; return this; } @@ -79,6 +80,11 @@ public MonitoringEnvironment WithOptionalApplicationInsights(string? connectionS { ApplicationInsightsConnectionString = connectionString; ApplicationInsightsSamplingRatio = samplingRatio; + UseApplicationInsights = true; + } + else + { + UseApplicationInsights = false; } return this; From 5fb06eff4b3b04fa2d4678a88d374b56a36ac68f Mon Sep 17 00:00:00 2001 From: Robin-Manuel Thiel Date: Sun, 10 Dec 2023 17:35:39 +0100 Subject: [PATCH 3/3] Fix Connection String assignment bug --- src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs index 5a2e3e1..99b9952 100644 --- a/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs +++ b/src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs @@ -76,7 +76,7 @@ public MonitoringEnvironment WithApplicationInsights(string connectionString, fl /// The sampling ratio to use. public MonitoringEnvironment WithOptionalApplicationInsights(string? connectionString, float samplingRatio = 1f) { - if (string.IsNullOrEmpty(connectionString)) + if (!string.IsNullOrEmpty(connectionString)) { ApplicationInsightsConnectionString = connectionString; ApplicationInsightsSamplingRatio = samplingRatio;