Skip to content

Commit 6c46a70

Browse files
authored
[1.3.0 patch] Future-proof OTLP exporter against future breaking changes (#3622)
1 parent 6b8eef0 commit 6c46a70

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ActivityExtensions.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
3636
internal static class ActivityExtensions
3737
{
3838
private static readonly ConcurrentBag<OtlpTrace.ScopeSpans> SpanListPool = new();
39-
private static readonly Action<RepeatedField<OtlpTrace.Span>, int> RepeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
39+
private static Action<RepeatedField<OtlpTrace.Span>, int> repeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
4040

4141
internal static void AddBatch(
4242
this OtlpCollector.ExportTraceServiceRequest request,
@@ -77,6 +77,11 @@ internal static void AddBatch(
7777
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7878
internal static void Return(this OtlpCollector.ExportTraceServiceRequest request)
7979
{
80+
if (repeatedFieldOfSpanSetCountAction == null)
81+
{
82+
return;
83+
}
84+
8085
var resourceSpans = request.ResourceSpans.FirstOrDefault();
8186
if (resourceSpans == null)
8287
{
@@ -85,15 +90,24 @@ internal static void Return(this OtlpCollector.ExportTraceServiceRequest request
8590

8691
foreach (var scope in resourceSpans.ScopeSpans)
8792
{
88-
RepeatedFieldOfSpanSetCountAction(scope.Spans, 0);
93+
try
94+
{
95+
repeatedFieldOfSpanSetCountAction(scope.Spans, 0);
96+
}
97+
catch
98+
{
99+
repeatedFieldOfSpanSetCountAction = null;
100+
return;
101+
}
102+
89103
SpanListPool.Add(scope);
90104
}
91105
}
92106

93107
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94108
internal static OtlpTrace.ScopeSpans GetSpanListFromPool(string name, string version)
95109
{
96-
if (!SpanListPool.TryTake(out var spans))
110+
if (repeatedFieldOfSpanSetCountAction == null || !SpanListPool.TryTake(out var spans))
97111
{
98112
spans = new OtlpTrace.ScopeSpans
99113
{
@@ -293,6 +307,11 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven
293307
{
294308
FieldInfo repeatedFieldOfSpanCountField = typeof(RepeatedField<OtlpTrace.Span>).GetField("count", BindingFlags.NonPublic | BindingFlags.Instance);
295309

310+
if (repeatedFieldOfSpanCountField == null)
311+
{
312+
return null;
313+
}
314+
296315
DynamicMethod dynamicMethod = new DynamicMethod(
297316
"CreateSetCountAction",
298317
null,

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/MetricItemExtensions.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
3333
internal static class MetricItemExtensions
3434
{
3535
private static readonly ConcurrentBag<OtlpMetrics.ScopeMetrics> MetricListPool = new();
36-
private static readonly Action<RepeatedField<OtlpMetrics.Metric>, int> RepeatedFieldOfMetricSetCountAction = CreateRepeatedFieldOfMetricSetCountAction();
36+
private static Action<RepeatedField<OtlpMetrics.Metric>, int> repeatedFieldOfMetricSetCountAction = CreateRepeatedFieldOfMetricSetCountAction();
3737

3838
internal static void AddMetrics(
3939
this OtlpCollector.ExportMetricsServiceRequest request,
@@ -76,6 +76,11 @@ internal static void AddMetrics(
7676
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7777
internal static void Return(this OtlpCollector.ExportMetricsServiceRequest request)
7878
{
79+
if (repeatedFieldOfMetricSetCountAction == null)
80+
{
81+
return;
82+
}
83+
7984
var resourceMetrics = request.ResourceMetrics.FirstOrDefault();
8085
if (resourceMetrics == null)
8186
{
@@ -84,15 +89,24 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque
8489

8590
foreach (var scope in resourceMetrics.ScopeMetrics)
8691
{
87-
RepeatedFieldOfMetricSetCountAction(scope.Metrics, 0);
92+
try
93+
{
94+
repeatedFieldOfMetricSetCountAction(scope.Metrics, 0);
95+
}
96+
catch
97+
{
98+
repeatedFieldOfMetricSetCountAction = null;
99+
return;
100+
}
101+
88102
MetricListPool.Add(scope);
89103
}
90104
}
91105

92106
[MethodImpl(MethodImplOptions.AggressiveInlining)]
93107
internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version)
94108
{
95-
if (!MetricListPool.TryTake(out var metrics))
109+
if (repeatedFieldOfMetricSetCountAction == null || !MetricListPool.TryTake(out var metrics))
96110
{
97111
metrics = new OtlpMetrics.ScopeMetrics
98112
{
@@ -338,6 +352,11 @@ private static OtlpMetrics.Exemplar ToOtlpExemplar(this IExemplar exemplar)
338352
{
339353
FieldInfo repeatedFieldOfMetricCountField = typeof(RepeatedField<OtlpMetrics.Metric>).GetField("count", BindingFlags.NonPublic | BindingFlags.Instance);
340354

355+
if (repeatedFieldOfMetricCountField == null)
356+
{
357+
return null;
358+
}
359+
341360
DynamicMethod dynamicMethod = new DynamicMethod(
342361
"CreateSetCountAction",
343362
null,

0 commit comments

Comments
 (0)