-
Notifications
You must be signed in to change notification settings - Fork 59
Adds counters to processed_variant and creates a wrapper for Beam counters. #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,109 @@ | ||
| # Copyright 2018 Google Inc. All Rights Reserved. | ||
| # | ||
| # Licensed 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. | ||
|
|
||
| """Wrapper for Metrics API of Beam. | ||
|
|
||
| This is used to avoid direct dependency on Beam in our library methods/classes | ||
| such that they are more modular and easier to test. Preferably instances | ||
| of classes in this module should be injected into library classes/methods | ||
| instead of direct instantiation. | ||
|
|
||
| This is an example of how to use the factory and counter objects: | ||
|
|
||
| ``` | ||
| factory = CounterFactory() | ||
| my_counter = factory.create_counter('my_counter_name') | ||
| my_counter.inc(4) | ||
| ``` | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import absolute_import | ||
|
|
||
| import logging | ||
|
|
||
| from apache_beam.runners import runner # pylint: disable=unused-import | ||
| from apache_beam import metrics | ||
| from apache_beam.metrics import metric | ||
|
|
||
| # The name space in which all metrics created by this class are. | ||
| _METRICS_NAMESPACE = 'VT_metrics_namespace' | ||
|
|
||
| # The name of the entry for counters in the dictionary that metrics().query() of | ||
| # Beam returns. | ||
| _COUNTERS = 'counters' | ||
|
|
||
|
|
||
| class CounterInterface(object): | ||
| """The interface of counter objects""" | ||
|
|
||
| def inc(self, n=1): | ||
| # type: (int) -> None | ||
| """Subclass implementations should do increment by `n`.""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class _NoOpCounter(CounterInterface): | ||
| """A counter that does nothing, good to be used when counter is optional.""" | ||
|
|
||
| def inc(self, n=1): | ||
| # type: (int) -> None | ||
| pass | ||
|
|
||
|
|
||
| class _CounterWrapper(CounterInterface): | ||
| """A wrapper for Beam counters.""" | ||
|
|
||
| def __init__(self, counter_name): | ||
| # type: (str) -> None | ||
| self._counter_name = counter_name | ||
| self._counter = metrics.Metrics.counter(_METRICS_NAMESPACE, counter_name) | ||
|
|
||
| def inc(self, n=1): | ||
| # type: (int) -> None | ||
| """Increments the counter by `n`""" | ||
| self._counter.inc(n) | ||
|
|
||
|
|
||
| class CounterFactoryInterface(object): | ||
| """The interface for counter factories.""" | ||
|
|
||
| def create_counter(self, counter_name): | ||
| # type: (str) -> CounterInterface | ||
| """Returns a counter with the given name.""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class NoOpCounterFactory(CounterFactoryInterface): | ||
| """A factory that creates counters that do nothing.""" | ||
|
|
||
| def create_counter(self, counter_name): | ||
| # type: (str) -> CounterInterface | ||
| return _NoOpCounter() | ||
|
|
||
|
|
||
| class CounterFactory(CounterFactoryInterface): | ||
|
|
||
| def create_counter(self, counter_name): | ||
| # type: (str) -> CounterInterface | ||
| return _CounterWrapper(counter_name) | ||
|
|
||
|
|
||
| def log_all_counters(pipeline_result): | ||
| """Logs all counters that belong to _METRICS_NAME_SPACE.""" | ||
| counter_filter = metric.MetricsFilter().with_namespace(_METRICS_NAMESPACE) | ||
| query_result = pipeline_result.metrics().query(counter_filter) | ||
| if query_result[_COUNTERS]: | ||
| for counter in query_result[_COUNTERS]: | ||
| logging.info('Counter %s = %d', counter, counter.committed) |
This file contains hidden or 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 @@ | ||
| # Copyright 2018 Google Inc. All Rights Reserved. | ||
| # | ||
| # Licensed 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. | ||
|
|
||
| """Unit tests for metrics_util module.""" | ||
|
|
||
| from __future__ import absolute_import | ||
|
|
||
| import unittest | ||
|
|
||
| from gcp_variant_transforms.libs import metrics_util | ||
|
|
||
|
|
||
| _TEST_COUNTER = 'test_counter' | ||
|
|
||
|
|
||
| class CounterFactoryTest(unittest.TestCase): | ||
|
|
||
| def test_create_counter(self): | ||
| counter = metrics_util.CounterFactory().create_counter(_TEST_COUNTER) | ||
| self.assertTrue(isinstance(counter, metrics_util.CounterInterface)) |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider creating an alias for _CounterEnum so that it becomes shorter and you don't need as much line breaks. We have adopted the style of importing
_CounterEnum as CounterEnumto make it look 'public' for tests (that's the only exception for directly importing classes in our code).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already done in the third PR (#131), so I prefer to leave it for there to get less merge conflicts.