Skip to content
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

ref(rules): refactor delayed processing batching logic to prepare for workflows #83670

Merged
merged 5 commits into from
Jan 30, 2025

Conversation

cathteng
Copy link
Member

@cathteng cathteng commented Jan 17, 2025

Workflow engine will be using the same logic as delayed processing for rules to process slow conditions. This PR refactors the shared bit, the batching logic, to prepare for adding processing for workflows.

There are two differences the two kinds of delayed processing:

  1. The exact task for processing
  2. The information to get the hash from the buffer

To encapsulate the two differences, I've added a registry. Depending on which delayed processing we are doing, we fetch a handler that includes the information above and we use it in batching the task.

Copy link

sentry-io bot commented Jan 17, 2025

🔍 Existing Issues For Review

Your pull request is modifying functions with the following pre-existing issues:

📄 File: src/sentry/rules/processing/delayed_processing.py

Function Unhandled Issue
apply_delayed Rule.DoesNotExist: Rule matching query does not exist. sentry.rules.processing.delayed_proc...
Event Count: 1

Did you find this useful? React with a 👍 or 👎

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 17, 2025
# with metrics.timer("delayed_workflow.process_all_conditions.duration"):
# process_project_ids(fetch_time, WORKFLOW_ENGINE_PROJECT_ID_BUFFER_LIST_KEY, "delayed_workflow")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example of how this will be done for workflow engine

Copy link

codecov bot commented Jan 17, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
324 1 323 10
View the top 1 failed tests by shortest run time
::tests.sentry.buffer.test_redis
Stack Traces | 0s run time
#x1B[31mImportError while importing test module '.../sentry/buffer/test_redis.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.1.../x64/lib/python3.13/importlib/__init__.py#x1B[0m:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
#x1B[1m#x1B[.../sentry/buffer/test_redis.py#x1B[0m:21: in <module>
    from sentry.rules.processing.buffer_processing import process_delayed_alert_conditions
#x1B[1m#x1B[31mE   ImportError: cannot import name 'process_delayed_alert_conditions' from 'sentry.rules.processing.buffer_processing' (.../rules/processing/buffer_processing.py)#x1B[0m#x1B[0m

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

Comment on lines 88 to 98
try:
processing_info = delayed_processing_registry.get(processing_type)(project_id)
except NoRegistrationExistsError:
logger.exception(log_format.format(processing_type, "no_registration"))
return

hash_args = processing_info.hash_args
task = processing_info.processing_task
filters: dict[str, models.Model | str | int] = asdict(hash_args.filters)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is new, depending on the processing_type (basically if we're processing rules or workflows) we'll have different args to pass when getting things from the buffer or pushing to it, and we'll have a different task to kick off to query snuba to evaluate slow conditions.

the rest of the logic below just uses this information and is the same as before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we give models.Model | str | int type a name to help illustrate the different args for this?

@cathteng cathteng marked this pull request as ready for review January 21, 2025 18:03
@cathteng cathteng requested a review from a team as a code owner January 21, 2025 18:03
@cathteng cathteng requested a review from a team January 21, 2025 18:03
@cathteng cathteng force-pushed the cathy/aci/delayed-processing-setup branch from 2a7b002 to a8e8717 Compare January 21, 2025 22:59
Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall i feel like this approach is great, i think it's mostly making sure we're making this generic in a nicely composable way. i think the biggest callout is that i think we should take a look at how we're registering to the FLUSH event in the buffer. right now i think it's a little split between two worlds, being generic and being specific. if the desire is to be highly generic, then i think we should rename some stuff or maybe move some code around. if teh desire is to be specific; we could change the buffer registry to have multiple handlers for a single event.

src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
@cathteng cathteng force-pushed the cathy/aci/delayed-processing-setup branch from a8e8717 to e98e919 Compare January 28, 2025 18:30
@cathteng cathteng requested review from saponifi3d and a team January 28, 2025 18:30
@cathteng
Copy link
Member Author

@saponifi3d i made it more generic

Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this approach looks great, just the nits / little cleanups 🎉

src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
Comment on lines 88 to 98
try:
processing_info = delayed_processing_registry.get(processing_type)(project_id)
except NoRegistrationExistsError:
logger.exception(log_format.format(processing_type, "no_registration"))
return

hash_args = processing_info.hash_args
task = processing_info.processing_task
filters: dict[str, models.Model | str | int] = asdict(hash_args.filters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we give models.Model | str | int type a name to help illustrate the different args for this?

src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
src/sentry/rules/processing/buffer_processing.py Outdated Show resolved Hide resolved
Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 looks great, thanks for all the clean up.

should_emit_logs = options.get("delayed_processing.emit_logs")

for processing_type, handler in delayed_processing_registry.registrations.items():
with metrics.timer(f"{processing_type}.process_all_conditions.duration"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have a consistent prefix on the metrics for delayed_processing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly, i can clean this up in a follow up

@cathteng cathteng merged commit e05d7bc into master Jan 30, 2025
49 checks passed
@cathteng cathteng deleted the cathy/aci/delayed-processing-setup branch January 30, 2025 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants