@@ -413,17 +413,19 @@ def _split_metrics_data(
413413 # and yield this batch
414414 new_scope_metrics .metrics .append (new_metric )
415415 new_resource_metrics .scope_metrics .append (new_scope_metrics )
416+
416417 yield pb2 .MetricsData (
417418 resource_metrics = split_resource_metrics
418419 )
419420
420- # Reset all the variables
421+ # Reset all the variables with current metrics_data position
422+ # minus yielded datapoints
421423 batch_size = 0
422424 split_data_points = []
423425
424- reset_metric = None
426+ new_metric = None
425427 if metric .HasField ("sum" ):
426- reset_metric = pb2 .Metric (
428+ new_metric = pb2 .Metric (
427429 name = metric .name ,
428430 description = metric .description ,
429431 unit = metric .unit ,
@@ -434,7 +436,7 @@ def _split_metrics_data(
434436 )
435437 )
436438 elif metric .HasField ("histogram" ):
437- reset_metric = pb2 .Metric (
439+ new_metric = pb2 .Metric (
438440 name = metric .name ,
439441 description = metric .description ,
440442 unit = metric .unit ,
@@ -444,7 +446,7 @@ def _split_metrics_data(
444446 ),
445447 )
446448 elif metric .HasField ("exponential_histogram" ):
447- reset_metric = pb2 .Metric (
449+ new_metric = pb2 .Metric (
448450 name = metric .name ,
449451 description = metric .description ,
450452 unit = metric .unit ,
@@ -454,7 +456,7 @@ def _split_metrics_data(
454456 ),
455457 )
456458 elif metric .HasField ("gauge" ):
457- reset_metric = pb2 .Metric (
459+ new_metric = pb2 .Metric (
458460 name = metric .name ,
459461 description = metric .description ,
460462 unit = metric .unit ,
@@ -463,7 +465,7 @@ def _split_metrics_data(
463465 )
464466 )
465467 elif metric .HasField ("summary" ):
466- reset_metric = pb2 .Metric (
468+ new_metric = pb2 .Metric (
467469 name = metric .name ,
468470 description = metric .description ,
469471 unit = metric .unit ,
@@ -474,37 +476,38 @@ def _split_metrics_data(
474476 else :
475477 _logger .warning ("Tried to split and export an unsupported metric type." )
476478
477- if reset_metric is not None :
478- split_metrics = [reset_metric ]
479+ if new_metric is not None :
480+ split_metrics = [new_metric ]
481+ else :
482+ split_metrics = []
479483
480- split_scope_metrics = [
481- pb2 .ScopeMetrics (
482- scope = scope_metrics .scope ,
483- metrics = split_metrics ,
484- schema_url = scope_metrics .schema_url ,
485- )
486- ]
487- split_resource_metrics = [
488- pb2 .ResourceMetrics (
489- resource = resource_metrics .resource ,
490- scope_metrics = split_scope_metrics ,
491- schema_url = resource_metrics .schema_url ,
492- )
493- ]
484+ new_scope_metrics = pb2 .ScopeMetrics (
485+ scope = scope_metrics .scope ,
486+ metrics = split_metrics ,
487+ schema_url = scope_metrics .schema_url ,
488+ )
489+ split_scope_metrics = [new_scope_metrics ]
494490
495- # Update scope metrics after all data_points added to metric
496- new_scope_metrics .metrics .append (new_metric )
491+ new_resource_metrics = pb2 .ResourceMetrics (
492+ resource = resource_metrics .resource ,
493+ scope_metrics = split_scope_metrics ,
494+ schema_url = resource_metrics .schema_url ,
495+ )
496+ split_resource_metrics = [new_resource_metrics ]
497497
498498 if not split_data_points :
499499 # If data_points is empty remove the whole metric
500500 split_metrics .pop ()
501-
502- # Update resource_metrics after all scope_metrics updated with metrics, data_points
503- new_resource_metrics . scope_metrics .append (new_scope_metrics )
501+ else :
502+ # Update scope metrics after all data_points added to metric
503+ new_scope_metrics . metrics .append (new_metric )
504504
505505 if not split_metrics :
506506 # If metrics is empty remove the whole scope_metrics
507507 split_scope_metrics .pop ()
508+ else :
509+ # Update resource_metrics after all scope_metrics updated with metrics, data_points
510+ new_resource_metrics .scope_metrics .append (new_scope_metrics )
508511
509512 if not split_scope_metrics :
510513 # If scope_metrics is empty remove the whole resource_metrics
0 commit comments