Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend AoT support #1541

Merged
merged 12 commits into from
Jan 23, 2024
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ jobs:
build: ['build/**', '.github/**/*.yml', '!.github/workflows/package-*']
shared: ['src/Shared/**']
code: ['**.cs', '.editorconfig']
aot: ['src/OpenTelemetry.Extensions.Enrichment/**']
aottestapp: ['test/OpenTelemetry.AotCompatibility.TestApp/**']
aspnet: ['*/OpenTelemetry.Instrumentation.AspNet*/**', 'examples/AspNet/**', '!**/*.md']
aws: ['*/OpenTelemetry.*.AWS*/**', '!**/*.md']
azure: ['*/OpenTelemetry.ResourceDetectors.Azure*/**', '!**/*.md']
eventcounters: ['*/OpenTelemetry.Instrumentation.EventCounters*/**', 'examples/event-counters/**', '!**/*.md']
extensions: ['*/OpenTelemetry.Extensions/**', '*/OpenTelemetry.Extensions.Tests/**', '!**/*.md']
Expand All @@ -35,6 +37,7 @@ jobs:
processdetector: ['*/OpenTelemetry.ResourceDetectors.Process/**', '*/OpenTelemetry.ResourceDetectors.Process.Tests/**', '!**/*.md']
processruntime: ['*/OpenTelemetry.ResourceDetectors.ProcessRuntime/**', '*/OpenTelemetry.ResourceDetectors.ProcessRuntime.Tests/**', '!**/*.md']
redis: ['*/OpenTelemetry.Instrumentation.StackExchangeRedis*/**', 'examples/redis/**', '!**/*.md']
resourcedetectors: ['*/OpenTelemetry.ResourceDetectors.*/**', '!**/*.md']
runtime: ['*/OpenTelemetry.Instrumentation.Runtime*/**', 'examples/runtime-instrumentation/**', '!**/*.md']
wcf: ['*/OpenTelemetry.Instrumentation.Wcf*/**', 'examples/wcf/**', '!**/*.md']
solution: [
Expand Down Expand Up @@ -343,10 +346,14 @@ jobs:
if: |
contains(needs.detect-changes.outputs.changes, 'eventcounters')
|| contains(needs.detect-changes.outputs.changes, 'runtime')
|| contains(needs.detect-changes.outputs.changes, 'aws')
|| contains(needs.detect-changes.outputs.changes, 'azure')
|| contains(needs.detect-changes.outputs.changes, 'extensions')
|| contains(needs.detect-changes.outputs.changes, 'host')
|| contains(needs.detect-changes.outputs.changes, 'processdetector')
|| contains(needs.detect-changes.outputs.changes, 'processruntime')
|| contains(needs.detect-changes.outputs.changes, 'resourcedetectors')
|| contains(needs.detect-changes.outputs.changes, 'aot')
|| contains(needs.detect-changes.outputs.changes, 'aottestapp')
|| contains(needs.detect-changes.outputs.changes, 'build')
|| contains(needs.detect-changes.outputs.changes, 'redis')
Expand Down
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Extensions.Enrichment/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# Changelog

## Unreleased

* Make Extensions.Enrichment AoT compatible.
([#1541](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1541))
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

using System;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;
Expand All @@ -23,7 +26,11 @@ public static class TraceEnrichmentProviderBuilderExtensions
/// <remarks>
/// Add this enricher *before* exporter related Activity processors.
/// </remarks>
#if NET6_0_OR_GREATER
public static TracerProviderBuilder AddTraceEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this TracerProviderBuilder builder)
#else
public static TracerProviderBuilder AddTraceEnricher<T>(this TracerProviderBuilder builder)
#endif
where T : TraceEnricher
{
Guard.ThrowIfNull(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

using System;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Linq;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenTelemetry.Extensions.Enrichment;
Expand All @@ -25,7 +28,11 @@ public static class TraceEnrichmentServiceCollectionExtensions
/// <remarks>
/// Add this enricher *before* exporter related Activity processors.
/// </remarks>
#if NET6_0_OR_GREATER
public static IServiceCollection AddTraceEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IServiceCollection services)
#else
public static IServiceCollection AddTraceEnricher<T>(this IServiceCollection services)
#endif
where T : TraceEnricher
{
Guard.ThrowIfNull(services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ internal static List<KeyValuePair<string, object>> ExtractResourceAttributes(AWS

internal static AWSEBSMetadataModel? GetEBSMetadata(string filePath)
{
#if NET6_0_OR_GREATER
return ResourceDetectorUtils.DeserializeFromFile(filePath, SourceGenerationContext.Default.AWSEBSMetadataModel);
#else
return ResourceDetectorUtils.DeserializeFromFile<AWSEBSMetadataModel>(filePath);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ internal static List<KeyValuePair<string, object>> ExtractResourceAttributes(AWS

internal static AWSEC2IdentityDocumentModel? DeserializeResponse(string response)
{
#if NET6_0_OR_GREATER
return ResourceDetectorUtils.DeserializeFromString(response, SourceGenerationContext.Default.AWSEC2IdentityDocumentModel);
#else
return ResourceDetectorUtils.DeserializeFromString<AWSEC2IdentityDocumentModel>(response);
#endif
}

private static string GetAWSEC2Token()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ internal static List<KeyValuePair<string, object>> ExtractResourceAttributes(str

internal static AWSEKSClusterInformationModel? DeserializeResponse(string response)
{
#if NET6_0_OR_GREATER
return ResourceDetectorUtils.DeserializeFromString(response, SourceGenerationContext.Default.AWSEKSClusterInformationModel);
#else
return ResourceDetectorUtils.DeserializeFromString<AWSEKSClusterInformationModel>(response);
#endif
}

private static string? GetEKSClusterName(string credentials, HttpClientHandler? httpClientHandler)
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.ResourceDetectors.AWS/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
([#1350](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1350))
* BREAKING: All Resource Detector classes marked as `sealed`.
([#1510](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1510))
* Make OpenTelemetry.ResourceDetectors.AWS native AoT compatible.
([#1541](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1541))

## 1.3.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;

namespace OpenTelemetry.ResourceDetectors.AWS.Models;

internal class AWSEC2IdentityDocumentModel
{
[JsonPropertyName("accountId")]
public string? AccountId { get; set; }

[JsonPropertyName("availabilityZone")]
public string? AvailabilityZone { get; set; }

[JsonPropertyName("region")]
public string? Region { get; set; }

[JsonPropertyName("instanceId")]
public string? InstanceId { get; set; }

[JsonPropertyName("instanceType")]
public string? InstanceType { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OpenTelemetry.ResourceDetectors.AWS.Models;

internal class AWSEKSClusterDataModel
internal sealed class AWSEKSClusterDataModel
{
[JsonPropertyName("cluster.name")]
public string? ClusterName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Text.Json.Serialization;

namespace OpenTelemetry.ResourceDetectors.AWS.Models;

internal class AWSEKSClusterInformationModel
internal sealed class AWSEKSClusterInformationModel
{
[JsonPropertyName("data")]
public AWSEKSClusterDataModel? Data { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.Net.Http;
using System.Text;
using System.Text.Json;
#if !NETFRAMEWORK
using System.Text.Json.Serialization.Metadata;
#endif
using System.Threading.Tasks;

namespace OpenTelemetry.ResourceDetectors.AWS;
Expand All @@ -16,7 +19,9 @@ namespace OpenTelemetry.ResourceDetectors.AWS;
/// </summary>
internal static class ResourceDetectorUtils
{
#if !NET6_0_OR_GREATER
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);
#endif

internal static async Task<string> SendOutRequest(string url, string method, KeyValuePair<string, string>? header, HttpClientHandler? handler = null)
{
Expand All @@ -40,6 +45,20 @@ internal static async Task<string> SendOutRequest(string url, string method, Key
}
}

#if NET6_0_OR_GREATER
internal static T? DeserializeFromFile<T>(string filePath, JsonTypeInfo<T> jsonTypeInfo)
{
using (var stream = GetStream(filePath))
{
return JsonSerializer.Deserialize(stream, jsonTypeInfo);
}
}

internal static T? DeserializeFromString<T>(string json, JsonTypeInfo<T> jsonTypeInfo)
{
return JsonSerializer.Deserialize(json, jsonTypeInfo);
}
#else
internal static T? DeserializeFromFile<T>(string filePath)
{
using (var stream = GetStream(filePath))
Expand All @@ -52,6 +71,7 @@ internal static async Task<string> SendOutRequest(string url, string method, Key
{
return JsonSerializer.Deserialize<T>(json, JsonSerializerOptions);
}
#endif

internal static Stream GetStream(string filePath)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
using System.Text.Json.Serialization;
using OpenTelemetry.ResourceDetectors.AWS.Models;

namespace OpenTelemetry.ResourceDetectors.AWS;

/// <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(AWSEBSMetadataModel))]
[JsonSerializable(typeof(AWSEC2IdentityDocumentModel))]
[JsonSerializable(typeof(AWSEKSClusterInformationModel))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext
{
}
#endif
21 changes: 19 additions & 2 deletions src/OpenTelemetry.Sampler.AWS/AWSXRaySamplerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public async Task<List<SamplingRule>> GetSamplingRules()

try
{
GetSamplingRulesResponse? getSamplingRulesResponse = JsonSerializer.Deserialize<GetSamplingRulesResponse>(responseJson);
GetSamplingRulesResponse? getSamplingRulesResponse = JsonSerializer
#if NET6_0_OR_GREATER
.Deserialize(responseJson, SourceGenerationContext.Default.GetSamplingRulesResponse);
#else
.Deserialize<GetSamplingRulesResponse>(responseJson);
#endif

if (getSamplingRulesResponse is not null)
{
if (getSamplingRulesResponse.SamplingRuleRecords is not null)
Expand Down Expand Up @@ -66,7 +72,14 @@ public async Task<List<SamplingRule>> GetSamplingRules()

public async Task<GetSamplingTargetsResponse?> GetSamplingTargets(GetSamplingTargetsRequest getSamplingTargetsRequest)
{
var content = new StringContent(JsonSerializer.Serialize(getSamplingTargetsRequest), Encoding.UTF8, this.jsonContentType);
var json = JsonSerializer
#if NET6_0_OR_GREATER
.Serialize(getSamplingTargetsRequest, SourceGenerationContext.Default.GetSamplingTargetsRequest);
#else
.Serialize(getSamplingTargetsRequest);
#endif

var content = new StringContent(json, Encoding.UTF8, this.jsonContentType);

using var request = new HttpRequestMessage(HttpMethod.Post, this.getSamplingTargetsEndpoint)
{
Expand All @@ -78,7 +91,11 @@ public async Task<List<SamplingRule>> GetSamplingRules()
try
{
GetSamplingTargetsResponse? getSamplingTargetsResponse = JsonSerializer
#if NET6_0_OR_GREATER
.Deserialize(responseJson, SourceGenerationContext.Default.GetSamplingTargetsResponse);
#else
.Deserialize<GetSamplingTargetsResponse>(responseJson);
#endif

return getSamplingTargetsResponse;
}
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Sampler.AWS/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ Initial release of `OpenTelemetry.Sampler.AWS`.

* Update OpenTelemetry SDK version to `1.7.0`.
([#1486](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1486))

* Make OpenTelemetry.Sampler.AWS native AoT compatible.
([#1541](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1541))
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OpenTelemetry.Sampler.AWS;

internal class GetSamplingRulesResponse
internal sealed class GetSamplingRulesResponse
{
[JsonPropertyName("NextToken")]
public string? NextToken { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OpenTelemetry.Sampler.AWS;

internal class GetSamplingTargetsRequest
internal sealed class GetSamplingTargetsRequest
{
public GetSamplingTargetsRequest(List<SamplingStatisticsDocument> documents)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OpenTelemetry.Sampler.AWS;

internal class GetSamplingTargetsResponse
internal sealed class GetSamplingTargetsResponse
{
public GetSamplingTargetsResponse(
double lastRuleModification,
Expand Down
23 changes: 23 additions & 0 deletions src/OpenTelemetry.Sampler.AWS/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if NET6_0_OR_GREATER
using System.Text.Json.Serialization;

namespace OpenTelemetry.Sampler.AWS;

/// <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(GetSamplingRulesResponse))]
[JsonSerializable(typeof(GetSamplingTargetsRequest))]
[JsonSerializable(typeof(GetSamplingTargetsResponse))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext
{
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
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.Extensions" />
<TrimmerRootAssembly Include="OpenTelemetry.Extensions.AWS" />
<TrimmerRootAssembly Include="OpenTelemetry.Extensions.Enrichment" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.Runtime" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.EventCounters" />
<TrimmerRootAssembly Include="OpenTelemetry.Instrumentation.StackExchangeRedis" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.AWS" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Azure" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Container" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Host" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.Process" />
<TrimmerRootAssembly Include="OpenTelemetry.ResourceDetectors.ProcessRuntime" />
<TrimmerRootAssembly Include="OpenTelemetry.Sampler.AWS" />

<TrimmerRootAssembly Update="@(TrimmerRootAssembly)" Path="$(RepoRoot)\src\%(Identity)\%(Identity).csproj" />
<ProjectReference Include="@(TrimmerRootAssembly->'%(Path)')" />
Expand Down
Loading