Skip to content

Commit 23bad1b

Browse files
author
Timothy Mothra
authored
refactor tokencredential (Azure#35019)
1 parent 6f7a63b commit 23bad1b

8 files changed

+50
-20
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorExporterLoggingExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public static class AzureMonitorExporterLoggingExtensions
1818
/// </summary>
1919
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
2020
/// <param name="configure">Exporter configuration options.</param>
21-
/// <param name="credential"><see cref="TokenCredential" /></param>
21+
/// <param name="credential">
22+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
23+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
24+
/// the Options will take precedence.
25+
/// </param>
2226
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
2327
public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<AzureMonitorExporterOptions>? configure = null, TokenCredential? credential = null)
2428
{
@@ -34,7 +38,14 @@ public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTel
3438
var options = new AzureMonitorExporterOptions();
3539
configure?.Invoke(options);
3640

37-
return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options, options.Credential ?? credential)));
41+
if (credential != null)
42+
{
43+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
44+
// Options should take precedence.
45+
options.Credential ??= credential;
46+
}
47+
48+
return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options)));
3849
}
3950
}
4051
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorExporterMetricExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ public static class AzureMonitorExporterMetricExtensions
2020
/// </summary>
2121
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
2222
/// <param name="configure">Exporter configuration options.</param>
23-
/// <param name="credential"><see cref="TokenCredential" /></param>
23+
/// <param name="credential">
24+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
25+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
26+
/// the Options will take precedence.
27+
/// </param>
2428
/// <param name="name">Name which is used when retrieving options.</param>
2529
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
2630
public static MeterProviderBuilder AddAzureMonitorMetricExporter(
@@ -59,7 +63,14 @@ public static MeterProviderBuilder AddAzureMonitorMetricExporter(
5963
configure(exporterOptions);
6064
}
6165

62-
return new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(exporterOptions, exporterOptions.Credential ?? credential))
66+
if (credential != null)
67+
{
68+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
69+
// Options should take precedence.
70+
exporterOptions.Credential ??= credential;
71+
}
72+
73+
return new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(exporterOptions))
6374
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta };
6475
});
6576
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorExporterTraceExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public static class AzureMonitorExporterTraceExtensions
2222
/// </summary>
2323
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
2424
/// <param name="configure">Callback action for configuring <see cref="AzureMonitorExporterOptions"/>.</param>
25-
/// <param name="credential"><see cref="TokenCredential" /></param>
25+
/// <param name="credential">
26+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
27+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
28+
/// the Options will take precedence.
29+
/// </param>
2630
/// <param name="name">Name which is used when retrieving options.</param>
2731
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
2832
public static TracerProviderBuilder AddAzureMonitorTraceExporter(
@@ -59,10 +63,17 @@ public static TracerProviderBuilder AddAzureMonitorTraceExporter(
5963
configure(exporterOptions);
6064
}
6165

66+
if (credential != null)
67+
{
68+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
69+
// Options should take precedence.
70+
exporterOptions.Credential ??= credential;
71+
}
72+
6273
return new CompositeProcessor<Activity>(new BaseProcessor<Activity>[]
6374
{
6475
new StandardMetricsExtractionProcessor(),
65-
new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions, exporterOptions.Credential?? credential))
76+
new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions))
6677
});
6778
});
6879
}

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorLogExporter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Threading;
6-
using Azure.Core;
76
using Azure.Core.Pipeline;
87
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
98
using Azure.Monitor.OpenTelemetry.Exporter.Internals.PersistentStorage;
@@ -20,7 +19,7 @@ internal class AzureMonitorLogExporter : BaseExporter<LogRecord>
2019
private AzureMonitorResource? _resource;
2120
private bool _disposed;
2221

23-
public AzureMonitorLogExporter(AzureMonitorExporterOptions options, TokenCredential? credential = null) : this(TransmitterFactory.Instance.Get(options, credential))
22+
public AzureMonitorLogExporter(AzureMonitorExporterOptions options) : this(TransmitterFactory.Instance.Get(options))
2423
{
2524
}
2625

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorMetricExporter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Threading;
6-
using Azure.Core;
76
using Azure.Core.Pipeline;
87
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
98
using Azure.Monitor.OpenTelemetry.Exporter.Internals.PersistentStorage;
@@ -20,7 +19,7 @@ internal class AzureMonitorMetricExporter : BaseExporter<Metric>
2019
private AzureMonitorResource? _resource;
2120
private bool _disposed;
2221

