Skip to content

Commit 372aa89

Browse files
rajkumar-rangarajTimothy Mothra
andauthored
[AzureMonitorExporter/Distro] Add sampler support to UseAzureMonitorExporter / Reintroduce sampler support in UseAzureMonitor distro. (Azure#48923)
* Add sampler support to UseAzureMonitorExporter / Reintroduce sampler support in UseAzureMonitor distro. * Fix failing tests * test update --------- Co-authored-by: Timothy Mothra <[email protected]>
1 parent a4c4b69 commit 372aa89

File tree

7 files changed

+49
-68
lines changed

7 files changed

+49
-68
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ internal void SetValueToExporterOptions(AzureMonitorExporterOptions exporterOpti
7777
exporterOptions.DisableOfflineStorage = DisableOfflineStorage;
7878
exporterOptions.SamplingRatio = SamplingRatio;
7979
exporterOptions.StorageDirectory = StorageDirectory;
80+
exporterOptions.EnableLiveMetrics = EnableLiveMetrics;
8081
if (Transport != null)
8182
{
8283
exporterOptions.Transport = Transport;

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

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
using System.Reflection;
55
using Azure.Monitor.OpenTelemetry.AspNetCore.Internals.AzureSdkCompat;
6-
using Azure.Monitor.OpenTelemetry.LiveMetrics.Internals;
76
using Azure.Monitor.OpenTelemetry.AspNetCore.Internals.Profiling;
8-
using Azure.Monitor.OpenTelemetry.LiveMetrics;
97
using Azure.Monitor.OpenTelemetry.Exporter;
108
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
119
using Microsoft.Extensions.Configuration;
@@ -41,10 +39,8 @@ public static class OpenTelemetryBuilderExtensions
4139
/// parsed state values.
4240
/// </para>
4341
///
44-
/// <para>The following vendored instrumentations are added for distributed tracing:</para>
42+
/// <para>The following vendored instrumentation is added for distributed tracing:</para>
4543
/// <list type="bullet">
46-
/// <item>ASP.NET Core.</item>
47-
/// <item>HTTP Client.</item>
4844
/// <item>SQL Client.</item>
4945
/// </list>
5046
/// </remarks>
@@ -68,10 +64,8 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
6864
/// parsed state values.
6965
/// </para>
7066
///
71-
/// <para>The following vendored instrumentations are added for distributed tracing:</para>
67+
/// <para>The following vendored instrumentation is added for distributed tracing:</para>
7268
/// <list type="bullet">
73-
/// <item>ASP.NET Core.</item>
74-
/// <item>HTTP Client.</item>
7569
/// <item>SQL Client.</item>
7670
/// </list>
7771
/// </remarks>
@@ -113,50 +107,9 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
113107
}
114108
return true;
115109
})
116-
.AddProcessor<ProfilingSessionTraceProcessor>()
117-
.AddAzureMonitorTraceExporter());
110+
.AddProcessor<ProfilingSessionTraceProcessor>());
118111

119-
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
120-
{
121-
var azureMonitorOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOptions>>().Get(Options.DefaultName);
122-
if (azureMonitorOptions.EnableLiveMetrics)
123-
{
124-
// TODO: THIS WILL COME FROM THE EXPORTER
125-
126-
//var manager = sp.GetRequiredService<LiveMetricsClientManager>();
127-
//builder.AddProcessor(new LiveMetricsActivityProcessor(manager));
128-
}
129-
});
130-
131-
builder.WithMetrics(b => b
132-
.AddHttpClientAndServerMetrics()
133-
.AddAzureMonitorMetricExporter());
134-
135-
builder.WithLogging(
136-
logging => logging.AddProcessor(sp =>
137-
{
138-
var azureMonitorOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOptions>>().CurrentValue;
139-
var azureMonitorExporterOptions = new AzureMonitorExporterOptions();
140-
azureMonitorOptions.SetValueToExporterOptions(azureMonitorExporterOptions);
141-
142-
var exporter = new AzureMonitorLogExporter(azureMonitorExporterOptions);
143-
144-
if (azureMonitorOptions.EnableLiveMetrics)
145-
{
146-
// TODO: THIS WILL COME FROM THE EXPORTER
147-
148-
//var manager = sp.GetRequiredService<LiveMetricsClientManager>();
149-
150-
//return new CompositeProcessor<LogRecord>(new BaseProcessor<LogRecord>[]
151-
//{
152-
// new LiveMetricsLogProcessor(manager),
153-
// new BatchLogRecordExportProcessor(exporter)
154-
//});
155-
}
156-
157-
return new BatchLogRecordExportProcessor(exporter);
158-
}),
159-
options => options.IncludeFormattedMessage = true);
112+
builder.WithMetrics(b => b.AddHttpClientAndServerMetrics());
160113

161114
// Register a configuration action so that when
162115
// AzureMonitorExporterOptions is requested it is populated from
@@ -184,19 +137,6 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
184137
return new AzureEventSourceLogForwarder(loggerFactory, loggerFilterOptions);
185138
});
186139

187-
// Register Manager as a singleton
188-
189-
// TODO: THIS WILL COME FROM THE EXPORTER
190-
191-
//builder.Services.AddSingleton<LiveMetricsClientManager>(sp =>
192-
//{
193-
// AzureMonitorOptions azureMonitorOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorOptions>>().Get(Options.DefaultName);
194-
// var azureMonitorLiveMetricsOptions = new AzureMonitorLiveMetricsOptions();
195-
// azureMonitorOptions.SetValueToLiveMetricsOptions(azureMonitorLiveMetricsOptions);
196-
197-
// return new LiveMetricsClientManager(azureMonitorLiveMetricsOptions, new DefaultPlatformDistro());
198-
//});
199-
200140
builder.Services.AddOptions<AzureMonitorOptions>()
201141
.Configure<IConfiguration>((options, config) =>
202142
{
@@ -222,6 +162,8 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
222162
}
223163
});
224164

165+
builder.UseAzureMonitorExporter();
166+
225167
return builder;
226168
}
227169

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Integration.Tests/DistroWebAppLiveTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public async Task VerifySendingToTwoResources_UsingExporter()
181181

