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-16671][python] Support for defining scopes on Python metric gr…
…oup (apache#11470)
- Loading branch information
Showing
44 changed files
with
386 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
################################################################################ | ||
# 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.metricbase import MetricGroup | ||
|
||
__all__ = ["MetricGroup"] |
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,78 @@ | ||
################################################################################ | ||
# 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. | ||
################################################################################ | ||
import abc | ||
from enum import Enum | ||
|
||
|
||
class MetricGroup(abc.ABC): | ||
|
||
def add_group(self, name: str, extra: str = None) -> 'MetricGroup': | ||
""" | ||
Creates a new MetricGroup and adds it to this groups sub-groups. | ||
If extra is not None, creates a new key-value MetricGroup pair. The key group | ||
is added to this groups sub-groups, while the value group is added to the key | ||
group's sub-groups. This method returns the value group. | ||
The only difference between calling this method and | ||
`group.add_group(key).add_group(value)` is that get_all_variables() | ||
of the value group return an additional `"<key>"="value"` pair. | ||
""" | ||
pass | ||
|
||
|
||
class MetricGroupType(Enum): | ||
""" | ||
Indicate the type of MetricGroup. | ||
""" | ||
generic = 0 | ||
key = 1 | ||
value = 2 | ||
|
||
|
||
class GenericMetricGroup(MetricGroup): | ||
|
||
def __init__( | ||
self, | ||
parent, | ||
name, | ||
metric_group_type=MetricGroupType.generic): | ||
self._parent = parent | ||
self._sub_groups = [] | ||
self._name = name | ||
self._metric_group_type = metric_group_type | ||
|
||
def _add_group(self, name: str, metric_group_type) -> 'MetricGroup': | ||
for group in self._sub_groups: | ||
if name == group._name and metric_group_type == group._metric_group_type: | ||
# we don't create same metric group repeatedly | ||
return group | ||
|
||
sub_group = GenericMetricGroup( | ||
self, | ||
name, | ||
metric_group_type) | ||
self._sub_groups.append(sub_group) | ||
return sub_group | ||
|
||
def add_group(self, name: str, extra: str = None) -> 'MetricGroup': | ||
if extra is None: | ||
return self._add_group(name, MetricGroupType.generic) | ||
else: | ||
return self._add_group(name, MetricGroupType.key)\ | ||
._add_group(extra, MetricGroupType.value) |
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. | ||
################################################################################ |
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,40 @@ | ||
################################################################################ | ||
# 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. | ||
################################################################################ | ||
|
||
import unittest | ||
from pyflink.metrics.metricbase import GenericMetricGroup, MetricGroup | ||
|
||
|
||
class MetricTests(unittest.TestCase): | ||
|
||
base_metric_group = GenericMetricGroup(None, None) | ||
|
||
@staticmethod | ||
def print_metric_group_path(mg: MetricGroup) -> str: | ||
if mg._parent is None: | ||
return 'root' | ||
else: | ||
return MetricTests.print_metric_group_path(mg._parent) + '.' + mg._name | ||
|
||
def test_add_group(self): | ||
new_group = MetricTests.base_metric_group.add_group('my_group') | ||
self.assertEqual(MetricTests.print_metric_group_path(new_group), 'root.my_group') | ||
|
||
def test_add_group_with_variable(self): | ||
new_group = MetricTests.base_metric_group.add_group('key', 'value') | ||
self.assertEqual(MetricTests.print_metric_group_path(new_group), 'root.key.value') |
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
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
35 changes: 35 additions & 0 deletions
35
flink-python/src/main/java/org/apache/flink/python/metric/FlinkMetricContainer.java
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,35 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.flink.python.metric; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.metrics.MetricGroup; | ||
|
||
/** | ||
* Helper class for forwarding Python metrics to Java accumulators and metrics. | ||
*/ | ||
@Internal | ||
public class FlinkMetricContainer { | ||
|
||
private final MetricGroup baseMetricGroup; | ||
|
||
public FlinkMetricContainer(MetricGroup metricGroup) { | ||
this.baseMetricGroup = metricGroup; | ||
} | ||
} |
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
Oops, something went wrong.