-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Make reporting more metrics for tasks possible #3343
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
+195
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,106 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2012-2017 Spotify AB | ||
# | ||
# 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. | ||
# | ||
|
||
import os | ||
import tempfile | ||
import time | ||
|
||
from helpers import LuigiTestCase, temporary_unloaded_module | ||
|
||
import luigi | ||
from luigi.metrics import MetricsCollectors | ||
from luigi.scheduler import Scheduler | ||
from luigi.worker import Worker | ||
|
||
|
||
class CustomMetricsTestMyTask(luigi.Task): | ||
root_path = luigi.PathParameter() | ||
|
||
n = luigi.IntParameter() | ||
|
||
def output(self): | ||
basename = "%s_%s.txt" % (self.__class__.__name__, self.n) | ||
return luigi.LocalTarget(os.path.join(self.root_path, basename)) | ||
|
||
def run(self): | ||
time.sleep(self.n) | ||
with self.output().open('w') as f: | ||
f.write("content\n") | ||
|
||
|
||
class CustomMetricsTestWrapper(CustomMetricsTestMyTask): | ||
def requires(self): | ||
return [self.clone(CustomMetricsTestMyTask, n=n) for n in range(self.n)] | ||
|
||
|
||
METRICS_COLLECTOR_MODULE = b''' | ||
from luigi.metrics import NoMetricsCollector | ||
|
||
class CustomMetricsCollector(NoMetricsCollector): | ||
def __init__(self, *args, **kwargs): | ||
super(CustomMetricsCollector, self).__init__(*args, **kwargs) | ||
self.elapsed = {} | ||
|
||
def handle_task_statistics(self, task, statistics): | ||
if "elapsed" in statistics: | ||
self.elapsed[(task.family, task.params.get("n"))] = statistics["elapsed"] | ||
''' | ||
|
||
|
||
TASK_CONTEXT_MODULE = b''' | ||
import time | ||
|
||
class CustomTaskContext: | ||
def __init__(self, task_process): | ||
self._task_process = task_process | ||
self._start = None | ||
|
||
def __enter__(self): | ||
self._start = time.perf_counter() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
assert self._start is not None | ||
elapsed = time.perf_counter() - self._start | ||
self._task_process.status_reporter.report_task_statistics({"elapsed": elapsed}) | ||
''' | ||
|
||
|
||
class CustomMetricsTest(LuigiTestCase): | ||
""" | ||
Test showcasing collection of cutom metrics | ||
""" | ||
|
||
def _run_task_on_worker(self, worker): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
task = CustomMetricsTestWrapper(n=3, root_path=tmpdir) | ||
self.assertTrue(worker.add(task)) | ||
worker.run() | ||
self.assertTrue(task.complete()) | ||
|
||
def _create_worker_and_run_task(self, scheduler): | ||
with temporary_unloaded_module(TASK_CONTEXT_MODULE) as task_context_module: | ||
with Worker(scheduler=scheduler, worker_id='X', task_process_context=task_context_module + '.CustomTaskContext') as worker: | ||
self._run_task_on_worker(worker) | ||
|
||
def test_custom_metrics(self): | ||
with temporary_unloaded_module(METRICS_COLLECTOR_MODULE) as metrics_collector_module: | ||
scheduler = Scheduler(metrics_collector=MetricsCollectors.custom, metrics_custom_import=metrics_collector_module + '.CustomMetricsCollector') | ||
self._create_worker_and_run_task(scheduler) | ||
for (family, n), elapsed in scheduler._state._metrics_collector.elapsed.items(): | ||
self.assertTrue(family in {'CustomMetricsTestMyTask', 'CustomMetricsTestWrapper'}) | ||
self.assertTrue(elapsed >= float(n)) |
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.
Is there a common implementation that can be introduced here? Or within the Datadog and/or Prometheus implementations of
MetricsCollector
?If not, and this method needs to remain as an optionally-implemented method, i would ask at a minimum for there to be appropriately informative and discoverable documentation written surrounding its intended use. (I don't believe there are currently any
docs/
content around metrics collectors).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 method is entirely optional. I had not provided any documentation and tests yet, because I was hoping to gather some feedback in #3342 as you suggested in my earlier pull request (#3333 (comment)).
I will gladly add tests and documentation if you are convinced this change is small enough.