2727
2828from apache_beam .coders import coder_impl
2929from apache_beam .coders import coders
30+ from apache_beam .internal .metrics .cells import HistogramData
3031from apache_beam .metrics .cells import BoundedTrieData
3132from apache_beam .metrics .cells import DistributionData
3233from apache_beam .metrics .cells import DistributionResult
4748 common_urns .monitoring_info_specs .FINISH_BUNDLE_MSECS .spec .urn )
4849TOTAL_MSECS_URN = common_urns .monitoring_info_specs .TOTAL_MSECS .spec .urn
4950USER_COUNTER_URN = common_urns .monitoring_info_specs .USER_SUM_INT64 .spec .urn
51+ USER_HISTOGRAM_URN = common_urns .monitoring_info_specs .USER_HISTOGRAM .spec .urn
5052USER_DISTRIBUTION_URN = (
5153 common_urns .monitoring_info_specs .USER_DISTRIBUTION_INT64 .spec .urn )
5254USER_GAUGE_URN = common_urns .monitoring_info_specs .USER_LATEST_INT64 .spec .urn
5961 USER_GAUGE_URN ,
6062 USER_STRING_SET_URN ,
6163 USER_BOUNDED_TRIE_URN ,
64+ USER_HISTOGRAM_URN
6265])
6366WORK_REMAINING_URN = common_urns .monitoring_info_specs .WORK_REMAINING .spec .urn
6467WORK_COMPLETED_URN = common_urns .monitoring_info_specs .WORK_COMPLETED .spec .urn
7780PROGRESS_TYPE = common_urns .monitoring_info_types .PROGRESS_TYPE .urn
7881STRING_SET_TYPE = common_urns .monitoring_info_types .SET_STRING_TYPE .urn
7982BOUNDED_TRIE_TYPE = common_urns .monitoring_info_types .BOUNDED_TRIE_TYPE .urn
83+ HISTOGRAM_TYPE = common_urns .monitoring_info_types .HISTOGRAM .urn
8084
8185COUNTER_TYPES = set ([SUM_INT64_TYPE ])
8286DISTRIBUTION_TYPES = set ([DISTRIBUTION_INT64_TYPE ])
8387GAUGE_TYPES = set ([LATEST_INT64_TYPE ])
8488STRING_SET_TYPES = set ([STRING_SET_TYPE ])
8589BOUNDED_TRIE_TYPES = set ([BOUNDED_TRIE_TYPE ])
90+ HISTOGRAM_TYPES = set ([HISTOGRAM_TYPE ])
8691
8792# TODO(migryz) extract values from beam_fn_api.proto::MonitoringInfoLabels
8893PCOLLECTION_LABEL = (
@@ -177,6 +182,14 @@ def extract_bounded_trie_value(monitoring_info_proto):
177182 metrics_pb2 .BoundedTrie .FromString (monitoring_info_proto .payload ))
178183
179184
185+ def extract_histogram_value (monitoring_info_proto ):
186+ if not is_histogram (monitoring_info_proto ):
187+ raise ValueError ('Unsupported type %s' % monitoring_info_proto .type )
188+
189+ return HistogramData .from_proto (
190+ metrics_pb2 .HistogramValue .FromString (monitoring_info_proto .payload ))
191+
192+
180193def create_labels (ptransform = None , namespace = None , name = None , pcollection = None ):
181194 """Create the label dictionary based on the provided values.
182195
@@ -334,6 +347,25 @@ def user_set_string(namespace, name, metric, ptransform=None):
334347 USER_STRING_SET_URN , STRING_SET_TYPE , metric , labels )
335348
336349
350+ def user_histogram (namespace , name , metric : HistogramData , ptransform = None ):
351+ """Return the histogram monitoring info for the URN, metric and labels.
352+
353+ Args:
354+ namespace: User-defined namespace of Histogram.
355+ name: Name of Histogram.
356+ metric: The Histogram representing the metrics.
357+ ptransform: The ptransform id used as a label.
358+ """
359+ labels = create_labels (ptransform = ptransform , namespace = namespace , name = name )
360+ metric_proto = metric .to_proto ()
361+
362+ return create_monitoring_info (
363+ USER_HISTOGRAM_URN ,
364+ HISTOGRAM_TYPE ,
365+ metric_proto .SerializeToString (),
366+ labels )
367+
368+
337369def user_bounded_trie (namespace , name , metric , ptransform = None ):
338370 """Return the string set monitoring info for the URN, metric and labels.
339371
@@ -353,7 +385,7 @@ def user_bounded_trie(namespace, name, metric, ptransform=None):
353385
354386def create_monitoring_info (
355387 urn , type_urn , payload , labels = None ) -> metrics_pb2 .MonitoringInfo :
356- """Return the gauge monitoring info for the URN, type, metric and labels.
388+ """Return the monitoring info for the URN, type, metric and labels.
357389
358390 Args:
359391 urn: The URN of the monitoring info/metric.
@@ -386,6 +418,11 @@ def is_distribution(monitoring_info_proto):
386418 return monitoring_info_proto .type in DISTRIBUTION_TYPES
387419
388420
421+ def is_histogram (monitoring_info_proto ):
422+ """Returns true if the monitoring info is a distrbution metric."""
423+ return monitoring_info_proto .type in HISTOGRAM_TYPES
424+
425+
389426def is_string_set (monitoring_info_proto ):
390427 """Returns true if the monitoring info is a StringSet metric."""
391428 return monitoring_info_proto .type in STRING_SET_TYPES
0 commit comments