23-
public AzureMonitorMetricExporter(AzureMonitorExporterOptions options, TokenCredential? credential = null) : this(TransmitterFactory.Instance.Get(options, credential))
22+
public AzureMonitorMetricExporter(AzureMonitorExporterOptions options) : this(TransmitterFactory.Instance.Get(options))
2423
{
2524
}
2625

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/AzureMonitorTraceExporter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Diagnostics;
66
using System.Threading;
7-
using Azure.Core;
87
using Azure.Core.Pipeline;
98
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
109
using Azure.Monitor.OpenTelemetry.Exporter.Internals.PersistentStorage;
@@ -20,7 +19,7 @@ internal class AzureMonitorTraceExporter : BaseExporter<Activity>
2019
private AzureMonitorResource? _resource;
2120
private bool _disposed;
2221

23-
public AzureMonitorTraceExporter(AzureMonitorExporterOptions options, TokenCredential? credential = null) : this(TransmitterFactory.Instance.Get(options, credential))
22+
public AzureMonitorTraceExporter(AzureMonitorExporterOptions options) : this(TransmitterFactory.Instance.Get(options))
2423
{
2524
}
2625

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/AzureMonitorTransmitter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class AzureMonitorTransmitter : ITransmitter
3030
private readonly ConnectionVars _connectionVars;
3131
private bool _disposed;
3232

33-
public AzureMonitorTransmitter(AzureMonitorExporterOptions options, TokenCredential? credential = null)
33+
public AzureMonitorTransmitter(AzureMonitorExporterOptions options)
3434
{
3535
if (options == null)
3636
{
@@ -41,7 +41,7 @@ public AzureMonitorTransmitter(AzureMonitorExporterOptions options, TokenCredent
4141

4242
_connectionVars = InitializeConnectionVars(options);
4343

44-
_applicationInsightsRestClient = InitializeRestClient(options, _connectionVars, credential);
44+
_applicationInsightsRestClient = InitializeRestClient(options, _connectionVars);
4545

4646
_fileBlobProvider = InitializeOfflineStorage(options);
4747

@@ -67,21 +67,21 @@ private static ConnectionVars InitializeConnectionVars(AzureMonitorExporterOptio
6767
throw new InvalidOperationException("A connection string was not found. Please set your connection string.");
6868
}
6969

70-
private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorExporterOptions options, ConnectionVars connectionVars, TokenCredential? credential)
70+
private static ApplicationInsightsRestClient InitializeRestClient(AzureMonitorExporterOptions options, ConnectionVars connectionVars)
7171
{
7272
HttpPipeline pipeline;
7373

74-
if (credential != null)
74+
if (options.Credential != null)
7575
{
7676
var scope = AadHelper.GetScope(connectionVars.AadAudience);
7777
var httpPipelinePolicy = new HttpPipelinePolicy[]
7878
{
79-
new BearerTokenAuthenticationPolicy(credential, scope),
79+
new BearerTokenAuthenticationPolicy(options.Credential, scope),
8080
new IngestionRedirectPolicy()
8181
};
8282

8383
pipeline = HttpPipelineBuilder.Build(options, httpPipelinePolicy);
84-
AzureMonitorExporterEventSource.Log.WriteInformational("SetAADCredentialsToPipeline", $"HttpPipelineBuilder is built with AAD Credentials. TokenCredential: {credential.GetType().Name} Scope: {scope}");
84+
AzureMonitorExporterEventSource.Log.WriteInformational("SetAADCredentialsToPipeline", $"HttpPipelineBuilder is built with AAD Credentials. TokenCredential: {options.Credential.GetType().Name} Scope: {scope}");
8585
}
8686
else
8787
{

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitterFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class TransmitterFactory
1919
internal readonly Dictionary<string, AzureMonitorTransmitter> _transmitters = new();
2020
private readonly object _lockObj = new();
2121

22-
public AzureMonitorTransmitter Get(AzureMonitorExporterOptions azureMonitorExporterOptions, TokenCredential? tokenCredential = null)
22+
public AzureMonitorTransmitter Get(AzureMonitorExporterOptions azureMonitorExporterOptions)
2323
{
2424
var key = azureMonitorExporterOptions.ConnectionString ?? string.Empty;
2525

@@ -29,7 +29,7 @@ public AzureMonitorTransmitter Get(AzureMonitorExporterOptions azureMonitorExpor
2929
{
3030
if (!_transmitters.TryGetValue(key, out transmitter))
3131
{
32-
transmitter = new AzureMonitorTransmitter(azureMonitorExporterOptions, tokenCredential);
32+
transmitter = new AzureMonitorTransmitter(azureMonitorExporterOptions);
3333

3434
_transmitters.Add(key, transmitter);
3535
}

0 commit comments

Comments
 (0)