Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions snuba/web/db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,16 +914,32 @@ def _apply_allocation_policies_quota(
stats["quota_allowance"]["summary"] = summary

if not can_run:
rejecting_policy = (
rejection_quota_and_policy.policy.class_name()
if rejection_quota_and_policy is not None
else "unknown"
)
span.set_data("policy", rejecting_policy)
span.set_data("action", "rejected")
metrics.increment(
"rejected_query",
tags={"storage_key": allocation_policies[0].resource_identifier.value},
tags={
"storage_key": allocation_policies[0].resource_identifier.value,
"policy": rejecting_policy,
},
)
raise AllocationPolicyViolations.from_args(stats["quota_allowance"])

if throttle_quota_and_policy is not None:
throttling_policy = throttle_quota_and_policy.policy.class_name()
span.set_data("policy", throttling_policy)
span.set_data("action", "throttled")
metrics.increment(
"throttled_query",
tags={"storage_key": allocation_policies[0].resource_identifier.value},
tags={
"storage_key": allocation_policies[0].resource_identifier.value,
"policy": throttling_policy,
},
)
else:
metrics.increment(
Expand Down
12 changes: 12 additions & 0 deletions tests/web/test_db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ def __init__(self, max_threads: int, policy_name: str) -> None:
},
}
}
throttled_metrics = get_recorded_metric_calls("increment", "db_query.throttled_query")
assert throttled_metrics
assert throttled_metrics[0].tags == {
"storage_key": "doesntmatter",
"policy": "ThrottleAllocationPolicy1",
}


def test_db_query_with_rejecting_allocation_policy() -> None:
Expand Down Expand Up @@ -950,6 +956,12 @@ def _update_quota_balance(
assert update_called, (
"update_quota_balance should have been called even though the query was rejected but was not"
)
rejected_metrics = get_recorded_metric_calls("increment", "db_query.rejected_query")
assert rejected_metrics
assert rejected_metrics[0].tags == {
"storage_key": "doesntmatter",
"policy": "RejectAllocationPolicy",
}


@pytest.mark.events_db
Expand Down
Loading