@@ -428,21 +428,31 @@ const view = @import("../../sdk/metrics/view.zig");
428
428
pub const AggregatedMetrics = struct {
429
429
fn deduplicate (allocator : std.mem.Allocator , instr : * Instrument , aggregation : view.Aggregation ) ! MeasurementsData {
430
430
// This function is only called on read/export
431
- // which is much less frequent than other SDK operations.
431
+ // which is much less frequent than other SDK operations (e.g. counter add) .
432
432
// TODO: update to @branchHint in 0.14+
433
433
@setCold (true );
434
434
435
435
const allMeasurements : MeasurementsData = try instr .getInstrumentsData (allocator );
436
- defer allMeasurements .deinit (allocator );
436
+ defer {
437
+ switch (allMeasurements ) {
438
+ inline else = > | list | allocator .free (list ),
439
+ }
440
+ }
437
441
438
442
switch (allMeasurements ) {
439
443
.int = > {
440
444
var deduped = std .ArrayList (DataPoint (i64 )).init (allocator );
441
- defer deduped .deinit ();
442
445
443
- var temp = std .HashMap (Attributes , i64 , Attributes .HashContext , std .hash_map .default_max_load_percentage ).init (allocator );
446
+ var temp = std .HashMap (
447
+ Attributes ,
448
+ i64 ,
449
+ Attributes .HashContext ,
450
+ std .hash_map .default_max_load_percentage ,
451
+ ).init (allocator );
444
452
defer temp .deinit ();
445
453
454
+ // iterate over all measurements and deduplicate them by applying the aggregation function
455
+ // using the attributes as the key.
446
456
for (allMeasurements .int ) | measure | {
447
457
switch (aggregation ) {
448
458
.Drop = > return MeasurementsData { .int = &[_ ]DataPoint (i64 ){} },
@@ -465,14 +475,12 @@ pub const AggregatedMetrics = struct {
465
475
466
476
var iter = temp .iterator ();
467
477
while (iter .next ()) | entry | {
468
- // TODO add sharedAttributes to the measurements attributes.
469
478
try deduped .append (DataPoint (i64 ){ .attributes = entry .key_ptr .* .attributes , .value = entry .value_ptr .* });
470
479
}
471
480
return .{ .int = try deduped .toOwnedSlice () };
472
481
},
473
482
.double = > {
474
483
var deduped = std .ArrayList (DataPoint (f64 )).init (allocator );
475
- defer deduped .deinit ();
476
484
477
485
var temp = std .AutoHashMap (Attributes , f64 ).init (allocator );
478
486
defer temp .deinit ();
@@ -499,7 +507,6 @@ pub const AggregatedMetrics = struct {
499
507
500
508
var iter = temp .iterator ();
501
509
while (iter .next ()) | entry | {
502
- // TODO add sharedAttributes (the meter's attributes)to the measurements attributes.
503
510
try deduped .append (DataPoint (f64 ){ .attributes = entry .key_ptr .* .attributes , .value = entry .value_ptr .* });
504
511
}
505
512
return .{ .double = try deduped .toOwnedSlice () };
@@ -547,7 +554,9 @@ test "aggregated metrics deduplicated from meter without attributes" {
547
554
const instr = iter .next () orelse unreachable ;
548
555
549
556
const deduped = try AggregatedMetrics .deduplicate (std .testing .allocator , instr .* , .Sum );
550
- defer deduped .deinit (std .testing .allocator );
557
+ defer switch (deduped ) {
558
+ inline else = > | m | std .testing .allocator .free (m ),
559
+ };
551
560
552
561
try std .testing .expectEqualDeep (DataPoint (i64 ){ .value = 4 }, deduped .int [0 ]);
553
562
}
@@ -570,8 +579,10 @@ test "aggregated metrics deduplicated from meter with attributes" {
570
579
var iter = meter .instruments .valueIterator ();
571
580
const instr = iter .next () orelse unreachable ;
572
581
573
- var deduped = try AggregatedMetrics .deduplicate (std .testing .allocator , instr .* , .Sum );
574
- defer deduped .deinit (std .testing .allocator );
582
+ const deduped = try AggregatedMetrics .deduplicate (std .testing .allocator , instr .* , .Sum );
583
+ defer switch (deduped ) {
584
+ inline else = > | m | std .testing .allocator .free (m ),
585
+ };
575
586
576
587
const attrs = try Attributes .from (std .testing .allocator , .{ "key" , val });
577
588
defer if (attrs ) | a | std .testing .allocator .free (a );
0 commit comments