Skip to content

Conversation

@martincostello
Copy link
Member

@martincostello martincostello commented Nov 24, 2025

Changes

Bubble-up [RequiresUnreferencedCode] to the public API surface to enable the AoT analyzer, but not actually support native AoT.

See #357 (comment).

Resolves #97.

Merge requirement checklist

  • Unit tests added/updated
  • CHANGELOG.md updated
  • Changes in public API reviewed (if applicable)

Base automatically changed from aot-warnings to main November 25, 2025 07:31
Bubble-up `[RequiresUnreferencedCode]` to the public API surface to enable the AoT analyzer, but not actually support native AoT.
@martincostello
Copy link
Member Author

@matt-hensley Thoughts on this approach? The most relevant changes are the annotations added to the UseGrafana() methods.

Add entries for native AoT.
Copilot AI review requested due to automatic review settings November 25, 2025 16:01
@martincostello martincostello marked this pull request as ready for review November 25, 2025 16:02
@martincostello martincostello requested a review from a team as a code owner November 25, 2025 16:02
@martincostello martincostello enabled auto-merge (squash) November 25, 2025 16:04
Copilot finished reviewing on behalf of martincostello November 25, 2025 16:05
@martincostello martincostello merged commit c482466 into main Nov 25, 2025
20 checks passed
@martincostello martincostello deleted the aot-annotations branch November 25, 2025 16:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds native AoT (Ahead-of-Time compilation) annotations to the Grafana OpenTelemetry distribution for .NET by adding [RequiresUnreferencedCode] attributes to public APIs and internal implementations. The purpose is to enable the AoT analyzer to warn developers when using features incompatible with native AoT, while explicitly acknowledging that full native AoT support is not yet implemented.

Key changes:

  • Added [RequiresUnreferencedCode] annotations to public API methods (UseGrafana extension methods)
  • Added annotations to internal resource detector and instrumentation initializer classes
  • Changed IsAotCompatible property from false to true in the project file to enable AoT analysis
  • Updated CHANGELOG.md with relevant entries

Reviewed changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 28 comments.

Show a summary per file
File Description
src/Grafana.OpenTelemetry/OpenTelemetryBuilderExtension.cs Added AoT annotation to public UseGrafana extension method with clear user-facing message
src/Grafana.OpenTelemetry.Base/TracerProviderBuilderExtensions.cs Added AoT annotations to public UseGrafana method and internal AddResourceDetectors method
src/Grafana.OpenTelemetry.Base/MeterProviderBuilderExtensions.cs Added AoT annotations to public UseGrafana method and internal AddResourceDetectors method
src/Grafana.OpenTelemetry.Base/ResourceDetectors/ResourceDetectorInitializer.cs Added AoT annotation to base initializer class
src/Grafana.OpenTelemetry.Base/ResourceDetectors/ProcessRuntimeResource.cs Added AoT annotation to process runtime resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/ProcessResourceInitializer.cs Added AoT annotation to process resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/OperatingSystemResourceInitializer.cs Added AoT annotation to OS resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/HostDetectorInitializer.cs Added AoT annotation to host resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/ContainerResource.cs Added AoT annotation to container resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AzureVMDetectorInitializer.cs Added AoT annotation to Azure VM resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AzureContainerAppsDetectorInitializer.cs Added AoT annotation to Azure Container Apps resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AzureAppServiceDetectorInitializer.cs Added AoT annotation to Azure App Service resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AWSEKSDetectorInitializer.cs Added AoT annotation to AWS EKS resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AWSECSDetectorInitializer.cs Added AoT annotation to AWS ECS resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AWSEC2DetectorInitializer.cs Added AoT annotation to AWS EC2 resource detector
src/Grafana.OpenTelemetry.Base/ResourceDetectors/AWSEBSDetectorInitializer.cs Added AoT annotation to AWS EBS resource detector
src/Grafana.OpenTelemetry.Base/Instrumentations/WcfInitializer.cs Added AoT annotation to WCF instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/StackExchangeRedisInitializer.cs Added AoT annotation to Redis instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/QuartzInitializer.cs Added AoT annotation to Quartz instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/OwinInitializer.cs Added AoT annotation to OWIN instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/MySqlDataInitializer.cs Added AoT annotation to MySQL instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/HangfireInitializer.cs Added AoT annotation to Hangfire instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/EntityFrameworkCoreInitializer.cs Added AoT annotation to Entity Framework Core instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/ElasticsearchClientInitializer.cs Added AoT annotation to Elasticsearch instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/CassandraInitializer.cs Added AoT annotation to Cassandra instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/AspNetInitializer.cs Added AoT annotation to ASP.NET instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/AspNetCoreInitializer.cs Added AoT annotation to ASP.NET Core instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/AWSLambdaInitializer.cs Added AoT annotation to AWS Lambda instrumentation initializer
src/Grafana.OpenTelemetry.Base/Instrumentations/AWSInitializer.cs Added AoT annotation to AWS instrumentation initializer
src/Grafana.OpenTelemetry.Base/Grafana.OpenTelemetry.Base.csproj Changed IsAotCompatible from false to true to enable AoT analyzer warnings
CHANGELOG.md Added entries documenting the addition of native AoT annotations

namespace Grafana.OpenTelemetry
{
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message "Types might be removed" is vague and doesn't provide actionable information to developers. Consider using a more descriptive message that explains why this code is incompatible with native AoT.

For example: "This resource detector uses reflection for type discovery, which is not compatible with native AoT compilation."

This would provide clearer context about the limitation.

Suggested change
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This resource detector uses reflection for type discovery, which is not compatible with native AoT compilation.")]

Copilot uses AI. Check for mistakes.
namespace Grafana.OpenTelemetry
{
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message "Types might be removed" is vague and doesn't provide actionable information to developers. Consider using a more descriptive message that explains why this code is incompatible with native AoT.

For example: "This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation."

This would provide clearer context about the limitation.

Suggested change
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation.")]

Copilot uses AI. Check for mistakes.
namespace Grafana.OpenTelemetry
{
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message "Types might be removed" is vague and doesn't provide actionable information to developers. Consider using a more descriptive message that explains why this code is incompatible with native AoT.

For example: "This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation."

This would provide clearer context about the limitation.

Suggested change
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation.")]

Copilot uses AI. Check for mistakes.
namespace Grafana.OpenTelemetry
{
#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message "Types might be removed" is vague and doesn't provide actionable information to developers. Consider using a more descriptive message that explains why this code is incompatible with native AoT.

For example: "This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation."

This would provide clearer context about the limitation.

Suggested change
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This instrumentation uses reflection for type discovery, which is not compatible with native AoT compilation.")]

Copilot uses AI. Check for mistakes.
}

#if NET8_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message "Types might be removed" is vague and doesn't provide actionable information to developers. Consider using a more descriptive message that explains why this code is incompatible with native AoT.

For example: "This functionality uses reflection to discover and instantiate resource detectors, which is not compatible with native AoT compilation."

This would match the pattern used in the public API methods and provide clearer context.

Suggested change
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Types might be removed")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This functionality uses reflection to discover and instantiate resource detectors, which is not compatible with native AoT compilation.")]

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does not support trimming or native aot in dotnet 8

2 participants