-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: add "flow logger" + instrument upload flow with it #47
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
716078f
feat: add "flow logger" + instrument upload flow with it
matt-codecov 581381b
Merge branch 'main' into matt/add-upload-flow-logs
matt-codecov 73f311b
rework so checkpoint data is serialized between tasks
matt-codecov aacbd27
Merge branch 'main' into matt/add-upload-flow-logs
matt-codecov 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 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,55 @@ | ||
import time | ||
from enum import Enum, auto | ||
|
||
import sentry_sdk | ||
|
||
|
||
class UploadFlow(Enum): | ||
UPLOAD_TASK_BEGIN = auto() | ||
PROCESSING_BEGIN = auto() | ||
INITIAL_PROCESSING_COMPLETE = auto() | ||
BATCH_PROCESSING_COMPLETE = auto() | ||
PROCESSING_COMPLETE = auto() | ||
NOTIFIED = auto() | ||
|
||
|
||
def _get_milli_timestamp(): | ||
return time.time_ns() // 1000000 | ||
|
||
|
||
class CheckpointLogger: | ||
def __init__(self, cls): | ||
self.cls = cls | ||
self.data = {} | ||
|
||
def _validate_checkpoint(self, checkpoint): | ||
if checkpoint.__class__ != self.cls: | ||
raise ValueError( | ||
f"Checkpoint {checkpoint} not part of flow `{self.cls.__name__}`" | ||
) | ||
|
||
def _subflow_duration(self, start, end): | ||
self._validate_checkpoint(start) | ||
self._validate_checkpoint(end) | ||
if start not in self.data: | ||
raise ValueError( | ||
f"Cannot compute duration; missing start checkpoint {start}" | ||
) | ||
elif end not in self.data: | ||
raise ValueError(f"Cannot compute duration; missing end checkpoint {end}") | ||
elif end.value <= start.value: | ||
raise ValueError( | ||
f"Cannot compute duration; end {end} is not after start {start}" | ||
) | ||
|
||
return self.data[end] - self.data[start] | ||
|
||
def log(self, checkpoint): | ||
if checkpoint in self.data: | ||
raise ValueError(f"Already recorded checkpoint {checkpoint}") | ||
self._validate_checkpoint(checkpoint) | ||
self.data[checkpoint] = _get_milli_timestamp() | ||
|
||
def submit_subflow(self, metric, start, end): | ||
duration = self._subflow_duration(start, end) | ||
sentry_sdk.set_measurement(metric, duration, "milliseconds") |
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,123 @@ | ||
import unittest | ||
from enum import Enum, auto | ||
from unittest.mock import ANY, patch | ||
|
||
import pytest | ||
import sentry_sdk | ||
|
||
from helpers.checkpoint_logger import CheckpointLogger, _get_milli_timestamp | ||
|
||
|
||
class TestEnum1(Enum): | ||
A = auto() | ||
B = auto() | ||
C = auto() | ||
|
||
|
||
class TestEnum2(Enum): | ||
A = auto() | ||
B = auto() | ||
C = auto() | ||
|
||
|
||
class TestCheckpointLogger(unittest.TestCase): | ||
@patch("time.time_ns", return_value=123456789) | ||
def test_get_milli_timestamp(self, mocker): | ||
expected_ms = 123456789 // 1000000 | ||
self.assertEqual(_get_milli_timestamp(), expected_ms) | ||
|
||
@patch("helpers.checkpoint_logger._get_milli_timestamp", return_value=1337) | ||
def test_log_checkpoint(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
|
||
self.assertEqual(checkpoints.data[TestEnum1.A], 1337) | ||
|
||
@patch( | ||
"helpers.checkpoint_logger._get_milli_timestamp", | ||
side_effect=[1337, 9001, 100000], | ||
) | ||
def test_log_multiple_checkpoints(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
checkpoints.log(TestEnum1.B) | ||
checkpoints.log(TestEnum1.C) | ||
|
||
self.assertEqual(checkpoints.data[TestEnum1.A], 1337) | ||
self.assertEqual(checkpoints.data[TestEnum1.B], 9001) | ||
self.assertEqual(checkpoints.data[TestEnum1.C], 100000) | ||
|
||
def test_log_checkpoint_twice_throws(self): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
|
||
with self.assertRaises(ValueError): | ||
checkpoints.log(TestEnum1.A) | ||
|
||
def test_log_checkpoint_wrong_enum_throws(self): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
|
||
with self.assertRaises(ValueError): | ||
checkpoints.log(TestEnum2.A) | ||
|
||
@patch("helpers.checkpoint_logger._get_milli_timestamp", side_effect=[1337, 9001]) | ||
def test_subflow_duration(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
checkpoints.log(TestEnum1.B) | ||
|
||
duration = checkpoints._subflow_duration(TestEnum1.A, TestEnum1.B) | ||
self.assertEqual(duration, 9001 - 1337) | ||
|
||
@patch("helpers.checkpoint_logger._get_milli_timestamp", side_effect=[1337, 9001]) | ||
def test_subflow_duration_missing_checkpoints(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
checkpoints.log(TestEnum1.C) | ||
|
||
# Missing end checkpoint | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum1.A, TestEnum1.B) | ||
|
||
# Missing start checkpoint | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum1.B, TestEnum1.C) | ||
|
||
@patch("helpers.checkpoint_logger._get_milli_timestamp", side_effect=[1337, 9001]) | ||
def test_subflow_duration_wrong_order(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
checkpoints.log(TestEnum1.B) | ||
|
||
# End < start | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum1.B, TestEnum1.A) | ||
|
||
# End == start | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum1.A, TestEnum1.A) | ||
|
||
@patch("helpers.checkpoint_logger._get_milli_timestamp", return_value=1337) | ||
def test_subflow_duration_wrong_enum(self, mocker): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
|
||
# Wrong enum for start checkpoint | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum2.A, TestEnum1.A) | ||
|
||
# Wrong enum for end checkpoint | ||
with self.assertRaises(ValueError): | ||
checkpoints._subflow_duration(TestEnum1.A, TestEnum2.A) | ||
|
||
@pytest.mark.real_checkpoint_logger | ||
@patch("helpers.checkpoint_logger._get_milli_timestamp", side_effect=[1337, 9001]) | ||
@patch("sentry_sdk.set_measurement") | ||
def test_submit_subflow(self, mock_sentry, mock_timestamp): | ||
checkpoints = CheckpointLogger(TestEnum1) | ||
checkpoints.log(TestEnum1.A) | ||
checkpoints.log(TestEnum1.B) | ||
|
||
expected_duration = 9001 - 1337 | ||
checkpoints.submit_subflow("metricname", TestEnum1.A, TestEnum1.B) | ||
mock_sentry.assert_called_with("metricname", expected_duration, "milliseconds") |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[pytest] | ||
addopts = --sqlalchemy-connect-url="postgresql://postgres@postgres:5432/background_test" | ||
markers= | ||
integration: integration tests (includes tests with vcrs) | ||
integration: integration tests (includes tests with vcrs) | ||
real_checkpoint_logger: prevents use of stubbed CheckpointLogger |
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.
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.
Would this be an instance of
CheckpointLogger
? I believe all these task arguments need to be serializable in some way (not sure if JSON is used or something else) - so just wanted to make sure this was tested in some sort of E2E fashion.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.
ah great callout, i think the tests don't actually exercise passing this between tasks
i was at least planning to create a sentry project and try to get a dashboard populated end-to-end before merging, but i'll also see if i can write a proper integration test that passes this between tasks in CI