Skip to content

Commit 80587d6

Browse files
Merge pull request #3088 from microsoft/rajrang/coreStDir
Adds configuration options for trace-based log sampling and offline storage in ASP.NET Core and WorkerService DI scenarios
2 parents a29d1bf + fe13def commit 80587d6

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/Stable/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOp
2222
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.TracesPerSecond.set -> void
2323
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableRequestTrackingTelemetryModule.get -> bool
2424
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableRequestTrackingTelemetryModule.set -> void
25+
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableTraceBasedLogsSampler.get -> bool?
26+
Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions.EnableTraceBasedLogsSampler.set -> void
2527
Microsoft.ApplicationInsights.AspNetCore.IJavaScriptSnippet
2628
Microsoft.ApplicationInsights.AspNetCore.IJavaScriptSnippet.FullScript.get -> string
2729
Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet

.publicApi/Microsoft.ApplicationInsights.WorkerService.dll/Stable/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.Sa
1818
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.SamplingRatio.set -> void
1919
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.TracesPerSecond.get -> double?
2020
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.TracesPerSecond.set -> void
21+
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableTraceBasedLogsSampler.get -> bool?
22+
Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions.EnableTraceBasedLogsSampler.set -> void
2123
Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions
2224
static Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryWorkerService(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection
2325
static Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryWorkerService(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.ApplicationInsights.WorkerService.ApplicationInsightsServiceOptions options) -> Microsoft.Extensions.DependencyInjection.IServiceCollection

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- OpenTelemetry.Resources.Azure 1.15.0-beta.1
1414
- [Added support to disable telemetry via TelemetryConfiguration.DisableTelemetry](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3084)
1515
- [Removed `EnabledAdaptiveSampling` and replaced with `TracesPerSecond` and `SamplingRatio`](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3085)
16+
- [Added `EnableTraceBasedLogsSampler` property to `ApplicationInsightsServiceOptions` for ASP.NET Core and WorkerService packages](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3088)
17+
- [Added support for `StorageDirectory` and `DisableOfflineStorage` from `TelemetryConfiguration` in DI scenarios](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3088)
1618

1719
## Version 3.0.0-beta2
1820
### Added

NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen
198198
{
199199
config["OTEL_SDK_DISABLED"] = "true";
200200
}
201+
202+
if (!string.IsNullOrEmpty(telemetryConfig.StorageDirectory))
203+
{
204+
exporterOptions.StorageDirectory = telemetryConfig.StorageDirectory;
205+
}
206+
207+
if (telemetryConfig.DisableOfflineStorage.HasValue)
208+
{
209+
exporterOptions.DisableOfflineStorage = telemetryConfig.DisableOfflineStorage.Value;
210+
}
211+
212+
if (serviceOptions.EnableTraceBasedLogsSampler.HasValue)
213+
{
214+
exporterOptions.EnableTraceBasedLogsSampler = serviceOptions.EnableTraceBasedLogsSampler.Value;
215+
}
201216

202217
// Copy connection string to Azure Monitor Exporter
203218
if (!string.IsNullOrEmpty(serviceOptions.ConnectionString))

NETCORE/src/Microsoft.ApplicationInsights.WorkerService/ApplicationInsightsExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ internal static IOpenTelemetryBuilder UseApplicationInsightsTelemetry(this IOpen
194194
config["OTEL_SDK_DISABLED"] = "true";
195195
}
196196

197+
if (!string.IsNullOrEmpty(telemetryConfig.StorageDirectory))
198+
{
199+
exporterOptions.StorageDirectory = telemetryConfig.StorageDirectory;
200+
}
201+
202+
if (telemetryConfig.DisableOfflineStorage.HasValue)
203+
{
204+
exporterOptions.DisableOfflineStorage = telemetryConfig.DisableOfflineStorage.Value;
205+
}
206+
207+
if (serviceOptions.EnableTraceBasedLogsSampler.HasValue)
208+
{
209+
exporterOptions.EnableTraceBasedLogsSampler = serviceOptions.EnableTraceBasedLogsSampler.Value;
210+
}
211+
197212
// Copy connection string to Azure Monitor Exporter
198213
if (!string.IsNullOrEmpty(serviceOptions.ConnectionString))
199214
{

NETCORE/src/Shared/Extensions/ApplicationInsightsServiceOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public class ApplicationInsightsServiceOptions
6464
/// </summary>
6565
public float? SamplingRatio { get; set; }
6666

67+
/// <summary>
68+
/// Gets or sets a value indicating whether trace-based log sampling is enabled.
69+
/// When null, the Azure Monitor Exporter default of true is used.
70+
/// </summary>
71+
public bool? EnableTraceBasedLogsSampler { get; set; }
72+
6773
#if AI_ASPNETCORE_WEB
6874
/// <summary>
6975
/// Gets or sets a value indicating whether RequestTrackingTelemetryModule should be enabled.
@@ -101,6 +107,7 @@ internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target)
101107
target.EnableDependencyTrackingTelemetryModule = this.EnableDependencyTrackingTelemetryModule;
102108
target.TracesPerSecond = this.TracesPerSecond;
103109
target.SamplingRatio = this.SamplingRatio;
110+
target.EnableTraceBasedLogsSampler = this.EnableTraceBasedLogsSampler;
104111

105112
#if AI_ASPNETCORE_WEB
106113
target.EnableAuthenticationTrackingJavaScript = this.EnableAuthenticationTrackingJavaScript;

NETCORE/test/Shared/ConfigurationTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,66 @@ public void ExplicitConfigurationTakesPrecedenceOverDefaultConfiguration()
305305
var options = serviceProvider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
306306
Assert.Equal(explicitConnectionString, options.ConnectionString);
307307
}
308+
309+
[Fact]
310+
public void EnableTraceBasedLogsSamplerFlowsToExporterOptions()
311+
{
312+
// ARRANGE
313+
var services = new ServiceCollection();
314+
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
315+
services.AddSingleton<IConfiguration>(config);
316+
317+
// ACT
318+
#if AI_ASPNETCORE_WEB
319+
services.AddApplicationInsightsTelemetry(options =>
320+
#else
321+
services.AddApplicationInsightsTelemetryWorkerService(options =>
322+
#endif
323+
{
324+
options.ConnectionString = TestConnectionString;
325+
options.EnableTraceBasedLogsSampler = false;
326+
});
327+
328+
// VALIDATE
329+
IServiceProvider serviceProvider = services.BuildServiceProvider();
330+
var aiOptions = serviceProvider.GetRequiredService<IOptions<ApplicationInsightsServiceOptions>>().Value;
331+
Assert.False(aiOptions.EnableTraceBasedLogsSampler);
332+
333+
var exporterOptions = serviceProvider.GetRequiredService<IOptions<AzureMonitorExporterOptions>>().Value;
334+
Assert.False(exporterOptions.EnableTraceBasedLogsSampler);
335+
}
336+
337+
[Fact]
338+
public void StorageOptionsFlowFromTelemetryConfigurationToExporterOptions()
339+
{
340+
// ARRANGE
341+
var services = new ServiceCollection();
342+
var config = new ConfigurationBuilder().AddInMemoryCollection().Build();
343+
services.AddSingleton<IConfiguration>(config);
344+
345+
// ACT
346+
#if AI_ASPNETCORE_WEB
347+
services.AddApplicationInsightsTelemetry(options =>
348+
#else
349+
services.AddApplicationInsightsTelemetryWorkerService(options =>
350+
#endif
351+
{
352+
options.ConnectionString = TestConnectionString;
353+
});
354+
355+
// Configure TelemetryConfiguration with storage options
356+
services.Configure<Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration>(tc =>
357+
{
358+
tc.StorageDirectory = @"C:\CustomStorage";
359+
tc.DisableOfflineStorage = true;
360+
});
361+
362+
// VALIDATE
363+
IServiceProvider serviceProvider = services.BuildServiceProvider();
364+
var exporterOptions = serviceProvider.GetRequiredService<IOptions<AzureMonitorExporterOptions>>().Value;
365+
366+
Assert.Equal(@"C:\CustomStorage", exporterOptions.StorageDirectory);
367+
Assert.True(exporterOptions.DisableOfflineStorage);
368+
}
308369
}
309370
}

0 commit comments

Comments
 (0)