forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-28887][python] Fix the bug of custom metrics in Thread Mode
This closes apache#20540.
- Loading branch information
1 parent
4739a5c
commit 4ebb787
Showing
35 changed files
with
635 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
17 changes: 17 additions & 0 deletions
17
flink-python/pyflink/fn_execution/metrics/embedded/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ |
41 changes: 41 additions & 0 deletions
41
flink-python/pyflink/fn_execution/metrics/embedded/counter_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from pyflink.metrics import Counter | ||
|
||
|
||
class CounterImpl(Counter): | ||
def __init__(self, inner_counter): | ||
self._inner_counter = inner_counter | ||
|
||
def inc(self, n: int = 1): | ||
""" | ||
Increment the current count by the given value. | ||
""" | ||
self._inner_counter.inc(n) | ||
|
||
def dec(self, n: int = 1): | ||
""" | ||
Decrement the current count by 1. | ||
""" | ||
self.inc(-n) | ||
|
||
def get_count(self) -> int: | ||
""" | ||
Returns the current count. | ||
""" | ||
return self._inner_counter.getCount() |
29 changes: 29 additions & 0 deletions
29
flink-python/pyflink/fn_execution/metrics/embedded/distribution_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from pyflink.metrics import Distribution | ||
|
||
|
||
class DistributionImpl(Distribution): | ||
def __init__(self, inner_distribution): | ||
self._inner_distribution = inner_distribution | ||
|
||
def update(self, value): | ||
""" | ||
Updates the distribution value. | ||
""" | ||
self._inner_distribution.update(value) |
36 changes: 36 additions & 0 deletions
36
flink-python/pyflink/fn_execution/metrics/embedded/meter_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from pyflink.metrics import Meter | ||
|
||
|
||
class MeterImpl(Meter): | ||
|
||
def __init__(self, inner_counter): | ||
self._inner_counter = inner_counter | ||
|
||
def mark_event(self, value: int = 1): | ||
""" | ||
Mark occurrence of the specified number of events. | ||
""" | ||
self._inner_counter.markEvent(value) | ||
|
||
def get_count(self) -> int: | ||
""" | ||
Get number of events marked on the meter. | ||
""" | ||
return self._inner_counter.getCount() |
61 changes: 61 additions & 0 deletions
61
flink-python/pyflink/fn_execution/metrics/embedded/metric_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from typing import Callable | ||
|
||
from pemja import findClass | ||
|
||
from pyflink.fn_execution.metrics.embedded.counter_impl import CounterImpl | ||
from pyflink.fn_execution.metrics.embedded.distribution_impl import DistributionImpl | ||
from pyflink.fn_execution.metrics.embedded.meter_impl import MeterImpl | ||
from pyflink.metrics import MetricGroup, Counter, Distribution, Meter | ||
|
||
JMeterView = findClass('org.apache.flink.metrics.MeterView') | ||
JMetricGauge = findClass('org.apache.flink.python.metric.embedded.MetricGauge') | ||
JMetricDistribution = findClass('org.apache.flink.python.metric.embedded.MetricDistribution') | ||
|
||
|
||
class MetricGroupImpl(MetricGroup): | ||
|
||
def __init__(self, metrics): | ||
self._metrics = metrics | ||
|
||
def add_group(self, name: str, extra: str = None) -> 'MetricGroup': | ||
if extra is None: | ||
return MetricGroupImpl(self._metrics.addGroup(name)) | ||
else: | ||
return MetricGroupImpl(self._metrics.addGroup(name, extra)) | ||
|
||
def counter(self, name: str) -> 'Counter': | ||
return CounterImpl(self._metrics.counter(name)) | ||
|
||
def gauge(self, name: str, obj: Callable[[], int]) -> None: | ||
self._metrics.gauge(name, JMetricGauge(PythonGaugeCallable(obj))) | ||
|
||
def meter(self, name: str, time_span_in_seconds: int = 60) -> 'Meter': | ||
return MeterImpl(self._metrics.meter(name, JMeterView(time_span_in_seconds))) | ||
|
||
def distribution(self, name: str) -> 'Distribution': | ||
return DistributionImpl(self._metrics.gauge(name, JMetricDistribution())) | ||
|
||
|
||
class PythonGaugeCallable(object): | ||
def __init__(self, func: Callable): | ||
self.func = func | ||
|
||
def get_value(self): | ||
return self.func() |
17 changes: 17 additions & 0 deletions
17
flink-python/pyflink/fn_execution/metrics/process/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ |
49 changes: 49 additions & 0 deletions
49
flink-python/pyflink/fn_execution/metrics/process/counter_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from pyflink.metrics import Counter | ||
|
||
|
||
class CounterImpl(Counter): | ||
def __init__(self, inner_counter): | ||
self._inner_counter = inner_counter | ||
|
||
def inc(self, n: int = 1): | ||
""" | ||
Increment the current count by the given value. | ||
.. versionadded:: 1.11.0 | ||
""" | ||
self._inner_counter.inc(n) | ||
|
||
def dec(self, n: int = 1): | ||
""" | ||
Decrement the current count by 1. | ||
.. versionadded:: 1.11.0 | ||
""" | ||
self.inc(-n) | ||
|
||
def get_count(self) -> int: | ||
""" | ||
Returns the current count. | ||
.. versionadded:: 1.11.0 | ||
""" | ||
from apache_beam.metrics.execution import MetricsEnvironment | ||
container = MetricsEnvironment.current_container() | ||
return container.get_counter(self._inner_counter.metric_name).get_cumulative() |
31 changes: 31 additions & 0 deletions
31
flink-python/pyflink/fn_execution/metrics/process/distribution_impl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
################################################################################ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
################################################################################ | ||
from pyflink.metrics import Distribution | ||
|
||
|
||
class DistributionImpl(Distribution): | ||
def __init__(self, inner_distribution): | ||
self._inner_distribution = inner_distribution | ||
|
||
def update(self, value): | ||
""" | ||
Updates the distribution value. | ||
.. versionadded:: 1.11.0 | ||
""" | ||
self._inner_distribution.update(value) |
Oops, something went wrong.