Skip to content

Commit dc2d56a

Browse files
author
Timothy Mothra
authored
[AzureMonitor] fix AOT warnings (part 6) (Azure#49756)
* add additional test coverage for Options before refactoring for AOT support * suppress warnings for IL2026 and IL3050 * cleanup * readme * pr feedback
1 parent a24ddc8 commit dc2d56a

File tree

3 files changed

+376
-1
lines changed

3 files changed

+376
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,27 @@ For more information on Azure SDK, please refer to [this website](https://azure.
259259
## Contributing
260260

261261
See [CONTRIBUTING.md](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md) for details on contribution process.
262+
263+
## AOT (Ahead-of-Time) Support
264+
265+
This library supports usage in .NET applications compiled with [AOT (Ahead-of-Time) compilation](https://learn.microsoft.com/dotnet/core/deploying/native-aot/).
266+
All core features of the Azure Monitor Exporter are compatible with AOT, including telemetry export for traces, metrics, and logs.
267+
268+
**Important:**
269+
While AOT is supported, automatic configuration binding from `appsettings.json` or other `IConfiguration` sources is **not** supported in AOT-compiled applications.
270+
This is due to .NET limitations on reflection-based binding APIs (such as `ConfigurationBinder.Bind` and `Get<T>()`) in AOT scenarios.
271+
272+
**Workaround:**
273+
In AOT scenarios, you can configure the Azure Monitor Exporter using one of the following approaches:
274+
275+
- **Environment Variable:** Set the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable to configure the connection string.
276+
277+
- **Programmatic Configuration:** Set the `AzureMonitorExporterOptions` directly in your application code:
278+
```csharp
279+
builder.Services.AddOpenTelemetry()
280+
.UseAzureMonitorExporter(options =>
281+
{
282+
options.ConnectionString = "<your-connection-string>";
283+
// Set other options as needed
284+
});
285+
```

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Diagnostics;
67
using Azure.Monitor.OpenTelemetry.Exporter.Internals.Platform;
78
using Microsoft.Extensions.Configuration;
@@ -29,7 +30,7 @@ public void Configure(AzureMonitorExporterOptions options)
2930
{
3031
if (_configuration != null)
3132
{
32-
_configuration.GetSection(AzureMonitorExporterSectionFromConfig).Bind(options);
33+
BindIConfigurationOptions(_configuration, options);
3334

3435
// IConfiguration can read from EnvironmentVariables or InMemoryCollection if configured to do so.
3536
var connectionStringFromIConfig = _configuration[EnvironmentVariableConstants.APPLICATIONINSIGHTS_CONNECTION_STRING];
@@ -51,5 +52,12 @@ public void Configure(AzureMonitorExporterOptions options)
5152
AzureMonitorExporterEventSource.Log.ConfigureFailed(ex);
5253
}
5354
}
55+
56+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Binding options is a known source of trim warnings; this is a deliberate usage.")]
57+
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Binding options is a known source of AOT warnings; this is a deliberate usage.")]
58+
private static void BindIConfigurationOptions(IConfiguration configuration, AzureMonitorExporterOptions options)
59+
{
60+
configuration.GetSection(AzureMonitorExporterSectionFromConfig).Bind(options);
61+
}
5462
}
5563
}

0 commit comments

Comments
 (0)