Skip to content

Commit

Permalink
Address AOT warnings in Exporter.OpenTelemetryProtocol
Browse files Browse the repository at this point in the history
This addresses the AOT warnings in the last library in this opentelemetry-dotnet.

1. Google.Protobuf v3.19.4 isn't trimming and AOT compatible. We need to upgrade to a newer version. I chose the latest patch in v3.22.x. v3.22.0 was the first version that is AOT compatible.

2. There were 2 places in Exporter.OpenTelemetryProtocol that was using System.Reflection.Emit to set a private field on Protobuf's RepeatedField class in order to return it to a pool. Setting this private field is no longer necessary since v3.22.0 because the Clear method was updated to support this scenario. See protocolbuffers/protobuf@a4fd216

Fix open-telemetry#3429
  • Loading branch information
eerhardt committed Sep 15, 2023
1 parent fe78453 commit a2e64cd
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Expand Up @@ -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)" />
Expand Down
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -84,7 +80,7 @@ internal static void Return(this ExportTraceServiceRequest request)

foreach (var scope in resourceSpans.ScopeSpans)
{
RepeatedFieldOfSpanSetCountAction(scope.Spans, 0);
scope.Spans.Clear();
SpanListPool.Add(scope);
}
}
Expand Down Expand Up @@ -298,27 +294,6 @@ private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent, SdkL
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;
Expand Down
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -82,7 +79,7 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque

foreach (var scope in resourceMetrics.ScopeMetrics)
{
RepeatedFieldOfMetricSetCountAction(scope.Metrics, 0);
scope.Metrics.Clear();
MetricListPool.Add(scope);
}
}
Expand Down Expand Up @@ -418,25 +415,4 @@ private static OtlpMetrics.Exemplar ToOtlpExemplar(this IExemplar exemplar)
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>));
}
}
Expand Up @@ -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" />
Expand Down

0 comments on commit a2e64cd

Please sign in to comment.