Skip to content

Commit 7e36acc

Browse files
author
Timothy Mothra
authored
[AzureMonitorExporter] api feedback (Azure#37996)
* merge Extensions classes * update Options class * changelog * update public api * remove nullables from public api * changelog * changelog * export public api * reset enum integer
1 parent 7b97ef7 commit 7e36acc

File tree

8 files changed

+207
-238
lines changed

8 files changed

+207
-238
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/AzureMonitorOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AzureMonitorOptions : ClientOptions
1717
/// The Connection String provides users with a single configuration setting to identify the Azure Monitor resource and endpoint.
1818
/// </summary>
1919
/// <remarks>
20-
/// (https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string).
20+
/// <see href="https://docs.microsoft.com/azure/azure-monitor/app/sdk-connection-string"/>.
2121
/// </remarks>
2222
public string ConnectionString { get; set; }
2323

@@ -27,7 +27,7 @@ public class AzureMonitorOptions : ClientOptions
2727
/// and Instrumentation Key from the Connection String will be used.
2828
/// </summary>
2929
/// <remarks>
30-
/// https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net#is-the-connection-string-a-secret
30+
/// <see href="https://learn.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string?tabs=net#is-the-connection-string-a-secret"/>.
3131
/// </remarks>
3232
public TokenCredential Credential { get; set; }
3333

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* Location ip on server spans will now be set using `client.address` tag key on
1010
activity instead of `http.client_ip`.
1111
([#37707](https://github.com/Azure/azure-sdk-for-net/pull/37707))
12+
* Removing `ServiceVersion.V2020_09_15_Preview`. This is no longer in use and
13+
the exporter has already defaulted to the latest `ServiceVersion.v2_1`.
14+
([#37996](https://github.com/Azure/azure-sdk-for-net/pull/37996))
15+
* Remove Nullable Annotations from the Exporter's public API.
16+
([#37996](https://github.com/Azure/azure-sdk-for-net/pull/37996))
1217

1318
### Bugs Fixed
1419

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
namespace Azure.Monitor.OpenTelemetry.Exporter
22
{
3-
public static partial class AzureMonitorExporterLoggingExtensions
3+
public static partial class AzureMonitorExporterExtensions
44
{
5-
public static OpenTelemetry.Logs.OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null) { throw null; }
6-
}
7-
public static partial class AzureMonitorExporterMetricExtensions
8-
{
9-
public static OpenTelemetry.Metrics.MeterProviderBuilder AddAzureMonitorMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null, string? name = null) { throw null; }
5+
public static OpenTelemetry.Logs.OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions loggerOptions, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null) { throw null; }
6+
public static OpenTelemetry.Metrics.MeterProviderBuilder AddAzureMonitorMetricExporter(this OpenTelemetry.Metrics.MeterProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null, string name = null) { throw null; }
7+
public static OpenTelemetry.Trace.TracerProviderBuilder AddAzureMonitorTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions> configure = null, Azure.Core.TokenCredential credential = null, string name = null) { throw null; }
108
}
119
public partial class AzureMonitorExporterOptions : Azure.Core.ClientOptions
1210
{
1311
public AzureMonitorExporterOptions() { }
1412
public AzureMonitorExporterOptions(Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion version = Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion.v2_1) { }
15-
public string? ConnectionString { get { throw null; } set { } }
16-
public Azure.Core.TokenCredential? Credential { get { throw null; } set { } }
13+
public string ConnectionString { get { throw null; } set { } }
14+
public Azure.Core.TokenCredential Credential { get { throw null; } set { } }
1715
public bool DisableOfflineStorage { get { throw null; } set { } }
1816
public float SamplingRatio { get { throw null; } set { } }
19-
public string? StorageDirectory { get { throw null; } set { } }
17+
public string StorageDirectory { get { throw null; } set { } }
2018
public Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion Version { get { throw null; } set { } }
2119
public enum ServiceVersion
2220
{
23-
V2020_09_15_Preview = 1,
24-
v2_1 = 2,
21+
v2_1 = 1,
2522
}
2623
}
27-
public static partial class AzureMonitorExporterTraceExtensions
28-
{
29-
public static OpenTelemetry.Trace.TracerProviderBuilder AddAzureMonitorTraceExporter(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions>? configure = null, Azure.Core.TokenCredential? credential = null, string? name = null) { throw null; }
30-
}
3124
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#nullable disable
5+
6+
using System;
7+
using System.Diagnostics;
8+
using Azure.Core;
9+
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Options;
12+
using OpenTelemetry;
13+
using OpenTelemetry.Logs;
14+
using OpenTelemetry.Metrics;
15+
using OpenTelemetry.Trace;
16+
17+
namespace Azure.Monitor.OpenTelemetry.Exporter
18+
{
19+
/// <summary>
20+
/// Extension methods to simplify registering of Azure Monitor Exporter for all signals.
21+
/// </summary>
22+
public static class AzureMonitorExporterExtensions
23+
{
24+
/// <summary>
25+
/// Adds Azure Monitor Trace exporter to the TracerProvider.
26+
/// </summary>
27+
/// <param name="builder"><see cref="TracerProviderBuilder"/> builder to use.</param>
28+
/// <param name="configure">Callback action for configuring <see cref="AzureMonitorExporterOptions"/>.</param>
29+
/// <param name="credential">
30+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
31+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
32+
/// the Options will take precedence.
33+
/// </param>
34+
/// <param name="name">Name which is used when retrieving options.</param>
35+
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
36+
public static TracerProviderBuilder AddAzureMonitorTraceExporter(
37+
this TracerProviderBuilder builder,
38+
Action<AzureMonitorExporterOptions> configure = null,
39+
TokenCredential credential = null,
40+
string name = null)
41+
{
42+
if (builder == null)
43+
{
44+
throw new ArgumentNullException(nameof(builder));
45+
}
46+
47+
var finalOptionsName = name ?? Options.DefaultName;
48+
49+
if (name != null && configure != null)
50+
{
51+
// If we are using named options we register the
52+
// configuration delegate into options pipeline.
53+
builder.ConfigureServices(services => services.Configure(finalOptionsName, configure));
54+
}
55+
56+
var deferredBuilder = builder as IDeferredTracerProviderBuilder;
57+
if (deferredBuilder == null)
58+
{
59+
throw new InvalidOperationException("The provided TracerProviderBuilder does not implement IDeferredTracerProviderBuilder.");
60+
}
61+
62+
return deferredBuilder.Configure((sp, builder) =>
63+
{
64+
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName);
65+
if (name == null && configure != null)
66+
{
67+
// If we are NOT using named options, we execute the
68+
// configuration delegate inline. The reason for this is
69+
// AzureMonitorExporterOptions is shared by all signals. Without a
70+
// name, delegates for all signals will mix together. See:
71+
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
72+
configure(exporterOptions);
73+
}
74+
75+
builder.SetSampler(new ApplicationInsightsSampler(exporterOptions.SamplingRatio));
76+
77+
if (credential != null)
78+
{
79+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
80+
// Options should take precedence.
81+
exporterOptions.Credential ??= credential;
82+
}
83+
84+
builder.AddProcessor(new CompositeProcessor<Activity>(new BaseProcessor<Activity>[]
85+
{
86+
new StandardMetricsExtractionProcessor(new AzureMonitorMetricExporter(exporterOptions)),
87+
new BatchActivityExportProcessor(new AzureMonitorTraceExporter(exporterOptions))
88+
}));
89+
});
90+
}
91+
92+
/// <summary>
93+
/// Adds Azure Monitor Metric exporter.
94+
/// </summary>
95+
/// <param name="builder"><see cref="MeterProviderBuilder"/> builder to use.</param>
96+
/// <param name="configure">Exporter configuration options.</param>
97+
/// <param name="credential">
98+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
99+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
100+
/// the Options will take precedence.
101+
/// </param>
102+
/// <param name="name">Name which is used when retrieving options.</param>
103+
/// <returns>The instance of <see cref="MeterProviderBuilder"/> to chain the calls.</returns>
104+
public static MeterProviderBuilder AddAzureMonitorMetricExporter(
105+
this MeterProviderBuilder builder,
106+
Action<AzureMonitorExporterOptions> configure = null,
107+
TokenCredential credential = null,
108+
string name = null)
109+
{
110+
if (builder == null)
111+
{
112+
throw new ArgumentNullException(nameof(builder));
113+
}
114+
115+
var finalOptionsName = name ?? Options.DefaultName;
116+
117+
if (name != null && configure != null)
118+
{
119+
// If we are using named options we register the
120+
// configuration delegate into options pipeline.
121+
builder.ConfigureServices(services => services.Configure(finalOptionsName, configure));
122+
}
123+
124+
return builder.AddReader(sp =>
125+
{
126+
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(finalOptionsName);
127+
128+
if (name == null && configure != null)
129+
{
130+
// If we are NOT using named options, we execute the
131+
// configuration delegate inline. The reason for this is
132+
// AzureMonitorExporterOptions is shared by all signals. Without a
133+
// name, delegates for all signals will mix together. See:
134+
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/4043
135+
configure(exporterOptions);
136+
}
137+
138+
if (credential != null)
139+
{
140+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
141+
// Options should take precedence.
142+
exporterOptions.Credential ??= credential;
143+
}
144+
145+
return new PeriodicExportingMetricReader(new AzureMonitorMetricExporter(exporterOptions))
146+
{ TemporalityPreference = MetricReaderTemporalityPreference.Delta };
147+
});
148+
}
149+
150+
/// <summary>
151+
/// Adds Azure Monitor Log Exporter with OpenTelemetryLoggerOptions.
152+
/// </summary>
153+
/// <param name="loggerOptions"><see cref="OpenTelemetryLoggerOptions"/> options to use.</param>
154+
/// <param name="configure">Exporter configuration options.</param>
155+
/// <param name="credential">
156+
/// An Azure <see cref="TokenCredential" /> capable of providing an OAuth token.
157+
/// Note: if a credential is provided to both <see cref="AzureMonitorExporterOptions"/> and this parameter,
158+
/// the Options will take precedence.
159+
/// </param>
160+
/// <returns>The instance of <see cref="OpenTelemetryLoggerOptions"/> to chain the calls.</returns>
161+
public static OpenTelemetryLoggerOptions AddAzureMonitorLogExporter(
162+
this OpenTelemetryLoggerOptions loggerOptions,
163+
Action<AzureMonitorExporterOptions> configure = null,
164+
TokenCredential credential = null)
165+
{
166+
if (loggerOptions == null)
167+
{
168+
throw new ArgumentNullException(nameof(loggerOptions));
169+
}
170+
171+
var options = new AzureMonitorExporterOptions();
172+
configure?.Invoke(options);
173+
174+
if (credential != null)
175+
{
176+
// Credential can be set by either AzureMonitorExporterOptions or Extension Method Parameter.
177+
// Options should take precedence.
178+
options.Credential ??= credential;
179+
}
180+
181+
return loggerOptions.AddProcessor(new BatchLogRecordExportProcessor(new AzureMonitorLogExporter(options)));
182+
}
183+
}
184+
}

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

Lines changed: 0 additions & 47 deletions
This file was deleted.

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

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)