@@ -292,16 +292,7 @@ private static int WriteMetric(byte[] buffer, int writePosition, Metric metric)
292
292
writePosition = ProtobufSerializer . WriteDoubleWithTag ( buffer , writePosition , ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Max , max ) ;
293
293
}
294
294
295
- foreach ( var histogramMeasurement in metricPoint . GetHistogramBuckets ( ) )
296
- {
297
- var bucketCount = ( ulong ) histogramMeasurement . BucketCount ;
298
- writePosition = ProtobufSerializer . WriteFixed64WithTag ( buffer , writePosition , ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Bucket_Counts , bucketCount ) ;
299
-
300
- if ( histogramMeasurement . ExplicitBound != double . PositiveInfinity )
301
- {
302
- writePosition = ProtobufSerializer . WriteDoubleWithTag ( buffer , writePosition , ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Explicit_Bounds , histogramMeasurement . ExplicitBound ) ;
303
- }
304
- }
295
+ writePosition = WriteHistogramBuckets ( buffer , writePosition , metricPoint . GetHistogramBuckets ( ) ) ;
305
296
306
297
writePosition = WriteDoubleExemplars ( buffer , writePosition , ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Exemplars , in metricPoint ) ;
307
298
@@ -513,4 +504,77 @@ private static int WriteExemplar(byte[] buffer, int writePosition, in Exemplar e
513
504
ProtobufSerializer . WriteReservedLength ( buffer , exemplarLengthPosition , writePosition - ( exemplarLengthPosition + ReserveSizeForLength ) ) ;
514
505
return writePosition ;
515
506
}
507
+
508
+ private static int WriteHistogramBuckets ( byte [ ] buffer , int writePosition , HistogramBuckets buckets )
509
+ {
510
+ writePosition = WriteBucketCounts ( buffer , writePosition , buckets . BucketCounts ) ;
511
+
512
+ writePosition = WriteExplicitBounds ( buffer , writePosition , buckets . ExplicitBounds ! ) ;
513
+
514
+ return writePosition ;
515
+
516
+ static int WriteBucketCounts ( byte [ ] buffer , int writePosition , HistogramBuckets . HistogramBucketValues [ ] values )
517
+ {
518
+ int length = values . Length ;
519
+
520
+ writePosition = WritePackedLength (
521
+ buffer ,
522
+ writePosition ,
523
+ length ,
524
+ ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Bucket_Counts ) ;
525
+
526
+ for ( int i = 0 ; i < length ; i ++ )
527
+ {
528
+ writePosition = ProtobufSerializer . WriteFixed64LittleEndianFormat (
529
+ buffer ,
530
+ writePosition ,
531
+ ( ulong ) values [ i ] . SnapshotValue ) ;
532
+ }
533
+
534
+ return writePosition ;
535
+ }
536
+
537
+ static int WriteExplicitBounds ( byte [ ] buffer , int writePosition , double [ ] values )
538
+ {
539
+ int length = 0 ;
540
+
541
+ for ( int i = 0 ; i < values . Length ; i ++ )
542
+ {
543
+ if ( values [ i ] != double . PositiveInfinity )
544
+ {
545
+ length ++ ;
546
+ }
547
+ }
548
+
549
+ if ( length > 0 )
550
+ {
551
+ writePosition = WritePackedLength (
552
+ buffer ,
553
+ writePosition ,
554
+ length ,
555
+ ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Explicit_Bounds ) ;
556
+
557
+ for ( int i = 0 ; i < values . Length ; i ++ )
558
+ {
559
+ var value = values [ i ] ;
560
+ if ( value != double . PositiveInfinity )
561
+ {
562
+ writePosition = ProtobufSerializer . WriteDouble ( buffer , writePosition , value ) ;
563
+ }
564
+ }
565
+ }
566
+
567
+ return writePosition ;
568
+ }
569
+
570
+ static int WritePackedLength ( byte [ ] buffer , int writePosition , int length , int fieldNumber )
571
+ {
572
+ return ProtobufSerializer . WriteTagAndLength (
573
+ buffer ,
574
+ writePosition ,
575
+ length * 8 ,
576
+ fieldNumber ,
577
+ ProtobufWireType . LEN ) ;
578
+ }
579
+ }
516
580
}
0 commit comments