Skip to content

Commit

Permalink
[Instrumentation.AWSLambda] Fix AoT warnings
Browse files Browse the repository at this point in the history
Make OpenTelemetry.Instrumentation.AWSLambda AoT compatible by using System.Text.Json instead of Newtonsoft.Json and updating to Amazon.Lambda packages that support native AoT.
  • Loading branch information
martincostello committed Jan 22, 2024
1 parent adef11b commit f7220fd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.AWSLambda/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
([#1295](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1295))
* BREAKING: Target `net6.0` instead of `netstandard2.0`
([#1545](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1545))
* Fix native AoT warnings
([#1543](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1543))

## 1.2.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using Amazon.Lambda.SNSEvents;
using Amazon.Lambda.SQSEvents;
using Newtonsoft.Json;
using OpenTelemetry.Context.Propagation;

namespace OpenTelemetry.Instrumentation.AWSLambda.Implementation;
Expand Down Expand Up @@ -132,7 +132,7 @@ internal static PropagationContext ExtractParentContext(SNSEvent.SNSMessage? mes
{
try
{
snsMessage = JsonConvert.DeserializeObject<SNSEvent.SNSMessage>(body);
snsMessage = JsonSerializer.Deserialize(body, SourceGenerationContext.Default.SNSMessage);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.4.1" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.2.0" />
<PackageReference Include="Amazon.Lambda.SNSEvents" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageReference Include="Amazon.Lambda.SNSEvents" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.2.0" />
<PackageReference Include="OpenTelemetry.Extensions.AWS" Version="1.3.0-beta.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;
using Amazon.Lambda.SNSEvents;

namespace OpenTelemetry.Instrumentation.AWSLambda;

/// <summary>
/// "Source Generation" is feature added to System.Text.Json in .NET 6.0.
/// This is a performance optimization that avoids runtime reflection when performing serialization.
/// Serialization metadata will be computed at compile-time and included in the assembly.
/// <see href="https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/source-generation-modes" />.
/// <see href="https://learn.microsoft.com/dotnet/standard/serialization/system-text-json/source-generation" />.
/// <see href="https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/" />.
/// </summary>
[JsonSerializable(typeof(SNSEvent.SNSMessage))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
When adding projects here please also update the verify-aot-compat job in
.github\workflows\ci.yml so that it runs for the project being added.
-->
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.Runtime" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.AWSLambda" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.EventCounters" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.Runtime" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.StackExchangeRedis" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Azure" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Host" />
Expand Down

0 comments on commit f7220fd

Please sign in to comment.