dianfu commented on a change in pull request #11543: [FLINK-16672][python]
Support Counter, Gauge, Meter, Distribution metric type for Python UDF
URL: https://github.com/apache/flink/pull/11543#discussion_r399288674
##########
File path: flink-python/pyflink/metrics/metricbase.py
##########
@@ -76,3 +107,97 @@ def add_group(self, name: str, extra: str = None) ->
'MetricGroup':
else:
return self._add_group(name, MetricGroupType.key)\
._add_group(extra, MetricGroupType.value)
+
+ def counter(self, name: str) -> 'Counter':
+ from apache_beam.metrics.metric import Metrics
+ return Counter(Metrics.counter(self._get_namespace(), name))
+
+ def gauge(self, name: str, obj: Callable[[], int]) -> None:
+ from apache_beam.metrics.metric import Metrics
+ self._flink_gauge[name] = obj
+ self._beam_gauge[name] = Metrics.gauge(self._get_namespace(), name)
+
+ def meter(self, name: str, time_span_in_seconds: int = 60) -> 'Meter':
+ from apache_beam.metrics.metric import Metrics
+ # There is no meter type in Beam, use counter to implement meter
+ return Meter(
+ Metrics.counter(self._get_namespace(time_span_in_seconds), name),
+ time_span_in_seconds)
+
+ def distribution(self, name: str) -> 'Distribution':
+ from apache_beam.metrics.metric import Metrics
+ return Distribution(Metrics.distribution(self._get_namespace(), name))
+
+ def _get_metric_group_names_and_types(self) -> ([], []):
+ if self._name is None:
+ return [], []
+ else:
+ names, types = self._parent._get_metric_group_names_and_types()
+ names.append(self._name)
+ types.append(str(self._metric_group_type))
+ return names, types
+
+ def _get_namespace(self, time=None) -> str:
+ (names, metric_group_type) = self._get_metric_group_names_and_types()
+ names.extend(metric_group_type)
+ if time is not None:
+ names.append(str(time))
+ return json.dumps(names)
+
+
+class Metric(object):
+ """Base interface of a metric object."""
+ pass
+
+
+class Counter(Metric):
+ """Counter metric interface. Allows a count to be incremented/decremented
+ during pipeline execution."""
+
+ def __init__(self, inner_counter):
+ self._inner_counter = inner_counter
+
+ def inc(self, n=1):
+ self._inner_counter.inc(n)
+
+ def dec(self, n=1):
+ self.inc(-n)
+
+ def get_count(self):
+ from apache_beam.metrics.execution import MetricsEnvironment
+ container = MetricsEnvironment.current_container()
+ return
container.get_counter(self._inner_counter.metric_name).get_cumulative()
+
+
+class Distribution(Metric):
+ """Distribution Metric interface.
+
+ Allows statistics about the distribution of a variable to be collected
during
+ pipeline execution."""
+
+ def __init__(self, inner_distribution):
+ self._inner_distribution = inner_distribution
+
+ def update(self, value):
+ self._inner_distribution.update(value)
+
+
+class Meter(Metric):
+ """Meter Metric interface.
+
+ Metric for measuring throughput."""
+
+ def __init__(self, inner_counter, time_span_in_seconds=60):
+ self._inner_counter = inner_counter
+ self._time_span_in_seconds = time_span_in_seconds
+
+ def make_event(self, value=1):
+ self._inner_counter.inc(value)
+
+ def get_time_span_in_seconds(self):
Review comment:
Is it necessary to expose this method to users?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services