Skip to content

Commit 1cfd94f

Browse files
[AzureMonitorExporter] Add ApplicationInsightsSampler to the exporter (Azure#36972)
* Add `ApplicationInsightsSampler` to the exporter * Changelog + public Api * IDeferredTracerProviderBuilder
1 parent 2a0f6ea commit 1cfd94f

17 files changed

+228
-27
lines changed

eng/Packages.Data.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
<!-- OpenTelemetry dependency approved for Azure.Monitor.OpenTelemetry.Exporter package only -->
123123
<PackageReference Update="OpenTelemetry" Version="1.4.0" />
124124
<PackageReference Update="OpenTelemetry.Exporter.InMemory" Version="1.4.0" />
125-
<PackageReference Update="OpenTelemetry.Extensions.AzureMonitor" Version="1.0.0-beta.4" />
126125
<PackageReference Update="OpenTelemetry.Extensions.Hosting" Version="1.4.0" />
127126
<PackageReference Update="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.14" />
128127
<PackageReference Update="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.14" />

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
### Features Added
66

7+
* Added `SamplingRatio` property to customize the sampling rate in Azure Monitor Exporter.
8+
([#36972](https://github.com/Azure/azure-sdk-for-net/pull/36972))
9+
710
### Breaking Changes
811

912
### Bugs Fixed
1013

1114
### Other Changes
1215

16+
* Removed reference to `OpenTelemetry.Extensions.AzureMonitor`.
17+
([#36972](https://github.com/Azure/azure-sdk-for-net/pull/36972))
18+
1319
## 1.0.0-beta.4 (2023-05-09)
1420

1521
### Features Added

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ The Azure Monitor Distro is a distribution of the .NET OpenTelemetry SDK and rel
1818
* [ASP.NET Core Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/) provides automatic tracing for incoming HTTP requests to ASP.NET Core applications.
1919
* [HTTP Client Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http/) provides automatic tracing for outgoing HTTP requests made using [System.Net.Http.HttpClient](https://docs.microsoft.com/dotnet/api/system.net.http.httpclient) and [System.Net.HttpWebRequest](https://docs.microsoft.com/dotnet/api/system.net.httpwebrequest).
2020
* [SQL Client Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.SqlClient) provides automatic tracing for SQL queries executed using the [Microsoft.Data.SqlClient](https://www.nuget.org/packages/Microsoft.Data.SqlClient) and [System.Data.SqlClient](https://www.nuget.org/packages/System.Data.SqlClient) packages.
21-
* [Application Insights Sampler](https://www.nuget.org/packages/OpenTelemetry.Extensions.AzureMonitor/) provides a sampling that is compatible with Application Insights sampling.
2221

2322
* Metrics
2423
* [ASP.NET Core Instrumentation Library](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore/) provides automatic collection of common ASP.NET Core metrics.
@@ -152,8 +151,10 @@ builder.Services.Configure<SqlClientInstrumentationOptions>(options =>
152151
When using the Azure Monitor Distro, the sampling percentage for telemetry data is set to 100% (1.0F) by default. For example, let's say you want to set the sampling percentage to 50%. You can achieve this by modifying the code as follows:
153152

154153
``` C#
155-
builder.Services.AddOpenTelemetry().UseAzureMonitor();
156-
builder.Services.Configure<ApplicationInsightsSamplerOptions>(options => { options.SamplingRatio = 0.5F; });
154+
builder.Services.AddOpenTelemetry().UseAzureMonitor(o =>
155+
{
156+
o.SamplingRatio = 0.5F;
157+
});
157158
```
158159

159160
#### Adding Custom ActivitySource to Traces

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public AzureMonitorOptions() { }
66
public string ConnectionString { get { throw null; } set { } }
77
public Azure.Core.TokenCredential Credential { get { throw null; } set { } }
88
public bool DisableOfflineStorage { get { throw null; } set { } }
9+
public float SamplingRatio { get { throw null; } set { } }
910
public string StorageDirectory { get { throw null; } set { } }
1011
}
1112
public static partial class OpenTelemetryBuilderExtensions

sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="OpenTelemetry.Extensions.AzureMonitor" />
1312
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
1413
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
1514
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public class AzureMonitorOptions : ClientOptions
3636
/// </summary>
3737
public bool DisableOfflineStorage { get; set; }
3838

39+
/// <summary>
40+
/// Gets or sets the ratio of telemetry items to be sampled. The value must be between 0.0F and 1.0F, inclusive.
41+
/// For example, specifying 0.4 means that 40% of traces are sampled and 60% are dropped.
42+
/// The default value is 1.0F, indicating that all telemetry items are sampled.
43+
/// </summary>
44+
public float SamplingRatio { get; set; } = 1.0F;
45+
3946
/// <summary>
4047
/// Override the default directory for offline storage.
4148
/// </summary>
@@ -46,6 +53,7 @@ internal void SetValueToExporterOptions(AzureMonitorExporterOptions exporterOpti
4653
exporterOptions.ConnectionString = ConnectionString;
4754
exporterOptions.Credential = Credential;
4855
exporterOptions.DisableOfflineStorage = DisableOfflineStorage;
56+
exporterOptions.SamplingRatio = SamplingRatio;
4957
exporterOptions.StorageDirectory = StorageDirectory;
5058
if (Transport != null)
5159
{

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
1010
using OpenTelemetry;
11-
using OpenTelemetry.Extensions.AzureMonitor;
1211
using OpenTelemetry.Logs;
1312
using OpenTelemetry.Metrics;
1413
using OpenTelemetry.Trace;
@@ -94,11 +93,6 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
9493
.AddAspNetCoreInstrumentation()
9594
.AddHttpClientInstrumentation()
9695
.AddSqlClientInstrumentation()
97-
.SetSampler(sp =>
98-
{
99-
var options = sp.GetRequiredService<IOptionsMonitor<ApplicationInsightsSamplerOptions>>().Get(Options.DefaultName);
100-
return new ApplicationInsightsSampler(options);
101-
})
10296
.AddAzureMonitorTraceExporter());
10397

10498
builder.WithMetrics(b => b
@@ -116,9 +110,6 @@ public static OpenTelemetryBuilder UseAzureMonitor(this OpenTelemetryBuilder bui
116110
});
117111
});
118112

119-
// Set the default sampling ratio to 100 % to ensure that all telemetry is captured by default.
120-
builder.Services.Configure<ApplicationInsightsSamplerOptions>(options => { options.SamplingRatio = 1.0F; });
121-
122113
// Add AzureMonitorLogExporter to AzureMonitorOptions
123114
// once the service provider is available containing the final
124115
// AzureMonitorOptions.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Features Added
66

7+
* Added `ApplicationInsightsSampler` to the exporter, enabling users to customize the sampling rate using the `SamplingRatio` property.
8+
([#36972](https://github.com/Azure/azure-sdk-for-net/pull/36972))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public AzureMonitorExporterOptions(Azure.Monitor.OpenTelemetry.Exporter.AzureMon
1515
public string? ConnectionString { get { throw null; } set { } }
1616
public Azure.Core.TokenCredential? Credential { get { throw null; } set { } }
1717
public bool DisableOfflineStorage { get { throw null; } set { } }
18+
public float SamplingRatio { get { throw null; } set { } }
1819
public string? StorageDirectory { get { throw null; } set { } }
1920
public Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorExporterOptions.ServiceVersion Version { get { throw null; } set { } }
2021
public enum ServiceVersion

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public class AzureMonitorExporterOptions : ClientOptions
3030
/// </summary>
3131
public TokenCredential? Credential { get; set; }
3232

33+
/// <summary>
34+
/// Gets or sets the ratio of telemetry items to be sampled. The value must be between 0.0F and 1.0F, inclusive.
35+
/// For example, specifying 0.4 means that 40% of traces are sampled and 60% are dropped.
36+
/// The default value is 1.0F, indicating that all telemetry items are sampled.
37+
/// </summary>
38+
public float SamplingRatio { get; set; } = 1.0F;
39+
3340
/// <summary>
3441
/// The <see cref="ServiceVersion"/> of the Azure Monitor ingestion API.
3542
/// </summary>

0 commit comments

Comments
 (0)