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

[FEATURE] Adding Application Insights with an Empty String should be optional but allowed #91

Merged
merged 5 commits into from
Dec 10, 2023
Merged
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
36 changes: 35 additions & 1 deletion src/Wemogy.Core/Monitoring/MonitoringEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -49,10 +49,44 @@ public MonitoringEnvironment(string serviceName, string serviceNamespace, string
ServiceVersion = serviceVersion;
}

/// <summary>
/// Adds an Application Insights exporter to publish metrics to.
/// Fails, if the Connection String is null or empty.
/// </summary>
/// <param name="connectionString">The connection string to export to.</param>
/// <param name="samplingRatio">The sampling ratio to use.</param>
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;
UseApplicationInsights = true;
return this;
}

/// <summary>
/// Adds an Application Insights exporter to publish metrics to.
/// Skips, if the Connection String is null or empty.
/// </summary>
/// <param name="connectionString">The connection string to export to.</param>
/// <param name="samplingRatio">The sampling ratio to use.</param>
public MonitoringEnvironment WithOptionalApplicationInsights(string? connectionString, float samplingRatio = 1f)
{
if (!string.IsNullOrEmpty(connectionString))
{
ApplicationInsightsConnectionString = connectionString;
ApplicationInsightsSamplingRatio = samplingRatio;
UseApplicationInsights = true;
}
else
{
UseApplicationInsights = false;
}

return this;
}

Expand Down
Loading