@@ -426,7 +426,7 @@ def _split_metrics_data(
426426
427427 if batch_size >= self ._max_export_batch_size :
428428 yield pb2 .MetricsData (
429- resource_metrics = self . _get_split_resource_metrics_pb2 (
429+ resource_metrics = _get_split_resource_metrics_pb2 (
430430 split_resource_metrics
431431 )
432432 )
@@ -527,171 +527,11 @@ def _split_metrics_data(
527527
528528 if batch_size > 0 :
529529 yield pb2 .MetricsData (
530- resource_metrics = self . _get_split_resource_metrics_pb2 (
530+ resource_metrics = _get_split_resource_metrics_pb2 (
531531 split_resource_metrics
532532 )
533533 )
534534
535- def _get_split_resource_metrics_pb2 (
536- self ,
537- split_resource_metrics : List [Dict ],
538- ) -> List [pb2 .ResourceMetrics ]:
539- """Helper that returns a list of pb2.ResourceMetrics objects based on split_resource_metrics.
540- Example input:
541-
542- ```python
543- [
544- {
545- "resource": <opentelemetry.proto.resource.v1.resource_pb2.Resource>,
546- "schema_url": "http://foo-bar",
547- "scope_metrics": [
548- "scope": <opentelemetry.proto.common.v1.InstrumentationScope>,
549- "schema_url": "http://foo-baz",
550- "metrics": [
551- {
552- "name": "apples",
553- "description": "number of apples purchased",
554- "sum": {
555- "aggregation_temporality": 1,
556- "is_monotonic": "false",
557- "data_points": [
558- {
559- start_time_unix_nano: 1000
560- time_unix_nano: 1001
561- exemplars {
562- time_unix_nano: 1002
563- span_id: "foo-span"
564- trace_id: "foo-trace"
565- as_int: 5
566- }
567- as_int: 5
568- }
569- ]
570- }
571- },
572- ],
573- ],
574- },
575- ]
576- ```
577-
578- Args:
579- split_resource_metrics: A list of dict representations of ResourceMetrics,
580- ScopeMetrics, Metrics, and data points.
581-
582- Returns:
583- List[pb2.ResourceMetrics]: A list of pb2.ResourceMetrics objects containing
584- pb2.ScopeMetrics, pb2.Metrics, and data points
585- """
586- split_resource_metrics_pb = []
587- for resource_metrics in split_resource_metrics :
588- new_resource_metrics = pb2 .ResourceMetrics (
589- resource = resource_metrics .get ("resource" ),
590- scope_metrics = [],
591- schema_url = resource_metrics .get ("schema_url" ),
592- )
593- for scope_metrics in resource_metrics .get ("scope_metrics" , []):
594- new_scope_metrics = pb2 .ScopeMetrics (
595- scope = scope_metrics .get ("scope" ),
596- metrics = [],
597- schema_url = scope_metrics .get ("schema_url" ),
598- )
599-
600- for metric in scope_metrics .get ("metrics" , []):
601- new_metric = None
602- data_points = []
603-
604- if "sum" in metric :
605- new_metric = pb2 .Metric (
606- name = metric .get ("name" ),
607- description = metric .get ("description" ),
608- unit = metric .get ("unit" ),
609- sum = pb2 .Sum (
610- data_points = [],
611- aggregation_temporality = metric .get ("sum" ).get (
612- "aggregation_temporality"
613- ),
614- is_monotonic = metric .get ("sum" ).get (
615- "is_monotonic"
616- ),
617- ),
618- )
619- data_points = metric .get ("sum" ).get ("data_points" )
620- elif "histogram" in metric :
621- new_metric = pb2 .Metric (
622- name = metric .get ("name" ),
623- description = metric .get ("description" ),
624- unit = metric .get ("unit" ),
625- histogram = pb2 .Histogram (
626- data_points = [],
627- aggregation_temporality = metric .get (
628- "histogram"
629- ).get ("aggregation_temporality" ),
630- ),
631- )
632- data_points = metric .get ("histogram" ).get (
633- "data_points"
634- )
635- elif "exponential_histogram" in metric :
636- new_metric = pb2 .Metric (
637- name = metric .get ("name" ),
638- description = metric .get ("description" ),
639- unit = metric .get ("unit" ),
640- exponential_histogram = pb2 .ExponentialHistogram (
641- data_points = [],
642- aggregation_temporality = metric .get (
643- "exponential_histogram"
644- ).get ("aggregation_temporality" ),
645- ),
646- )
647- data_points = metric .get ("exponential_histogram" ).get (
648- "data_points"
649- )
650- elif "gauge" in metric :
651- new_metric = pb2 .Metric (
652- name = metric .get ("name" ),
653- description = metric .get ("description" ),
654- unit = metric .get ("unit" ),
655- gauge = pb2 .Gauge (
656- data_points = [],
657- ),
658- )
659- data_points = metric .get ("gauge" ).get ("data_points" )
660- elif "summary" in metric :
661- new_metric = pb2 .Metric (
662- name = metric .get ("name" ),
663- description = metric .get ("description" ),
664- unit = metric .get ("unit" ),
665- summary = pb2 .Summary (
666- data_points = [],
667- ),
668- )
669- data_points = metric .get ("summary" ).get ("data_points" )
670- else :
671- _logger .warning (
672- "Tried to split and export an unsupported metric type. Skipping."
673- )
674- continue
675-
676- for data_point in data_points :
677- if "sum" in metric :
678- new_metric .sum .data_points .append (data_point )
679- elif "histogram" in metric :
680- new_metric .histogram .data_points .append (data_point )
681- elif "exponential_histogram" in metric :
682- new_metric .exponential_histogram .data_points .append (
683- data_point
684- )
685- elif "gauge" in metric :
686- new_metric .gauge .data_points .append (data_point )
687- elif "summary" in metric :
688- new_metric .summary .data_points .append (data_point )
689-
690- new_scope_metrics .metrics .append (new_metric )
691- new_resource_metrics .scope_metrics .append (new_scope_metrics )
692- split_resource_metrics_pb .append (new_resource_metrics )
693- return split_resource_metrics_pb
694-
695535 def shutdown (self , timeout_millis : float = 30_000 , ** kwargs ) -> None :
696536 if self ._shutdown :
697537 _logger .warning ("Exporter already shutdown, ignoring call" )
@@ -708,6 +548,162 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool:
708548 return True
709549
710550
551+ def _get_split_resource_metrics_pb2 (
552+ split_resource_metrics : List [Dict ],
553+ ) -> List [pb2 .ResourceMetrics ]:
554+ """Helper that returns a list of pb2.ResourceMetrics objects based on split_resource_metrics.
555+ Example input:
556+
557+ ```python
558+ [
559+ {
560+ "resource": <opentelemetry.proto.resource.v1.resource_pb2.Resource>,
561+ "schema_url": "http://foo-bar",
562+ "scope_metrics": [
563+ "scope": <opentelemetry.proto.common.v1.InstrumentationScope>,
564+ "schema_url": "http://foo-baz",
565+ "metrics": [
566+ {
567+ "name": "apples",
568+ "description": "number of apples purchased",
569+ "sum": {
570+ "aggregation_temporality": 1,
571+ "is_monotonic": "false",
572+ "data_points": [
573+ {
574+ start_time_unix_nano: 1000
575+ time_unix_nano: 1001
576+ exemplars {
577+ time_unix_nano: 1002
578+ span_id: "foo-span"
579+ trace_id: "foo-trace"
580+ as_int: 5
581+ }
582+ as_int: 5
583+ }
584+ ]
585+ }
586+ },
587+ ],
588+ ],
589+ },
590+ ]
591+ ```
592+
593+ Args:
594+ split_resource_metrics: A list of dict representations of ResourceMetrics,
595+ ScopeMetrics, Metrics, and data points.
596+
597+ Returns:
598+ List[pb2.ResourceMetrics]: A list of pb2.ResourceMetrics objects containing
599+ pb2.ScopeMetrics, pb2.Metrics, and data points
600+ """
601+ split_resource_metrics_pb = []
602+ for resource_metrics in split_resource_metrics :
603+ new_resource_metrics = pb2 .ResourceMetrics (
604+ resource = resource_metrics .get ("resource" ),
605+ scope_metrics = [],
606+ schema_url = resource_metrics .get ("schema_url" ),
607+ )
608+ for scope_metrics in resource_metrics .get ("scope_metrics" , []):
609+ new_scope_metrics = pb2 .ScopeMetrics (
610+ scope = scope_metrics .get ("scope" ),
611+ metrics = [],
612+ schema_url = scope_metrics .get ("schema_url" ),
613+ )
614+
615+ for metric in scope_metrics .get ("metrics" , []):
616+ new_metric = None
617+ data_points = []
618+
619+ if "sum" in metric :
620+ new_metric = pb2 .Metric (
621+ name = metric .get ("name" ),
622+ description = metric .get ("description" ),
623+ unit = metric .get ("unit" ),
624+ sum = pb2 .Sum (
625+ data_points = [],
626+ aggregation_temporality = metric .get ("sum" ).get (
627+ "aggregation_temporality"
628+ ),
629+ is_monotonic = metric .get ("sum" ).get ("is_monotonic" ),
630+ ),
631+ )
632+ data_points = metric .get ("sum" ).get ("data_points" )
633+ elif "histogram" in metric :
634+ new_metric = pb2 .Metric (
635+ name = metric .get ("name" ),
636+ description = metric .get ("description" ),
637+ unit = metric .get ("unit" ),
638+ histogram = pb2 .Histogram (
639+ data_points = [],
640+ aggregation_temporality = metric .get (
641+ "histogram"
642+ ).get ("aggregation_temporality" ),
643+ ),
644+ )
645+ data_points = metric .get ("histogram" ).get ("data_points" )
646+ elif "exponential_histogram" in metric :
647+ new_metric = pb2 .Metric (
648+ name = metric .get ("name" ),
649+ description = metric .get ("description" ),
650+ unit = metric .get ("unit" ),
651+ exponential_histogram = pb2 .ExponentialHistogram (
652+ data_points = [],
653+ aggregation_temporality = metric .get (
654+ "exponential_histogram"
655+ ).get ("aggregation_temporality" ),
656+ ),
657+ )
658+ data_points = metric .get ("exponential_histogram" ).get (
659+ "data_points"
660+ )
661+ elif "gauge" in metric :
662+ new_metric = pb2 .Metric (
663+ name = metric .get ("name" ),
664+ description = metric .get ("description" ),
665+ unit = metric .get ("unit" ),
666+ gauge = pb2 .Gauge (
667+ data_points = [],
668+ ),
669+ )
670+ data_points = metric .get ("gauge" ).get ("data_points" )
671+ elif "summary" in metric :
672+ new_metric = pb2 .Metric (
673+ name = metric .get ("name" ),
674+ description = metric .get ("description" ),
675+ unit = metric .get ("unit" ),
676+ summary = pb2 .Summary (
677+ data_points = [],
678+ ),
679+ )
680+ data_points = metric .get ("summary" ).get ("data_points" )
681+ else :
682+ _logger .warning (
683+ "Tried to split and export an unsupported metric type. Skipping."
684+ )
685+ continue
686+
687+ for data_point in data_points :
688+ if "sum" in metric :
689+ new_metric .sum .data_points .append (data_point )
690+ elif "histogram" in metric :
691+ new_metric .histogram .data_points .append (data_point )
692+ elif "exponential_histogram" in metric :
693+ new_metric .exponential_histogram .data_points .append (
694+ data_point
695+ )
696+ elif "gauge" in metric :
697+ new_metric .gauge .data_points .append (data_point )
698+ elif "summary" in metric :
699+ new_metric .summary .data_points .append (data_point )
700+
701+ new_scope_metrics .metrics .append (new_metric )
702+ new_resource_metrics .scope_metrics .append (new_scope_metrics )
703+ split_resource_metrics_pb .append (new_resource_metrics )
704+ return split_resource_metrics_pb
705+
706+
711707@deprecated (
712708 "Use one of the encoders from opentelemetry-exporter-otlp-proto-common instead. Deprecated since version 1.18.0." ,
713709)
0 commit comments