@@ -33,7 +33,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
33
33
internal static class MetricItemExtensions
34
34
{
35
35
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 ( ) ;
37
37
38
38
internal static void AddMetrics (
39
39
this OtlpCollector . ExportMetricsServiceRequest request ,
@@ -76,6 +76,11 @@ internal static void AddMetrics(
76
76
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
77
77
internal static void Return ( this OtlpCollector . ExportMetricsServiceRequest request )
78
78
{
79
+ if ( repeatedFieldOfMetricSetCountAction == null )
80
+ {
81
+ return ;
82
+ }
83
+
79
84
var resourceMetrics = request . ResourceMetrics . FirstOrDefault ( ) ;
80
85
if ( resourceMetrics == null )
81
86
{
@@ -84,15 +89,24 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque
84
89
85
90
foreach ( var scope in resourceMetrics . ScopeMetrics )
86
91
{
87
- RepeatedFieldOfMetricSetCountAction ( scope . Metrics , 0 ) ;
92
+ try
93
+ {
94
+ repeatedFieldOfMetricSetCountAction ( scope . Metrics , 0 ) ;
95
+ }
96
+ catch
97
+ {
98
+ repeatedFieldOfMetricSetCountAction = null ;
99
+ return ;
100
+ }
101
+
88
102
MetricListPool . Add ( scope ) ;
89
103
}
90
104
}
91
105
92
106
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
93
107
internal static OtlpMetrics . ScopeMetrics GetMetricListFromPool ( string name , string version )
94
108
{
95
- if ( ! MetricListPool . TryTake ( out var metrics ) )
109
+ if ( repeatedFieldOfMetricSetCountAction == null || ! MetricListPool . TryTake ( out var metrics ) )
96
110
{
97
111
metrics = new OtlpMetrics . ScopeMetrics
98
112
{
@@ -338,6 +352,11 @@ private static OtlpMetrics.Exemplar ToOtlpExemplar(this IExemplar exemplar)
338
352
{
339
353
FieldInfo repeatedFieldOfMetricCountField = typeof ( RepeatedField < OtlpMetrics . Metric > ) . GetField ( "count" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
340
354
355
+ if ( repeatedFieldOfMetricCountField == null )
356
+ {
357
+ return null ;
358
+ }
359
+
341
360
DynamicMethod dynamicMethod = new DynamicMethod (
342
361
"CreateSetCountAction" ,
343
362
null ,
0 commit comments