@@ -54,8 +54,6 @@ internal static void ReturnActivityListToPool()
5454
5555 internal static int WriteResourceSpans ( byte [ ] buffer , int writePosition , SdkLimitOptions sdkLimitOptions , Resources . Resource ? resource , Dictionary < string , List < Activity > > scopeTraces )
5656 {
57- int maxAttributeValueLength = sdkLimitOptions . AttributeValueLengthLimit ?? int . MaxValue ;
58-
5957 writePosition = ProtobufOtlpResourceSerializer . WriteResource ( buffer , writePosition , resource ) ;
6058 writePosition = WriteScopeSpans ( buffer , writePosition , sdkLimitOptions , scopeTraces ) ;
6159
@@ -96,20 +94,19 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt
9694 {
9795 var maxAttributeCount = sdkLimitOptions . SpanAttributeCountLimit ?? int . MaxValue ;
9896 var maxAttributeValueLength = sdkLimitOptions . AttributeValueLengthLimit ?? int . MaxValue ;
99- var attributeCount = 0 ;
100- var droppedAttributeCount = 0 ;
101-
10297 ProtobufOtlpTagWriter . OtlpTagWriterState otlpTagWriterState = new ProtobufOtlpTagWriter . OtlpTagWriterState
10398 {
10499 Buffer = buffer ,
105100 WritePosition = writePosition ,
101+ TagCount = 0 ,
102+ DroppedTagCount = 0 ,
106103 } ;
107104
108105 if ( activitySource . Tags is IReadOnlyList < KeyValuePair < string , object ? > > activitySourceTagsList )
109106 {
110107 for ( int i = 0 ; i < activitySourceTagsList . Count ; i ++ )
111108 {
112- if ( attributeCount < maxAttributeCount )
109+ if ( otlpTagWriterState . TagCount < maxAttributeCount )
113110 {
114111 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( otlpTagWriterState . Buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . InstrumentationScope_Attributes , ProtobufWireType . LEN ) ;
115112 int instrumentationScopeAttributesLengthPosition = otlpTagWriterState . WritePosition ;
@@ -119,19 +116,19 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt
119116
120117 var instrumentationScopeAttributesLength = otlpTagWriterState . WritePosition - ( instrumentationScopeAttributesLengthPosition + ReserveSizeForLength ) ;
121118 ProtobufSerializer . WriteReservedLength ( otlpTagWriterState . Buffer , instrumentationScopeAttributesLengthPosition , instrumentationScopeAttributesLength ) ;
122- attributeCount ++ ;
119+ otlpTagWriterState . TagCount ++ ;
123120 }
124121 else
125122 {
126- droppedAttributeCount ++ ;
123+ otlpTagWriterState . DroppedTagCount ++ ;
127124 }
128125 }
129126 }
130127 else
131128 {
132129 foreach ( var tag in activitySource . Tags )
133130 {
134- if ( attributeCount < maxAttributeCount )
131+ if ( otlpTagWriterState . TagCount < maxAttributeCount )
135132 {
136133 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( otlpTagWriterState . Buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . InstrumentationScope_Attributes , ProtobufWireType . LEN ) ;
137134 int instrumentationScopeAttributesLengthPosition = otlpTagWriterState . WritePosition ;
@@ -141,19 +138,19 @@ internal static int WriteScopeSpan(byte[] buffer, int writePosition, SdkLimitOpt
141138
142139 var instrumentationScopeAttributesLength = otlpTagWriterState . WritePosition - ( instrumentationScopeAttributesLengthPosition + ReserveSizeForLength ) ;
143140 ProtobufSerializer . WriteReservedLength ( otlpTagWriterState . Buffer , instrumentationScopeAttributesLengthPosition , instrumentationScopeAttributesLength ) ;
144- attributeCount ++ ;
141+ otlpTagWriterState . TagCount ++ ;
145142 }
146143 else
147144 {
148- droppedAttributeCount ++ ;
145+ otlpTagWriterState . DroppedTagCount ++ ;
149146 }
150147 }
151148 }
152149
153- if ( droppedAttributeCount > 0 )
150+ if ( otlpTagWriterState . DroppedTagCount > 0 )
154151 {
155152 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . InstrumentationScope_Dropped_Attributes_Count , ProtobufWireType . VARINT ) ;
156- otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) droppedAttributeCount ) ;
153+ otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) otlpTagWriterState . DroppedTagCount ) ;
157154 }
158155
159156 writePosition = otlpTagWriterState . WritePosition ;
@@ -242,13 +239,12 @@ internal static (int Position, StatusCode? StatusCode, string? StatusMessage) Wr
242239 string ? statusMessage = null ;
243240 int maxAttributeCount = sdkLimitOptions . SpanAttributeCountLimit ?? int . MaxValue ;
244241 int maxAttributeValueLength = sdkLimitOptions . AttributeValueLengthLimit ?? int . MaxValue ;
245- int attributeCount = 0 ;
246- int droppedAttributeCount = 0 ;
247-
248242 ProtobufOtlpTagWriter . OtlpTagWriterState otlpTagWriterState = new ProtobufOtlpTagWriter . OtlpTagWriterState
249243 {
250244 Buffer = buffer ,
251245 WritePosition = writePosition ,
246+ TagCount = 0 ,
247+ DroppedTagCount = 0 ,
252248 } ;
253249
254250 foreach ( ref readonly var tag in activity . EnumerateTagObjects ( ) )
@@ -275,7 +271,7 @@ not null when OkStatusCodeTagValue.Equals(tag.Value as string, StringComparison.
275271 continue ;
276272 }
277273
278- if ( attributeCount < maxAttributeCount )
274+ if ( otlpTagWriterState . TagCount < maxAttributeCount )
279275 {
280276 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( otlpTagWriterState . Buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Span_Attributes , ProtobufWireType . LEN ) ;
281277 int spanAttributesLengthPosition = otlpTagWriterState . WritePosition ;
@@ -284,18 +280,18 @@ not null when OkStatusCodeTagValue.Equals(tag.Value as string, StringComparison.
284280 ProtobufOtlpTagWriter . Instance . TryWriteTag ( ref otlpTagWriterState , tag . Key , tag . Value , maxAttributeValueLength ) ;
285281
286282 ProtobufSerializer . WriteReservedLength ( buffer , spanAttributesLengthPosition , otlpTagWriterState . WritePosition - ( spanAttributesLengthPosition + 4 ) ) ;
287- attributeCount ++ ;
283+ otlpTagWriterState . TagCount ++ ;
288284 }
289285 else
290286 {
291- droppedAttributeCount ++ ;
287+ otlpTagWriterState . DroppedTagCount ++ ;
292288 }
293289 }
294290
295- if ( droppedAttributeCount > 0 )
291+ if ( otlpTagWriterState . DroppedTagCount > 0 )
296292 {
297293 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Span_Dropped_Attributes_Count , ProtobufWireType . VARINT ) ;
298- otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) droppedAttributeCount ) ;
294+ otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) otlpTagWriterState . DroppedTagCount ) ;
299295 }
300296
301297 return ( otlpTagWriterState . WritePosition , statusCode , statusMessage ) ;
@@ -340,36 +336,36 @@ internal static int WriteEventAttributes(ref byte[] buffer, int writePosition, S
340336 {
341337 int maxAttributeCount = sdkLimitOptions . SpanEventAttributeCountLimit ?? int . MaxValue ;
342338 int maxAttributeValueLength = sdkLimitOptions . AttributeValueLengthLimit ?? int . MaxValue ;
343- int attributeCount = 0 ;
344- int droppedAttributeCount = 0 ;
345339
346340 ProtobufOtlpTagWriter . OtlpTagWriterState otlpTagWriterState = new ProtobufOtlpTagWriter . OtlpTagWriterState
347341 {
348342 Buffer = buffer ,
349343 WritePosition = writePosition ,
344+ TagCount = 0 ,
345+ DroppedTagCount = 0 ,
350346 } ;
351347
352348 foreach ( ref readonly var tag in evnt . EnumerateTagObjects ( ) )
353349 {
354- if ( attributeCount < maxAttributeCount )
350+ if ( otlpTagWriterState . TagCount < maxAttributeCount )
355351 {
356352 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( otlpTagWriterState . Buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Event_Attributes , ProtobufWireType . LEN ) ;
357353 int eventAttributesLengthPosition = otlpTagWriterState . WritePosition ;
358354 otlpTagWriterState . WritePosition += ReserveSizeForLength ;
359355 ProtobufOtlpTagWriter . Instance . TryWriteTag ( ref otlpTagWriterState , tag . Key , tag . Value , maxAttributeValueLength ) ;
360356 ProtobufSerializer . WriteReservedLength ( buffer , eventAttributesLengthPosition , otlpTagWriterState . WritePosition - ( eventAttributesLengthPosition + ReserveSizeForLength ) ) ;
361- attributeCount ++ ;
357+ otlpTagWriterState . TagCount ++ ;
362358 }
363359 else
364360 {
365- droppedAttributeCount ++ ;
361+ otlpTagWriterState . DroppedTagCount ++ ;
366362 }
367363 }
368364
369- if ( droppedAttributeCount > 0 )
365+ if ( otlpTagWriterState . DroppedTagCount > 0 )
370366 {
371367 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Event_Dropped_Attributes_Count , ProtobufWireType . VARINT ) ;
372- otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) droppedAttributeCount ) ;
368+ otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) otlpTagWriterState . DroppedTagCount ) ;
373369 }
374370
375371 return otlpTagWriterState . WritePosition ;
@@ -423,36 +419,35 @@ internal static int WriteLinkAttributes(byte[] buffer, int writePosition, SdkLim
423419 {
424420 int maxAttributeCount = sdkLimitOptions . SpanLinkAttributeCountLimit ?? int . MaxValue ;
425421 int maxAttributeValueLength = sdkLimitOptions . AttributeValueLengthLimit ?? int . MaxValue ;
426- int attributeCount = 0 ;
427- int droppedAttributeCount = 0 ;
428-
429422 ProtobufOtlpTagWriter . OtlpTagWriterState otlpTagWriterState = new ProtobufOtlpTagWriter . OtlpTagWriterState
430423 {
431424 Buffer = buffer ,
432425 WritePosition = writePosition ,
426+ TagCount = 0 ,
427+ DroppedTagCount = 0 ,
433428 } ;
434429
435430 foreach ( ref readonly var tag in link . EnumerateTagObjects ( ) )
436431 {
437- if ( attributeCount < maxAttributeCount )
432+ if ( otlpTagWriterState . TagCount < maxAttributeCount )
438433 {
439434 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( otlpTagWriterState . Buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Link_Attributes , ProtobufWireType . LEN ) ;
440435 int linkAttributesLengthPosition = otlpTagWriterState . WritePosition ;
441436 otlpTagWriterState . WritePosition += ReserveSizeForLength ;
442437 ProtobufOtlpTagWriter . Instance . TryWriteTag ( ref otlpTagWriterState , tag . Key , tag . Value , maxAttributeValueLength ) ;
443438 ProtobufSerializer . WriteReservedLength ( buffer , linkAttributesLengthPosition , otlpTagWriterState . WritePosition - ( linkAttributesLengthPosition + ReserveSizeForLength ) ) ;
444- attributeCount ++ ;
439+ otlpTagWriterState . TagCount ++ ;
445440 }
446441 else
447442 {
448- droppedAttributeCount ++ ;
443+ otlpTagWriterState . DroppedTagCount ++ ;
449444 }
450445 }
451446
452- if ( droppedAttributeCount > 0 )
447+ if ( otlpTagWriterState . DroppedTagCount > 0 )
453448 {
454449 otlpTagWriterState . WritePosition = ProtobufSerializer . WriteTag ( buffer , otlpTagWriterState . WritePosition , ProtobufOtlpTraceFieldNumberConstants . Link_Dropped_Attributes_Count , ProtobufWireType . VARINT ) ;
455- otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) droppedAttributeCount ) ;
450+ otlpTagWriterState . WritePosition = ProtobufSerializer . WriteVarInt32 ( buffer , otlpTagWriterState . WritePosition , ( uint ) otlpTagWriterState . DroppedTagCount ) ;
456451 }
457452
458453 return otlpTagWriterState . WritePosition ;
0 commit comments