Address AOT warnings in Exporter.OpenTelemetryProtocol (#4859)

This commit is contained in:
Eric Erhardt 2023-09-20 12:51:15 -05:00 committed by GitHub
parent 7cb92d3b6d
commit f3da99f4ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 56 deletions

View File

@ -13,7 +13,7 @@
vulnerability in the NuGet packages that are published from this repository.
-->
<ItemGroup>
<PackageVersion Include="Google.Protobuf" Version="[3.19.4,4.0)" />
<PackageVersion Include="Google.Protobuf" Version="[3.22.5,4.0)" />
<PackageVersion Include="Grpc" Version="[2.44.0,3.0)" />
<PackageVersion Include="Grpc.Net.Client" Version="[2.52.0,3.0)" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="[2.1.1,6.0)" />
@ -41,9 +41,6 @@
-->
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
<!-- A conservative version of System.Reflection.Emit.Lightweight must be used here since there is no backward compatibility guarantee during major version bumps. -->
<PackageVersion Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<!-- A conservative version of System.Text.Encodings.Web must be used here since there is no backward compatibility guarantee during major version bumps. -->
<PackageVersion Include="System.Text.Encodings.Web" Version="4.7.2" />

View File

@ -2,6 +2,12 @@
## Unreleased
* Bumped the version of `Google.Protobuf` used by the project to `3.22.5` so
that consuming applications can be published as NativeAOT successfully. Also,
a new performance feature can be used instead of reflection emit, which is
not AOT-compatible. Removed the dependency on `System.Reflection.Emit.Lightweight`.
([#4859](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4859))
## 1.6.0
Released 2023-Sep-05

View File

@ -16,11 +16,8 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using Google.Protobuf;
using Google.Protobuf.Collections;
using OpenTelemetry.Internal;
using OpenTelemetry.Proto.Collector.Trace.V1;
using OpenTelemetry.Proto.Common.V1;
@ -34,7 +31,6 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
internal static class ActivityExtensions
{
private static readonly ConcurrentBag<ScopeSpans> SpanListPool = new();
private static readonly Action<RepeatedField<Span>, int> RepeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
internal static void AddBatch(
this ExportTraceServiceRequest request,
@ -84,7 +80,7 @@ internal static class ActivityExtensions
foreach (var scope in resourceSpans.ScopeSpans)
{
RepeatedFieldOfSpanSetCountAction(scope.Spans, 0);
scope.Spans.Clear();
SpanListPool.Add(scope);
}
}
@ -298,27 +294,6 @@ internal static class ActivityExtensions
return otlpEvent;
}
private static Action<RepeatedField<Span>, int> CreateRepeatedFieldOfSpanSetCountAction()
{
FieldInfo repeatedFieldOfSpanCountField = typeof(RepeatedField<Span>).GetField("count", BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod dynamicMethod = new DynamicMethod(
"CreateSetCountAction",
null,
new[] { typeof(RepeatedField<Span>), typeof(int) },
typeof(ActivityExtensions).Module,
skipVisibility: true);
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld, repeatedFieldOfSpanCountField);
generator.Emit(OpCodes.Ret);
return (Action<RepeatedField<Span>, int>)dynamicMethod.CreateDelegate(typeof(Action<RepeatedField<Span>, int>));
}
private struct TagEnumerationState : PeerServiceResolver.IPeerServiceState
{
public SdkLimitOptions SdkLimitOptions;

View File

@ -15,8 +15,6 @@
// </copyright>
using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using Google.Protobuf;
using Google.Protobuf.Collections;
@ -31,7 +29,6 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
internal static class MetricItemExtensions
{
private static readonly ConcurrentBag<OtlpMetrics.ScopeMetrics> MetricListPool = new();
private static readonly Action<RepeatedField<OtlpMetrics.Metric>, int> RepeatedFieldOfMetricSetCountAction = CreateRepeatedFieldOfMetricSetCountAction();
internal static void AddMetrics(
this OtlpCollector.ExportMetricsServiceRequest request,
@ -82,7 +79,7 @@ internal static class MetricItemExtensions
foreach (var scope in resourceMetrics.ScopeMetrics)
{
RepeatedFieldOfMetricSetCountAction(scope.Metrics, 0);
scope.Metrics.Clear();
MetricListPool.Add(scope);
}
}
@ -418,25 +415,4 @@ internal static class MetricItemExtensions
return otlpExemplar;
}
*/
private static Action<RepeatedField<OtlpMetrics.Metric>, int> CreateRepeatedFieldOfMetricSetCountAction()
{
FieldInfo repeatedFieldOfMetricCountField = typeof(RepeatedField<OtlpMetrics.Metric>).GetField("count", BindingFlags.NonPublic | BindingFlags.Instance);
DynamicMethod dynamicMethod = new DynamicMethod(
"CreateSetCountAction",
null,
new[] { typeof(RepeatedField<OtlpMetrics.Metric>), typeof(int) },
typeof(MetricItemExtensions).Module,
skipVisibility: true);
var generator = dynamicMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld, repeatedFieldOfMetricCountField);
generator.Emit(OpCodes.Ret);
return (Action<RepeatedField<OtlpMetrics.Metric>, int>)dynamicMethod.CreateDelegate(typeof(Action<RepeatedField<OtlpMetrics.Metric>, int>));
}
}

View File

@ -19,7 +19,6 @@
<PackageReference Include="Grpc" Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net462'" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Condition="'$(TargetFramework)' != 'net6.0'" />
</ItemGroup>
<ItemGroup>

View File

@ -17,6 +17,7 @@
<TrimmerRootAssembly Include="OpenTelemetry.Api" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.Console" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.InMemory" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.Prometheus.HttpListener" />
<TrimmerRootAssembly Include="OpenTelemetry.Exporter.Zipkin" />