Skip to content

Commit

Permalink
Annotate Azure.Messaging.EventHubs for trim/AOT compat (#46606)
Browse files Browse the repository at this point in the history
* Annotate Azure.Messaging.EventHubs for trim/AOT compat

* PR feedback

- Annotate Azure.Messaging.EventHubs.Processor, add ci check
- Add changelog entries

Also make some minor fixes to the AOT compat script.

* Fix RequiresUnreferencedCode message
  • Loading branch information
sbomer authored Oct 17, 2024
1 parent 8cba7d7 commit 4a4a28c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 7 deletions.
18 changes: 12 additions & 6 deletions eng/scripts/compatibility/Check-AOT-Compatibility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if ($LASTEXITCODE -ne 0)
Set-Location -Path ..
Remove-Item -Path "./$folderPath" -Recurse -Force

Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
Exit 2
}

Expand Down Expand Up @@ -104,15 +104,18 @@ if (Test-Path $expectedWarningsFullPath -PathType Leaf) {
$numWarnings = $warnings.Count

if ($numWarnings -gt 0) {
Write-Host "Found $numWarnings additional warnings that were not expected:`n$warnings"
Write-Host "Found $numWarnings additional warnings that were not expected:"
foreach ($warning in $warnings) {
Write-Host $warning
}
}

Write-Host "Deleting test app files."

Set-Location -Path ..
Remove-Item -Path "./$folderPath" -Recurse -Force

Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"

exit $warnings.Count
}
Expand All @@ -126,7 +129,10 @@ Write-Host "Checking against the list of expected warnings. There are $numExpect
$warnings = $publishOutput -split "`n" | select-string -pattern 'IL\d+' | select-string -pattern '##' -notmatch | select-string -pattern $expectedWarnings -notmatch
$numWarnings = $warnings.Count
if ($numWarnings -gt 0) {
Write-Host "Found $numWarnings additional warnings that were not expected:`n$warnings"
Write-Host "Found $numWarnings additional warnings that were not expected:"
foreach ($warning in $warnings) {
Write-Host $warning
}
}

### Cleanup ###
Expand All @@ -138,9 +144,9 @@ Remove-Item -Path "./$folderPath" -Recurse -Force

if ($numExpectedWarnings -ne $actualWarningCount) {
Write-Host "The number of expected warnings ($numExpectedWarnings) was different than the actual warning count ($actualWarningCount)."
Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
exit 2
}

Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
exit $warnings.Count
5 changes: 4 additions & 1 deletion sdk/core/Azure.Core/src/Shared/AzureEventSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable
Expand All @@ -12,6 +12,9 @@ namespace Azure.Core.Diagnostics
{
internal abstract class AzureEventSource: EventSource
{
private protected const string EventSourceRequiresUnreferencedCodeMessage = "EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type";
private protected const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe.";

private const string SharedDataKey = "_AzureEventSourceNamesInUse";
private static readonly HashSet<string> NamesInUse;

Expand Down
2 changes: 2 additions & 0 deletions sdk/eventhub/Azure.Messaging.EventHubs.Processor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Other Changes

- Added annotations to make the package compatible with trimming and native AOT compilation.

## 5.11.5 (2024-08-14)

### Other Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<PackageTags>Azure;Event Hubs;EventHubs;.NET;Event Processor;EventProcessor;$(PackageCommonTags)</PackageTags>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics.Tracing;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Azure.Core.Diagnostics;
using Azure.Messaging.EventHubs.Primitives;
Expand Down Expand Up @@ -91,6 +92,7 @@ public virtual void ListOwnershipStart(string fullyQualifiedNamespace,
/// <param name="ownershipCount">The amount of ownership received from the storage service.</param>
///
[Event(22, Level = EventLevel.Verbose, Message = "Completed listing ownership for FullyQualifiedNamespace: '{0}'; EventHubName: '{1}'; ConsumerGroup: '{2}'. There were {3} ownership entries were found.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
public virtual void ListOwnershipComplete(string fullyQualifiedNamespace,
string eventHubName,
string consumerGroup,
Expand Down Expand Up @@ -434,6 +436,7 @@ public virtual void GetCheckpointError(string fullyQualifiedNamespace,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[RequiresUnreferencedCode(EventSourceRequiresUnreferencedCodeMessage)]
private unsafe void WriteEvent<TValue1>(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -476,6 +479,7 @@ private unsafe void WriteEvent<TValue1>(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -518,6 +522,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -567,6 +572,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -621,6 +627,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[RequiresUnreferencedCode(EventSourceRequiresUnreferencedCodeMessage)]
private unsafe void WriteEvent<TValue1>(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -677,6 +684,7 @@ private unsafe void WriteEvent<TValue1>(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -744,6 +752,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using Azure.Core.Diagnostics;
Expand Down Expand Up @@ -235,6 +236,7 @@ public virtual void EventBatchProcessingHandlerCall(string sequenceNumber,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -278,6 +280,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down Expand Up @@ -327,6 +330,7 @@ private unsafe void WriteEvent(int eventId,
///
[NonEvent]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
private unsafe void WriteEvent(int eventId,
string arg1,
string arg2,
Expand Down
2 changes: 2 additions & 0 deletions sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### Other Changes

- Added annotations to make the package compatible with trimming and native AOT compilation.

## 5.11.5 (2024-07-31)

### Other Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<PackageTags>Azure;Event Hubs;EventHubs;.NET;AMQP;IoT;$(PackageCommonTags)</PackageTags>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using Azure.Core.Extensions;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Producer;
Expand Down Expand Up @@ -46,6 +47,8 @@ public static IAzureClientBuilder<EventHubProducerClient, EventHubProducerClient
/// Registers a <see cref="EventHubProducerClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance.
/// </summary>
///
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
public static IAzureClientBuilder<EventHubProducerClient, EventHubProducerClientOptions> AddEventHubProducerClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration)
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
{
Expand Down Expand Up @@ -86,6 +89,8 @@ public static IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClient
/// Registers a <see cref="EventHubConsumerClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance.
/// </summary>
///
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
public static IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClientOptions> AddEventHubConsumerClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration)
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
{
Expand Down
Loading

0 comments on commit 4a4a28c

Please sign in to comment.