Skip to content

Commit db10c20

Browse files
committed
handle more straightforward metrics values
1 parent 849a2ec commit db10c20

3 files changed

Lines changed: 70 additions & 183 deletions

File tree

sdks/python/apache_beam/runners/dataflow/dataflow_metrics.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def _get_match(proto, filter_fn):
5757

5858

5959
# V1b3 MetricStructuredName keys to accept and copy to the MetricKey labels.
60-
STEP_LABEL = 'step'
6160
STRUCTURED_NAME_LABELS = set(
6261
['execution_step', 'original_name', 'output_user_name'])
6362

@@ -135,7 +134,7 @@ def _get_metric_key(self, metric):
135134
# step name (only happens for unstructured-named metrics).
136135
# 2. Unable to unpack [step] or [namespace]; which should only happen
137136
# for unstructured names.
138-
step = metric.name.context[STEP_LABEL],
137+
step = metric.name.context['step']
139138
step = self._translate_step_name(step)
140139
except ValueError:
141140
pass
@@ -203,36 +202,14 @@ def _get_metric_value(self, metric):
203202
if metric is None:
204203
return None
205204

206-
scalar_values = metric.scalar.struct_value.values()
207-
208-
if len(scalar_values) != 0:
205+
if metric.HasField('scalar'):
209206
# This will always be a single value if there is any data in the field.
210-
return scalar_values[0]
211-
elif metric.distribution is not None:
212-
dist_count = _get_match(
213-
metric.distribution.object_value.properties['count'],
214-
lambda x: x.key == 'count').value['integer_value']
215-
dist_min = _get_match(
216-
metric.distribution.object_value.properties,
217-
lambda x: x.key == 'min').value['integer_value']
218-
dist_max = _get_match(
219-
metric.distribution.object_value.properties,
220-
lambda x: x.key == 'max').value['integer_value']
221-
dist_sum = _get_match(
222-
metric.distribution.object_value.properties,
223-
lambda x: x.key == 'sum').value['integer_value']
224-
if dist_sum is None:
225-
# distribution metric is not meant to use on large values, but in case
226-
# it is, the value can overflow and become double_value, the correctness
227-
# of the value may not be guaranteed.
228-
_LOGGER.info(
229-
"Distribution metric sum value seems to have "
230-
"overflowed integer_value range, the correctness of sum or mean "
231-
"value may not be guaranteed: %s" % metric.distribution)
232-
dist_sum = int(
233-
_get_match(
234-
metric.distribution.object_value.properties,
235-
lambda x: x.key == 'sum').value.double_value)
207+
return metric.scalar.number_value
208+
elif metric.HasField('distribution'):
209+
dist_count = metric.distribution.struct_value.fields['count'].number_value
210+
dist_min = metric.distribution.struct_value.fields['min'].number_value
211+
dist_max = metric.distribution.struct_value.fields['max'].number_value
212+
dist_sum = metric.distribution.struct_value.fields['sum'].number_value
236213
return DistributionResult(
237214
DistributionData(dist_sum, dist_count, dist_min, dist_max))
238215
#TODO(https://github.com/apache/beam/issues/31788) support StringSet after
@@ -271,8 +248,6 @@ def all_metrics(self, job_id=None):
271248
def query(self, filter=None):
272249
metric_results = []
273250
response = self._get_metrics_from_dataflow()
274-
print("Metrics Response: \n")
275-
print(response)
276251
self._populate_metrics(response, metric_results, user_metrics=True)
277252
return {
278253
self.COUNTERS: [

0 commit comments

Comments
 (0)