2626from google .cloud .dataflow .internal .auth import get_service_credentials
2727from google .cloud .dataflow .internal .json_value import to_json_value
2828from google .cloud .dataflow .io import iobase
29+ from google .cloud .dataflow .transforms import cy_combiners
2930from google .cloud .dataflow .utils import dependency
3031from google .cloud .dataflow .utils import retry
3132from google .cloud .dataflow .utils .names import PropertyNames
4445STORAGE_API_SERVICE = 'storage.googleapis.com'
4546
4647
47- def append_counter (status_object , counter , tentative = False ):
48+ def append_counter (status_object , counter , tentative ):
4849 """Appends a counter to the status.
4950
5051 Args:
@@ -55,22 +56,23 @@ def append_counter(status_object, counter, tentative=False):
5556 logging .debug ('Appending counter%s %s' ,
5657 ' (tentative)' if tentative else '' ,
5758 counter )
59+ kind , setter = metric_translations [counter .combine_fn .__class__ ]
5860 append_metric (
59- status_object , counter .name , counter .total ,
60- counter .elements if counter .aggregation_kind == counter .MEAN else None ,
61- tentative = tentative )
61+ status_object , counter .name , kind , counter .accumulator ,
62+ setter , tentative = tentative )
6263
6364
64- def append_metric (status_object , metric_name , value1 , value2 = None ,
65+ def append_metric (status_object , metric_name , kind , value , setter = None ,
6566 step = None , output_user_name = None , tentative = False ,
6667 worker_id = None , cumulative = True ):
6768 """Creates and adds a MetricUpdate field to the passed-in protobuf.
6869
6970 Args:
7071 status_object: a work_item_status to which to add this metric
7172 metric_name: a string naming this metric
72- value1: scalar for a Sum or mean_sum for a Mean
73- value2: mean_count for a Mean aggregation (do not provide for a Sum).
73+ kind: dataflow counter kind (e.g. 'sum')
74+ value: accumulator value to encode
75+ setter: if not None, a lambda to use to update metric_update with value
7476 step: the name of the associated step
7577 output_user_name: the user-visible name to use
7678 tentative: whether this should be labeled as a tentative metric
@@ -103,19 +105,13 @@ def append_to_context(key, value):
103105 append_to_context ('workerId' , worker_id )
104106 if cumulative and is_counter :
105107 metric_update .cumulative = cumulative
106- if value2 is None :
107- if is_counter :
108- # Counters are distinguished by having a kind; metrics do not.
109- metric_update .kind = 'Sum'
110- metric_update .scalar = to_json_value (value1 , with_type = True )
111- elif value2 > 0 :
112- metric_update .kind = 'Mean'
113- metric_update .meanSum = to_json_value (value1 , with_type = True )
114- metric_update .meanCount = to_json_value (value2 , with_type = True )
108+ if is_counter :
109+ # Counters are distinguished by having a kind; metrics do not.
110+ metric_update .kind = kind
111+ if setter :
112+ setter (value , metric_update )
115113 else :
116- # A denominator of 0 will raise an error in the service.
117- # What it means is we have nothing to report yet, so don't.
118- pass
114+ metric_update .scalar = to_json_value (value , with_type = True )
119115 logging .debug ('Appending metric_update: %s' , metric_update )
120116 status_object .metricUpdates .append (metric_update )
121117
@@ -840,3 +836,33 @@ def cloud_position_to_reader_position(cloud_position):
840836def approximate_progress_to_dynamic_split_request (approximate_progress ):
841837 return iobase .DynamicSplitRequest (cloud_progress_to_reader_progress (
842838 approximate_progress ))
839+
840+
841+ def set_scalar (accumulator , metric_update ):
842+ metric_update .scalar = to_json_value (accumulator .value , with_type = True )
843+
844+
845+ def set_mean (accumulator , metric_update ):
846+ if accumulator .count :
847+ metric_update .meanSum = to_json_value (accumulator .sum , with_type = True )
848+ metric_update .meanCount = to_json_value (accumulator .count , with_type = True )
849+ else :
850+ # A denominator of 0 will raise an error in the service.
851+ # What it means is we have nothing to report yet, so don't.
852+ metric_update .kind = None
853+
854+
855+ # To enable a counter on the service, add it to this dictionary.
856+ metric_translations = {
857+ cy_combiners .CountCombineFn : ('sum' , set_scalar ),
858+ cy_combiners .SumInt64Fn : ('sum' , set_scalar ),
859+ cy_combiners .MinInt64Fn : ('min' , set_scalar ),
860+ cy_combiners .MaxInt64Fn : ('max' , set_scalar ),
861+ cy_combiners .MeanInt64Fn : ('mean' , set_mean ),
862+ cy_combiners .SumFloatFn : ('sum' , set_scalar ),
863+ cy_combiners .MinFloatFn : ('min' , set_scalar ),
864+ cy_combiners .MaxFloatFn : ('max' , set_scalar ),
865+ cy_combiners .MeanFloatFn : ('mean' , set_mean ),
866+ cy_combiners .AllCombineFn : ('and' , set_scalar ),
867+ cy_combiners .AnyCombineFn : ('or' , set_scalar ),
868+ }
0 commit comments