Skip to content

Commit

Permalink
fix(dynamic-sampling): enusre that at leas one project has count (#81727
Browse files Browse the repository at this point in the history
)

Make sure that at least one project with counts is present before doing
a Project Rebalancing run

Closes #81556
  • Loading branch information
constantinius authored Dec 6, 2024
1 parent 1fd4fe8 commit f336c6b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ def adjust_sample_rates_of_projects(
projects_with_counts = {
project_id: count_per_root for project_id, count_per_root, _, _ in projects_with_tx_count
}
# The rebalancing will not work (or would make sense) when we have only projects with zero-counts.
if not any(projects_with_counts.values()):
return

# Since we don't mind about strong consistency, we query a replica of the main database with the possibility of
# having out of date information. This is a trade-off we accept, since we work under the assumption that eventually
# the projects of an org will be replicated consistently across replicas, because no org should continue to create
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import timedelta
from typing import cast
from unittest.mock import patch

from django.utils import timezone

Expand Down Expand Up @@ -183,6 +184,22 @@ def test_project_mode_sampling_with_query(self):

assert get_guarded_project_sample_rate(org1, p1) == 0.2

@with_feature(["organizations:dynamic-sampling", "organizations:dynamic-sampling-custom"])
def test_project_mode_sampling_with_query_zero_metrics(self):
organization = self.create_organization("test-org")
project = self.create_project(organization=organization)

organization.update_option("sentry:sampling_mode", DynamicSamplingMode.PROJECT)
project.update_option("sentry:target_sample_rate", 0.2)

# make sure that no rebalancing is actually run
with patch(
"sentry.dynamic_sampling.models.projects_rebalancing.ProjectsRebalancingModel._run"
) as mock_run:
with self.tasks():
boost_low_volume_projects.delay()
assert not mock_run.called

def test_complex(self):
context = TaskContext("rebalancing", 20)
org1 = self.create_organization("test-org1")
Expand Down

0 comments on commit f336c6b

Please sign in to comment.