Skip to content

Commit 7f795f2

Browse files
Refactor AzureMonitorOpenTelemetryOptions (Azure#34550)
1 parent 07f8cce commit 7f795f2

File tree

3 files changed

+28
-38
lines changed

3 files changed

+28
-38
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry/api/Azure.Monitor.OpenTelemetry.netstandard2.0.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ public static partial class AzureMonitorOpenTelemetryExtensions
99
public partial class AzureMonitorOpenTelemetryOptions
1010
{
1111
public AzureMonitorOpenTelemetryOptions() { }
12-
public Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions AzureMonitorExporterOptions { get { throw null; } set { } }
1312
public string ConnectionString { get { throw null; } set { } }
13+
public bool DisableOfflineStorage { get { throw null; } set { } }
1414
public bool EnableLogs { get { throw null; } set { } }
1515
public bool EnableMetrics { get { throw null; } set { } }
1616
public bool EnableTraces { get { throw null; } set { } }
17+
public string StorageDirectory { get { throw null; } set { } }
1718
}
1819
}

sdk/monitor/Azure.Monitor.OpenTelemetry/src/AzureMonitorOpenTelemetryImplementations.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi
7676
}
7777
else
7878
{
79-
SetValueToExporterOptions(sp, options);
79+
options.SetValueToExporterOptions(sp);
8080
var sdkProviderWrapper = sp.GetRequiredService<SdkProviderWrapper>();
8181
sdkProviderWrapper.SdkTracerProvider = (TracerProvider)sdkTracerProviderServiceRegistration.ImplementationFactory(sp);
8282
return sdkProviderWrapper.SdkTracerProvider;
@@ -92,7 +92,7 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi
9292
}
9393
else
9494
{
95-
SetValueToExporterOptions(sp, options);
95+
options.SetValueToExporterOptions(sp);
9696
var sdkProviderWrapper = sp.GetRequiredService<SdkProviderWrapper>();
9797
sdkProviderWrapper.SdkMeterProvider = (MeterProvider)sdkMeterProviderServiceRegistration.ImplementationFactory(sp);
9898
return sdkProviderWrapper.SdkMeterProvider;
@@ -106,23 +106,6 @@ internal static IServiceCollection AddAzureMonitorOpenTelemetryWithAction(IServi
106106
return services;
107107
}
108108

109-
private static void SetValueToExporterOptions(IServiceProvider sp, AzureMonitorOpenTelemetryOptions options)
110-
{
111-
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get("");
112-
var defaultOptions = new AzureMonitorExporterOptions();
113-
114-
if (ReferenceEquals(exporterOptions, defaultOptions))
115-
{
116-
exporterOptions.ConnectionString = options.AzureMonitorExporterOptions.ConnectionString;
117-
exporterOptions.DisableOfflineStorage = options.AzureMonitorExporterOptions.DisableOfflineStorage;
118-
exporterOptions.StorageDirectory = options.AzureMonitorExporterOptions.StorageDirectory;
119-
}
120-
else if (exporterOptions.ConnectionString == null)
121-
{
122-
exporterOptions.ConnectionString = options.AzureMonitorExporterOptions.ConnectionString;
123-
}
124-
}
125-
126109
private sealed class NoopTracerProvider : TracerProvider
127110
{
128111
}

sdk/monitor/Azure.Monitor.OpenTelemetry/src/AzureMonitorOpenTelemetryOptions.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#nullable disable
55

6-
using System.Runtime.Serialization;
76
using Azure.Monitor.OpenTelemetry.Exporter;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Options;
9+
using System;
810

911
namespace Azure.Monitor.OpenTelemetry
1012
{
@@ -13,28 +15,18 @@ namespace Azure.Monitor.OpenTelemetry
1315
/// </summary>
1416
public class AzureMonitorOpenTelemetryOptions
1517
{
16-
/// <summary>
17-
/// Gets or sets a value of Azure Monitor Exporter Options.
18-
/// </summary>
19-
public AzureMonitorExporterOptions AzureMonitorExporterOptions { get; set; } = new AzureMonitorExporterOptions();
20-
2118
/// <summary>
2219
/// The Connection String provides users with a single configuration setting to identify the Azure Monitor resource and endpoint.
2320
/// </summary>
2421
/// <remarks>
2522
/// (https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string).
2623
/// </remarks>
27-
public string ConnectionString
28-
{
29-
get
30-
{
31-
return AzureMonitorExporterOptions.ConnectionString;
32-
}
33-
set
34-
{
35-
AzureMonitorExporterOptions.ConnectionString = value;
36-
}
37-
}
24+
public string ConnectionString { get; set; }
25+
26+
/// <summary>
27+
/// Disable offline storage.
28+
/// </summary>
29+
public bool DisableOfflineStorage { get; set; }
3830

3931
/// <summary>
4032
/// Gets or sets a value indicating whether Logs should be enabled.
@@ -51,18 +43,32 @@ public string ConnectionString
5143
/// </summary>
5244
public bool EnableTraces { get; set; } = true;
5345

46+
/// <summary>
47+
/// Override the default directory for offline storage.
48+
/// </summary>
49+
public string StorageDirectory { get; set; }
50+
5451
internal AzureMonitorOpenTelemetryOptions Clone(AzureMonitorOpenTelemetryOptions options)
5552
{
5653
if (options != null)
5754
{
58-
AzureMonitorExporterOptions = options.AzureMonitorExporterOptions;
5955
ConnectionString = options.ConnectionString;
56+
DisableOfflineStorage = options.DisableOfflineStorage;
6057
EnableLogs = options.EnableLogs;
6158
EnableMetrics = options.EnableMetrics;
6259
EnableTraces = options.EnableTraces;
60+
StorageDirectory = options.StorageDirectory;
6361
}
6462

6563
return this;
6664
}
65+
66+
internal void SetValueToExporterOptions(IServiceProvider sp)
67+
{
68+
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get("");
69+
exporterOptions.ConnectionString = ConnectionString;
70+
exporterOptions.DisableOfflineStorage = DisableOfflineStorage;
71+
exporterOptions.StorageDirectory = StorageDirectory;
72+
}
6773
}
6874
}

0 commit comments

Comments
 (0)