182182
[RecordedTest]
183183
[SyncOnly] // This test cannot run concurrently with another test because OTel instruments the process and will cause side effects.
184+
[Ignore("TODO: Repurpose this test to validate the error.")]
184185
public async Task VerifySendingToTwoResources_UsingDistroWithExporter()
185186
{
186187
// SETUP WEBAPPLICATION WITH OPENTELEMETRY

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/AzureSdkLoggingTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public async Task PublicLogForwarderIsAdded(LogLevel eventLevel, string expected
8181

8282
using var serviceProvider = serviceCollection.BuildServiceProvider();
8383

84+
await StartHostedServicesAsync(serviceProvider);
85+
8486
var logForwarder = serviceProvider.GetRequiredService<Microsoft.Extensions.Azure.AzureEventSourceLogForwarder>();
8587
Assert.NotNull(logForwarder);
8688
logForwarder.Start();

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2ETests/HttpClientInstrumentationTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Threading.Tasks;
1212
using Azure.Monitor.OpenTelemetry.Exporter.Models;
1313
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Hosting;
1415
using OpenTelemetry.Instrumentation.Http;
1516
using OpenTelemetry.Resources;
1617
using OpenTelemetry.Tests;
@@ -75,6 +76,8 @@ public async Task HttpRequestsAreCapturedCorrectly(string? queryString, int expe
7576
});
7677
using var serviceProvider = serviceCollection.BuildServiceProvider();
7778

79+
await StartHostedServicesAsync(serviceProvider);
80+
7881
// We must resolve the TracerProvider here to ensure that it is initialized.
7982
// In a normal app, the OpenTelemetry.Extensions.Hosting package would handle this.
8083
var tracerProvider = serviceProvider.GetRequiredService<TracerProvider>();
@@ -85,13 +88,12 @@ public async Task HttpRequestsAreCapturedCorrectly(string? queryString, int expe
8588
? path
8689
: path + queryString;
8790

88-
// TODO: THIS NEEDS TO BE INVETIGATED. DISTRO SHOULD BE DISABLING REDACTION.
8991
string expectedQueryString = queryString is null
9092
? string.Empty
9193
#if NET9_0_OR_GREATER //Starting with .NET 9, HttpClient library performs redaction by default
9294
: "?*";
9395
#else // For all older frameworks, the Instrumentation Library performs redaction by default
94-
: "?key=Redacted";
96+
: queryString;
9597
#endif
9698

9799
string urlForValidation = path + expectedQueryString;
@@ -211,5 +213,14 @@ internal static void VerifyTelemetryItem(
211213
Assert.Contains(remoteDependencyData.Properties, kvp => kvp.Key == "enrichedOnException" && kvp.Value == "yes");
212214
}
213215
}
216+
217+
private static async Task StartHostedServicesAsync(ServiceProvider serviceProvider)
218+
{
219+
var hostedServices = serviceProvider.GetServices<IHostedService>();
220+
foreach (var hostedService in hostedServices)
221+
{
222+
await hostedService.StartAsync(CancellationToken.None);
223+
}
224+
}
214225
}
215226
}

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/tests/Azure.Monitor.OpenTelemetry.AspNetCore.Tests/E2ETests/SqlClientInstrumentationTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
using System.Data;
1212
using System.Diagnostics;
1313
using System.Linq;
14+
using System.Threading;
15+
using System.Threading.Tasks;
1416
using Azure.Monitor.OpenTelemetry.Exporter.Models;
1517
using Azure.Monitor.OpenTelemetry.Exporter.Tests.CommonTestFramework;
1618
using Microsoft.Data.SqlClient;
1719
using Microsoft.Extensions.DependencyInjection;
20+
using Microsoft.Extensions.Hosting;
1821
using OpenTelemetry.Instrumentation.SqlClient;
1922
using OpenTelemetry.Resources;
2023
using OpenTelemetry.Trace;
@@ -51,7 +54,7 @@ public SqlClientInstrumentationTests(ITestOutputHelper output)
5154
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftWriteCommandError)]
5255
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftWriteCommandError, false)]
5356
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftWriteCommandError, false, true)]
54-
public void SqlClientErrorsAreCollectedSuccessfully(
57+
public async Task SqlClientErrorsAreCollectedSuccessfully(
5558
string beforeCommand,
5659
string errorCommand,
5760
bool shouldEnrich = true,
@@ -83,6 +86,8 @@ public void SqlClientErrorsAreCollectedSuccessfully(
8386
});
8487
using var serviceProvider = serviceCollection.BuildServiceProvider();
8588

89+
await StartHostedServicesAsync(serviceProvider);
90+
8691
// We must resolve the TracerProvider here to ensure that it is initialized.
8792
// In a normal app, the OpenTelemetry.Extensions.Hosting package would handle this.
8893
var tracerProvider = serviceProvider.GetRequiredService<TracerProvider>();
@@ -146,7 +151,7 @@ public void SqlClientErrorsAreCollectedSuccessfully(
146151
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftAfterExecuteCommand, CommandType.StoredProcedure, "SP_GetOrders", false, true, false)]
147152
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftAfterExecuteCommand, CommandType.Text, "select * from sys.databases", false, true)]
148153
[InlineData(SqlMicrosoftBeforeExecuteCommand, SqlMicrosoftAfterExecuteCommand, CommandType.Text, "select * from sys.databases", false, true, false)]
149-
public void SqlClientCallsAreCollectedSuccessfully(
154+
public async Task SqlClientCallsAreCollectedSuccessfully(
150155
string beforeCommand,
151156
string afterCommand,
152157
CommandType commandType,
@@ -182,6 +187,8 @@ public void SqlClientCallsAreCollectedSuccessfully(
182187
});
183188
using var serviceProvider = serviceCollection.BuildServiceProvider();
184189

190+
await StartHostedServicesAsync(serviceProvider);
191+
185192
// We must resolve the TracerProvider here to ensure that it is initialized.
186193
// In a normal app, the OpenTelemetry.Extensions.Hosting package would handle this.
187194
var tracerProvider = serviceProvider.GetRequiredService<TracerProvider>();
@@ -298,6 +305,15 @@ internal static void VerifyTelemetryItem(
298305
}
299306
}
300307

308+
private static async Task StartHostedServicesAsync(ServiceProvider serviceProvider)
309+
{
310+
var hostedServices = serviceProvider.GetServices<IHostedService>();
311+
foreach (var hostedService in hostedServices)
312+
{
313+
await hostedService.StartAsync(CancellationToken.None);
314+
}
315+
}
316+
301317
private class FakeSqlClientDiagnosticSource : IDisposable
302318
{
303319
private readonly DiagnosticListener listener;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using OpenTelemetry;
1313
using OpenTelemetry.Logs;
1414
using System;
15+
using OpenTelemetry.Trace;
16+
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
1517
using OpenTelemetry.Metrics;
1618

1719
namespace Azure.Monitor.OpenTelemetry.Exporter
@@ -73,6 +75,12 @@ public static IOpenTelemetryBuilder UseAzureMonitorExporter(this IOpenTelemetryB
7375
.WithMetrics()
7476
.WithTracing();
7577

78+
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, tracerProviderBuilder) =>
79+
{
80+
var exporterOptions = sp.GetRequiredService<IOptionsMonitor<AzureMonitorExporterOptions>>().Get(Options.DefaultName);
81+
tracerProviderBuilder.SetSampler(new ApplicationInsightsSampler(exporterOptions.SamplingRatio));
82+
});
83+
7684
builder.Services.Configure<OpenTelemetryLoggerOptions>((loggingOptions) =>
7785
{
7886
loggingOptions.IncludeFormattedMessage = true;

0 commit comments

Comments
 (0)